Servicestack 在自己的数据库连接上启用 MiniProfiler

Servicestack enable MiniProfiler on own DB Connection

在 Mini Profiler 文档中,您可以执行以下操作:

public static DbConnection GetOpenConnection()
{
    var cnn = CreateRealConnection(); // A SqlConnection, SqliteConnection   ... or whatever

    // wrap the connection with a profiling connection that tracks timings 
    return new StackExchange.Profiling.Data.ProfiledDbConnection(cnn,     MiniProfiler.Current);
}

我如何在我自己的数据库连接上使用 Miniprofiler 实现的 Servicestack 版本在 Servicestack 中启用配置数据库连接。我有一个 return 连接字符串的函数,而不是使用 base.Db.

中内置的服务栈
public static DbConnection GetOpenConnection(string ConnectionName)
    {
        string provider = ConfigurationManager.ConnectionStrings[ConnectionName].ProviderName;
        string connectionstring = ConfigurationManager.ConnectionStrings[ConnectionName].ConnectionString;

        DbProviderFactory factory = DbProviderFactories.GetFactory(provider);
        DbConnection cnn = factory.CreateConnection();
        cnn.ConnectionString = connectionstring;
        cnn.Open();
        //this is ServiceStack current profiler for request and response DTO's
        Profiler profiler = Profiler.Current;
        // I want to return a profiled db connection here that will include the connection profiles into the current Profiler data.
        return cnn;

    }

您可以像 Ormlite 一样做,只需将您的连接与 MiniProfiler 的 ProfiledDbConnection 包装起来,例如:

return new ProfiledDbConnection(cnn, Profiler.Current)