MiniProfiler in different project

418 Views Asked by At

I have the following solutions structure: 1- Asp.net Core MVC project1 (views and UI) 2- Asp.net core API project2 (api) 3- EF Core project3 (db access)

simple scenario is project 1 make call to project 2 and project 2 request data from project 3

There is no direct link between project1 and project3 How can i install MiniProfiler so that i can see all my SQL transactions in my view?

1

There are 1 best solutions below

0
Rena On BEST ANSWER

1.Install MiniProfiler.AspNetCore.Mvc in Project 1

2.Install MiniProfiler.EntityFrameworkCore in Project 1

3.ConfigureServices in Project 1:

services.AddMiniProfiler(options =>
{
    // All of this is optional. You can simply call .AddMiniProfiler() for all defaults

    // (Optional) Path to use for profiler URLs, default is /mini-profiler-resources
    options.RouteBasePath = "/profiler";
    // (Optional) Control which SQL formatter to use, InlineFormatter is the default
    options.SqlFormatter = new StackExchange.Profiling.SqlFormatters.InlineFormatter();               
}).AddEntityFramework();

4.Configure in Project 1:

app.UseMiniProfiler();
app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
});

5.Add Tag Helpers in _ViewImports.cshtml:

@using StackExchange.Profiling
@addTagHelper *, MiniProfiler.AspNetCore.Mvc

6.Add MiniProfiler to your master layout (Shared/_Layout.cshtml by default):

<div class="container">
    <partial name="_CookieConsentPartial" />
    <main role="main" class="pb-3">

        <mini-profiler/>

        @RenderBody()
    </main>
</div>

7.How to see sql transactions:

enter image description here Reference: https://miniprofiler.com/dotnet/AspDotNetCore

Here is a simple demo that I test for MiniProfiler:

  • EF Core Project 3:

    public class MyContext : DbContext
    {
        public MyContext(DbContextOptions<MyContext> options)
            : base(options)
        {
        }
        public virtual DbSet<Test> Tests { get; set; }
    }
    
  • Web Api Project 2(add reference to Project 3):

    public class ValuesController : ApiController
    {
        private readonly MyContext _context;
        public ValuesController(MyContext context)
        {
            _context = context;
        }
        [HttpGet]
        public async Task test()
        {
            var data = _context.Tests.ToList();
        }
    }
    

    Startup.cs:

    var connection = "Server=(localdb)\\mssqllocaldb;Database=EFProjectDatabase;Trusted_Connection=True;MultipleActiveResultSets=true";
    //register MyContext which is in your EF Core Project 3
    services.AddDbContext<MyContext>(
            options => options.UseSqlServer(connection));
    
  • MVC Project 1(add reference to Project 2):

1.Controller:

public class HomeController : Controller
{
    private readonly ValuesController _services;//get the web api project's ValuesController
    public HomeController(ValuesController services)
    {
        _services = services;
    }
    public IActionResult Index()
    {
        using (MiniProfiler.Current.Step("Get Existing"))
        {
            var data = _services.test();
        }
        return View();
    }
}

2.Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    //...
    services.AddMiniProfiler(options =>
    {
        options.RouteBasePath = "/profiler";
        options.SqlFormatter = new StackExchange.Profiling.SqlFormatters.InlineFormatter();
        options.TrackConnectionOpenClose = true;
    }).AddEntityFramework();

    //register ValuesController which is in your Web Api Project 2
    services.AddTransient<ValuesController>();
    var connection = "Server=(localdb)\\mssqllocaldb;Database=EFProjectDatabase;Trusted_Connection=True;MultipleActiveResultSets=true";

    //register MyContext which is in your EF Core Project 3
    services.AddDbContext<MyContext>(
            options => options.UseSqlServer(connection));
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{ 
    //...
    app.UseMiniProfiler();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}