简体   繁体   English

用于 .Net 6 的带有 MiniProfiler 的 ServiceStack

[英]ServiceStack with MiniProfiler for .Net 6

I was attempting to add Profiling into ServiceStack 6 with .Net 6 and using the .Net Framework MiniProfiler Plugin code as a starting point.我试图使用 .Net 6 将 Profiling 添加到 ServiceStack 6 中,并使用 .Net Framework MiniProfiler 插件代码作为起点。

I noticed that ServiceStack still has Profiler.Current.Step("Step Name") in the Handlers, AutoQueryFeature and others.我注意到 ServiceStack 在处理程序、AutoQueryFeature 等中仍然有Profiler.Current.Step("Step Name")

What is currently causing me some stress is the following:目前给我带来一些压力的原因如下:

  1. In ServiceStackHandlerBase.GetResponseAsync(IRequest httpReq, object request) the Async Task is not awaited.ServiceStackHandlerBase.GetResponseAsync(IRequest httpReq, object request)中,不等待异步任务。 This causes the step to be disposed of the when it reaches the first async method it must await, causing all the subsequent nested steps to not be children.这会导致该步骤在到达它必须等待的第一个异步方法时被释放,从而导致所有后续嵌套步骤都不是子步骤。 Is there something simple I'm missing here or is this just a bug in a seldom used feature?我在这里遗漏了什么简单的东西,或者这只是一个很少使用的功能中的错误?
  2. In SqlServerOrmLiteDialectProvider most of the async methods make use of an Unwrap function that drills down to the SqlConnection or SqlCommand this causes an issue when attempting to wrap a command to enable profiling as it ignores the override methods in the wrapper in favour of the IHasDbCommand.DbCommand nested within.SqlServerOrmLiteDialectProvider中,大多数异步方法都使用了Unwrap函数,该函数深入到SqlConnectionSqlCommand这会在尝试包装命令以启用分析时导致问题,因为它忽略了包装器中的覆盖方法而支持IHasDbCommand.DbCommand嵌套在里面。 Not using IHasDbCommand on the wrapping command makes it attempt to use wrapping command but hits a snag because of the forced cast to SqlCommand .未在包装命令上使用IHasDbCommand会使其尝试使用包装命令,但由于强制转换为SqlCommand而遇到障碍。 Is there an easy way to combat this issue, or do I have to extend each OrmliteDialectProvider I wish to use that has this issue to take into account the wrapping command if it is present?有没有一种简单的方法来解决这个问题,或者我是否必须扩展我希望使用的每个OrmliteDialectProvider以考虑包装命令(如果存在)?

Any input would be appreciated.任何输入将不胜感激。

Thanks.谢谢。


Extra Information Point 1额外信息点 1

Below is the code from ServiceStackHandlerBase that appears (to me) to be a bug?下面是来自 ServiceStackHandlerBase 的代码(在我看来)是一个错误?

public virtual Task<object> GetResponseAsync(IRequest httpReq, object request)
{
    using (Profiler.Current.Step("Execute " + GetType().Name + " Service"))
    {
        return appHost.ServiceController.ExecuteAsync(request, httpReq);
    }
}

I made a small example that shows what I am looking at:我做了一个小例子来展示我在看什么:

using System;
using System.Threading.Tasks;
                    
public class Program
{
    public static async Task<int> Main(string[] args)
    {
        Console.WriteLine("App Start.");
        
        await GetResponseAsync();
        
        Console.WriteLine("App End.");
        return 0;
    }
    
    // Async method with a using and non-awaited task.
    private static Task GetResponseAsync()
    {
        using(new Test())
        {
            return AdditionAsync();
        }
    }
    
    // Placeholder async method.
    private static async Task AdditionAsync()
    {
        Console.WriteLine("Async Task Started.");
        
        await Task.Delay(2000);
        
        Console.WriteLine("Async Task Complete.");
    }
}

public class Test : IDisposable 
{
    public Test() 
    {
        Console.WriteLine("Disposable instance created.");
    }

    public void Dispose() 
    {
        Console.WriteLine("Disposable instance disposed.");
    }
}

My Desired Result:我想要的结果:

App Start.
Disposable instance created.
Async Task Started.
Async Task Complete.
Disposable instance disposed.
App End.

My Actual Result:我的实际结果:

App Start.
Disposable instance created.
Async Task Started.
Disposable instance disposed.
Async Task Complete.
App End.

This to me shows that even though the task is awaited at a later point in the code, the using has already disposed of the contained object.这对我来说表明,即使在代码中稍后等待任务, using 已经处理了包含的对象。

Mini Profiler was coupled to System.Web so isn't supported in ServiceStack .NET6 . Mini Profiler 与 System.Web 耦合,因此在 ServiceStack .NET6 中不受支持

To view the generated SQL you can use a BeforeExecFilter to inspect the IDbCommand before it's executed.要查看生成的 SQL,您可以使用BeforeExecFilter在执行IDbCommand之前对其进行检查。

This is what PrintSql() uses to write all generated SQL to the console:这是PrintSql()用来将所有生成的 SQL 写入控制台的方法:

OrmLiteUtils.PrintSql();

Note: when you return a non-awaited task it just means it doesn't get awaited at that point, it still gets executed when the return task is eventually awaited.注意:当你返回一个非等待的任务时,它只是意味着它在那个时候没有被等待,当返回任务最终被等待时它仍然会被执行。

To avoid the explicit casting you should be able to override a SQL Server Dialect Provider where you'll be able to replace the existing implementation with your own.为避免显式转换,您应该能够覆盖SQL Server 方言提供程序,您可以在其中将现有实现替换为您自己的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM