如何使用Linq to SQL配置mvc mini profiler?

时间:2011-06-21 08:10:57

标签: asp.net-mvc linq linq-to-sql datacontext mvc-mini-profiler

我已经使用asp.net mvc应用程序配置了mini profiler。我也想描述我的数据库,所以我用this example中的L2S datacontext连接它。

它适用于某些查询,但在其他查询中,我发现空引用异常。当我将源代码附加到调试时,我发现了

 internal void AddSqlTiming(SqlTiming stats)
        {
            Head.AddSqlTiming(stats);
        }

Head上面方法中的属性在第198行的MiniProfiler.cs中为null。任何想法为什么?

编辑:以下方法返回datacontext对象

public static EvoletDataContext Get() 
        {
            var connection = ProfiledDbConnection.Get(new SqlConnection(ConfigurationManager.ConnectionStrings["evoletworksConnectionString"].ToString()));
            //var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["evoletworksConnectionString"].ToString());
            //return new EvoletDataContext(connection);
            return DataContextUtils.CreateDataContext<EvoletDataContext>(connection);
        }

以下是miniprofiler第一次崩溃的查询

public sysModule GetModuleHead(string actionName)
        {
            var val =  (from mod in db.sysModules
                        where
                        mod.ModuleActionResult.ToLower().Equals(actionName.ToLowerInvariant())
                    select mod).SingleOrDefault();
            return val;


        }

有关详细信息,请参阅this question。我尝试但未能在演示项目中重现问题。

编辑2:这是stacktrace:

[NullReferenceException: Object reference not set to an instance of an object.]
   MvcMiniProfiler.MiniProfiler.AddSqlTiming(SqlTiming stats) in C:\Dev\mvc-mini-profiler\MvcMiniProfiler\MiniProfiler.cs:241
   MvcMiniProfiler.SqlTiming..ctor(DbCommand command, ExecuteType type, MiniProfiler profiler) in C:\Dev\mvc-mini-profiler\MvcMiniProfiler\SqlTiming.cs:66
   MvcMiniProfiler.SqlProfiler.ExecuteStartImpl(DbCommand command, ExecuteType type) in C:\Dev\mvc-mini-profiler\MvcMiniProfiler\SqlProfiler.cs:50
   MvcMiniProfiler.SqlProfilerExtensions.ExecuteStart(SqlProfiler sqlProfiler, DbCommand command, ExecuteType type) in C:\Dev\mvc-mini-profiler\MvcMiniProfiler\SqlProfiler.cs:95
   MvcMiniProfiler.Data.ProfiledDbCommand.ExecuteDbDataReader(CommandBehavior behavior) in C:\Dev\mvc-mini-profiler\MvcMiniProfiler\Data\ProfiledDbCommand.cs:149
   System.Data.Common.DbCommand.ExecuteReader() +12
   System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +724
   System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) +189
   System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +659
   System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute(Expression expression) +59
   System.Linq.Queryable.SingleOrDefault(IQueryable`1 source) +265
   UserManagement.Models.FilterRepository.GetModuleHead(String actionName) in D:\Evolet\UserManagement\UserManagement\Models\FilterRepository.cs:14
   UserManagement.Models.DummyAttrib.OnAuthorization(AuthorizationContext filterContext) in D:\Evolet\UserManagement\UserManagement\Models\Filters.cs:30
   Glimpse.Net.Plumbing.GlimpseAuthorizationFilter.OnAuthorization(AuthorizationContext filterContext) +157
   System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor) +149
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +830
   System.Web.Mvc.Controller.ExecuteCore() +135
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +232
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +39
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +68
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +44
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +42
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +140
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +54
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +61
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +31
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +56
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +110
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +690
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +194

2 个答案:

答案 0 :(得分:3)

如果您在游戏中有ProfiledDbConnection并且实际的探查器设置为null,则会发生这种情况。

一般情况下,如果当前请求未被分析,我们会避免为内部消费者提供ProfiledDbConnection ...这意味着一切都会快一点。

不幸的是,这对EF和L2S来说有时很棘手。为了解决这个问题,我刚刚检查了一个允许你使用Profiling bespoke对象的更改,即使没有分析器在运行。

尽管如此,我的建议仍然是您在未进行概要分析时应尽可能使用原始连接。

答案 1 :(得分:0)

看起来这个问题最终在最近的提交中被修复了:

https://github.com/SamSaffron/MiniProfiler/commit/bcea578dd47d7f9ccf1f495cf67c360cdece5f2a