不同项目中的 Mini Profiler

MiniProfiler in different project

我有以下解决方案结构: 1- Asp.net 核心 MVC 项目 1(视图和 UI) 2- Asp.net 核心 API 项目 2 (api) 3- EF Core 项目 3(数据库访问)

简单的场景是项目 1 调用项目 2,项目 2 从项目 3 请求数据

project1 和 project3 之间没有直接 link 我如何安装 MiniProfiler 以便在我的视图中看到我所有的 SQL 交易?

1.Install MiniProfiler.AspNetCore.Mvc 在项目 1

2.Install MiniProfiler.EntityFrameworkCore 在项目 1

3.ConfigureServices 在项目 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 在项目 1 中:

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

5.Add _ViewImports.cshtml 中的标记助手:

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

6.Add MiniProfiler 到您的主布局(默认为Shared/_Layout.cshtml):

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

        <mini-profiler/>

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

7.How 查看 sql 笔交易:

参考:https://miniprofiler.com/dotnet/AspDotNetCore

这是我为 MiniProfiler 测试的一个简单演示:

  • EF 核心项目 3:

    public class MyContext : DbContext
    {
        public MyContext(DbContextOptions<MyContext> options)
            : base(options)
        {
        }
        public virtual DbSet<Test> Tests { get; set; }
    }
    
  • Web Api 项目 2(添加对项目 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 项目 1(添加对项目 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?}");
    });
}