0

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

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();
    }
}

国际奥委会注册

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

全球.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 时间,但事实并非如此,连接没有被分析。

是否需要启用或纠正其他东西才能使其正常工作?

谢谢你,斯蒂芬

4

1 回答 1

2

要在 MiniProfiler 中启用SQL 分析,您需要注册OrmLiteConnectionFactory以使用 MiniProfilers ProfiledDbConnection,例如:

Container.Register<IDbConnectionFactory>(c =>
    new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider) {
        ConnectionFilter = x => new ProfiledDbConnection(x, Profiler.Current)
    });
于 2014-11-25T18:49:05.070 回答