我已经使用asp.net mvc应用程序配置了mini profiler.我也想描述我的数据库,所以我把它与L2S datacontext挂钩,如
this example所示.
它适用于某些查询,但在其他查询中,我发现null引用异常.当我将源代码附加到调试时我发现了
internal void AddsqlTiming(sqlTiming stats) { Head.AddsqlTiming(stats); }
上面方法中的Head属性在第198行的MiniProfiler.cs中为空.任何想法为什么?
编辑:以下方法返回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:这是堆栈跟踪:
[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,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
解决方法
如果您正在使用ProfiledDbConnection且实际的探查器设置为null,则会发生这种情况.
一般来说,如果当前请求没有被分析,我们就会避免为我们的内部消费者提供ProfiledDbConnection ……这意味着一切都快一点.
不幸的是,这对EF和L2S来说有时很棘手.为了克服这个问题,我刚刚检查了一个允许你使用Profiling bespoke对象的更改,即使没有探查器在运行.
尽管如此,我的建议仍然是,在不进行概要分析时,应尽可能使用原始连接.