如何扩展ServiceStack IDbConnectionFactory和MiniProfiler

时间:2014-11-25 15:02:30

标签: servicestack ormlite-servicestack miniprofiler

给出了我的连接工厂的以下代码:

public interface IDbFrontEndConnectionFactory : IDbConnectionFactory
{

}

public class FrontEndDbFactory : IDbFrontEndConnectionFactory
{
    private readonly IAppSettings _settings;

    private readonly IDbConnectionFactory _dbFactory;

    public Func<IDbConnection, IDbConnection> ConnectionFilter { get; set; }

    public FrontEndDbFactory(IDbConnectionFactory dbFactory, IAppSettings settings)
    {
        _dbFactory = dbFactory;
        _settings = settings;
        ConnectionFilter = (Func<IDbConnection, IDbConnection>)(x => x);
    }

    public IDbConnection OpenDbConnection()
    {
        var tenantId = Tenant.GetTenant();
        return OpenTenant(tenantId);
    }

    public IDbConnection OpenTenant(string tenantId = null)
    {
        return tenantId != null
            ? new OrmLiteConnectionFactory(_settings.GetString("TenantId{0}:{1}".Fmt(tenantId, "Frontend"))).OpenDbConnection()
            : _dbFactory.OpenDbConnection();
    }

    public IDbConnection CreateDbConnection()
    {
        return _dbFactory.CreateDbConnection();
    }
}

IoC注册

IDbFrontEndConnectionFactory feFactory = new FrontEndDbFactory(masterDbFactory, fileSettings)
{
    ConnectionFilter = x => new ProfiledDbConnection(x, Profiler.Current)
};
container.Register(feFactory);

Global.asax中

protected void Application_Start(object sender, EventArgs e)
{
    LogManager.LogFactory = new NLogFactory();
    new AppHost().Init();
}

protected void Application_BeginRequest(object src, EventArgs e)
{
    if (Request.IsLocal)
        Profiler.Start();
}
protected void Application_EndRequest(object src, EventArgs e)
{
    Profiler.Stop();
}

存储库:

public partial class CampaignRepository : ICampaignRepository
{
    readonly ILog _log = LogManager.GetLogger(typeof(CampaignRepository));

    private readonly IDbFrontEndConnectionFactory _connectionFactory;

    public CampaignRepository(IDbFrontEndConnectionFactory connectionFactory)
    {
        _connectionFactory = connectionFactory;
    }
}

我的存储库的IoC注册

container.Register<ICampaignRepository>(c => new CampaignRepository(c.Resolve<IDbFrontEndConnectionFactory>()));

我在检查探查器时的服务方法

public CampaignViewModel Get(GetCampaign request)
{
    if (request == null) throw new ArgumentNullException("request");
    var profiler = Profiler.Current;
    using (profiler.Step("Campaign Service Get"))
    {
        try
        {
            var vm = CampaignRepository.GetCampaignInfo(request.Id).ToViewModel();
            if (vm.Campaign != null) return vm;
            Response.StatusCode = (int)HttpStatusCode.NotFound;
            return null;
        }
        catch (Exception exception)
        {
            _log.Error(request.ToJson(), exception);
            throw;
        }            
    }
}

我期望探查器显示sql时序,但事实并非如此,连接未被分析。

是否需要启用或更正其他内容才能使其正常工作?

谢谢你,Stephen

1 个答案:

答案 0 :(得分:2)

要启用SQL Profiling in MiniProfiler,您需要注册OrmLiteConnectionFactory才能使用MiniProfilers ProfiledDbConnection,例如:

Container.Register<IDbConnectionFactory>(c =>
    new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider) {
        ConnectionFilter = x => new ProfiledDbConnection(x, Profiler.Current)
    });