【问题标题】:ServiceStack Profiler NullReferenceExceptionServiceStack Profiler NullReferenceException
【发布时间】:2014-04-03 15:34:55
【问题描述】:

我认为我正确设置了 ServiceStack 的分析器,但也许我没有。我只是想把基础知识搞好。

到目前为止我做了什么

到目前为止,我为安装分析所采取的唯一步骤—— 在Global.asax.cs:

   private void Application_BeginRequest(object sender, EventArgs e)
    {
        if (Request.IsLocal)
        {
            Profiler.Start();
        }
    }

    private void Application_EndRequest(object sender, EventArgs e)
    {
        Profiler.Stop();
    }

在我的 _SiteLayout.cshtml 页面中,在呈现任何其他 javascript 文件之前,我尝试呈现以下内容:

<body>
<!-- ... -->

@Html.Raw(HttpUtility.HtmlDecode(Profiler.RenderIncludes().ToString()))

<!-- ...other scripts... -->
</body>

我收到的错误:

[NullReferenceException: 对象引用未设置为对象的实例。]

ServiceStack.MiniProfiler.UI.MiniProfilerHandler.RenderIncludes(Profiler profiler, Nullable1 position, Nullable1 showTrivial, Nullable1 showTimeWithChildren, Nullable1 maxTracesToShow, Boolean xhtml, Nullable`1 showControls, String path) +293

ServiceStack.MiniProfiler.Profiler.RenderIncludes(Nullable1 position, Nullable1 showTrivial, Nullable1 showTimeWithChildren, Nullable1 maxTracesToShow, Boolean xhtml, Nullable`1 showControls) +99

....

鉴于我正在尝试完成基础知识,我不确定此时可能为 null。在启动分析器之前是否需要某种额外的设置?会不会是路由问题?

【问题讨论】:

  • 请注意:我通常是 ServiceStack 的新手,我们主要将它用于 OrmLite 功能,因此我们可能没有进行 wiki 可能假设已经完成的其他初始设置。
  • @DourHighArch 这与 NullReferenceException 的概念无关。我在问为什么在这种特定情况下,一个库(我看不到)可能会生成 nullReferenceException。这是一个关于此时配置中的哪些内容可能为空以生成异常的问题。如果您取消投票结束,我将不胜感激。
  • 好的,但您必须为我编辑问题才能撤销我的投票。也许添加更多的 .cshtml。是否包含@ServiceStack.MiniProfiler.Profiler.RenderIncludes()
  • @DourHighArch 我大体上理解,但是如果您查看问题的中间部分,我已经专门发布了呈现包含的代码。
  • 为了防止游戏/拖钓网站不允许撤销投票,除非问题被编辑。添加更多的 .cshtml 仍然是个好主意;我怀疑问题出在您未显示的代码中。你的项目中还有其他课程吗? IIRC 他们必须在你的 global.aspx、host、DTD 或其他东西中注册。

标签: c# asp.net-mvc-4 servicestack ormlite-servicestack mvc-mini-profiler


【解决方案1】:

这种情况下的解决方案似乎是只使用标准 MiniProfiler 库而不是 ServiceStack 中包含的库。

初始设置

在 Nuget 包安装程序中,我运行了:

Install-Package MiniProfiler
Install-Package MiniProfiler.MVC4

我通过以下方式修改了global.asax

private void Application_BeginRequest(object sender, EventArgs e)
{
        MiniProfiler.Start();
}

private void Application_AuthenticateRequest(object sender, EventArgs e)
{
     //stops the profiler if the user isn't on the tech team
    var currentUser = ClaimsPrincipal.Current.Identity as ClaimsIdentity;
    if (!Request.IsLocal && !currentUser.GetGlobalRoles().Contains(Constant.Roles.TechTeam))
    {
        MiniProfiler.Stop(discardResults:true);
    }
}

private void Application_EndRequest(object sender, EventArgs e)
{
    MiniProfiler.Stop();
}

然后,在我的Layout.cshtml 文件中,在body 标签的末尾之前,我放置了:

    @MiniProfiler.RenderIncludes()    

    </body>
</html>

分析数据库连接

在返回我的 OrmLiteConnectionFactory 的代码部分中,我使用以下代码:

    private OrmLiteConnectionFactory claimFactory = new OrmLiteConnectionFactory(ConfigurationManager.ConnectionStrings["MyConnectionString"].ToString(), true, SqlServerDialect.Provider)
     {
         ConnectionFilter = x => new ProfiledDbConnection(x as System.Data.SqlClient.SqlConnection, MiniProfiler.Current)
     };

这似乎很好地分析了 SQL 和连接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    相关资源
    最近更新 更多