【问题标题】:MiniProfiler: How do I profile an AngularJS + WebAPI app?MiniProfiler:如何分析 AngularJS + WebAPI 应用程序?
【发布时间】:2016-11-10 00:11:19
【问题描述】:

交叉发布on the MiniProfiler community

我正在尝试将 MiniProfiler 放入我当前的堆栈中。我想我大部分时间都在设置,但我缺少 UI 方法,并希望获得有关最佳方式的建议。

当前堆栈

  • SQL for DB(包括 MiniProfiler 表)
  • EF 6
  • WebAPI 2 API 应用程序
  • Angular 1.x。用于 UI 的应用程序(单独的应用程序,没有 MVC 支持)——我认为目前是 1.5.x。

所以,RenderIncludes() 的当前方法对我不可用。

包含 JS 文件并设置它们以从 SQL Server 存储中检索信息的最佳方法是什么?我知道这些文件包含在 in the UI repo 中,但我没有看到明确配置的文档。

到目前为止我尝试过的东西——Web API 应用程序

  • 安装了MiniProfilerMiniProfiler.EF6 包。

Web.Config -- 添加处理程序

(不确定是否有必要):

<add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />

添加了一个 CORS 过滤器以向我的客户端应用公开 MiniProfiler ID:

public class AddMiniProfilerCORSHeaderFilter : ActionFilterAttribute
{
    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
        actionExecutedContext.Response.Headers.Add("Access-Control-Expose-Headers", "X-MiniProfiler-Ids");
    }
}

将该过滤器添加为启动的一部分:

config.Filters.Add(new AddMiniProfilerCORSHeaderFilter());`

在 Global.asax 中,添加到 Application_Start():

var connectionString = ConfigurationReader.GetConnectionString(Constants.ConfigSettings.CONNECTION_STRING_NAME);

MiniProfiler.Settings.Storage = new SqlServerStorage(connectionString);
MiniProfilerEF6.Initialize();

更新开始/结束请求:

   protected void Application_BeginRequest()
    {
        if (Request.IsLocal || ConfigurationReader.GetAppSetting(Constants.ConfigSettings.USE_PROFILER, false))
        {
            var sessionId = Guid.NewGuid().ToString();
            MiniProfiler.Start(sessionId);
        }
    }

    protected void Application_EndRequest()
    {
        MiniProfiler.Stop();
    }

到目前为止我尝试过的——客户端(Angular)应用程序

  • the Github Repo 抓取了 UI 文件
  • 将 UI 目录复制到我项目的输出中

参考 CSS:

<link rel="stylesheet" href="js/lib/miniprofiler/includes.css" />

调用 JavaScript

  <script async type="text/javascript" 
    id="mini-profiler" 
    src="js/lib/miniprofiler/includes.js?v=1.0.0.0" 
    data-current-id="" 
    data-path="https://localhost:44378/api/profiler/" 
    data-children="true" 
    data-ids="" 
    data-version="1.0.0.0" 
    data-controls="true" 
    data-start-hidden="false" 
    data-trivial-milliseconds="5">
  </script>

当前状态

当我做这些事情时,看起来它只是找不到合适的 WebAPI 控制器来呈现结果。如果我能弄清楚那个控制器在哪里或复制它(就像我目前正在尝试做的那样),我想我会做生意的。

【问题讨论】:

    标签: angularjs asp.net-web-api asp.net-web-api2 mvc-mini-profiler


    【解决方案1】:

    RenderIncludes 函数导致将&lt;script&gt; 标记输出到页面。它在 UI Repo 中定义为 include.partial.html,目前看起来像这样:

    <script async type="text/javascript" id="mini-profiler" 
            src="{path}includes.js?v={version}" data-version="{version}" 
            data-path="{path}" data-current-id="{currentId}" 
            data-ids="{ids}" data-position="{position}" 
            data-trivial="{showTrivial}" data-children="{showChildren}" 
            data-max-traces="{maxTracesToShow}" data-controls="{showControls}"
            data-authorized="{authorized}" data-toggle-shortcut="{toggleShortcut}" 
            data-start-hidden="{startHidden}" data-trivial-milliseconds="{trivialMilliseconds}">
    </script>
    

    这是运行渲染的一段 Javascript。

    RenderIncludes 函数 is defined here。它执行以下操作:

    1. 确保您已设置存储
    2. 检查当前请求是否有权查看结果
    3. 获取当前用户未查看的个人资料的 ID
    4. 将 ID 与您传递给函数的任何其他参数一起插入到 include.partial.html 中定义的脚本中的占位符中
    5. 输出这个&lt;script&gt;

    因此,如果您无法调用RenderIncludes,那么您应该没有理由不能将脚本文件放置到位,检索未查看的 ID,而是将它们与您想要的任何其他设置值一起放入 &lt;script&gt; 标签中,并输出标签。

    检索 Id 值的关键代码行是:

    var ids = authorized 
                ? MiniProfiler.Settings.Storage.GetUnviewedIds(profiler.User) 
                : new List<Guid>();
    ids.Add(profiler.Id);
    

    其中profilerMiniProfiler 的当前实例(在当前请求上运行。

    您可能还需要确保您可以处理脚本将对 /mini-profiler-resources/results 进行的调用(将分析器的 id 作为参数传递)。 GetSingleProfilerResult(HttpContext context) 函数中 is located here 的胆量

    【讨论】:

    • 谢谢!我刚刚发布了一个大型更新,记录了我当前的方法。你认为这个答案在那种情况下仍然适用吗?
    • @SeanKilleen 您的方法应该不错,但如果您不在脚本中包含 id 值,它将无济于事。
    • 是的,明白了。默认情况下我并没有真正拥有它们,因为我不知道我可以在脚本加载之前将标题放入元素的数据属性中。它确实会在下一次调用时接听它们,并尝试访问一个端点来为它们检索数据,这就是它失败的地方。试图弄清楚我是否需​​要创建该端点或只是以某种方式显示现有/默认端点。
    • 检索数据到/mini-profiler-resources/results,包括作为表单元素的id值。所以你也需要模拟这个
    • 啊,这很有道理,谢谢!当我回到代码前面时,我会尽快试一试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多