从System.Net.Http.DelegatingHandler调用时,MiniProfiler.Current为null

toz*_*evv 12 mvc-mini-profiler asp.net-web-api

我在我的asp.net Web API项目中使用迷你探查器,并希望跟踪在自定义DelegatingHandler中运行的某些代码的性能.

MiniProfiler.Current.Step()内部的调用DelegatingHandler不会显示在结果中.同一项目中的其他调用显示正常.

进一步的调查显示,MiniProfiler.Current是从中获取HttpContext.CurrentWebRequestProfilerProvider.而HttpContext.Current从调用时为空 DelegatingHandler.

有没有更好的方法来检索MiniProfiler.Current,以便它在处理程序内部工作?

Yaa*_*lis 6

HttpContext.CurrentMiniProfiler 计时默认存储(正如您所发现的)。因此,如果您从 null 的位置调用 MiniProfiler HttpContxt.Current,则无法保存结果。解决方案是从其他地方保存(和检索)结果。

MiniProfiler 提供了更改所有结果存储和检索位置的选项(使用MiniProfiler.Settings.Storage)。新的 v3 MiniProfiler此处为 beta nuget)提供了为每个请求配置不同的选项IStorage,以及使用 来MultiStorageProvider指定可以存储和检索结果的多个位置。您可以在 github 上的Sample.Mvc项目中查看此示例。

MultiStorageProvider在您的情况下,最好的方法可能是为您的全局设置一个MiniProfiler.Settings.Storage,首先保存/检索HttpRuntimeCacheStorage,然后使用可IStorageDelegatingHandler. 然后在 中DelegatingHandler,将 中设置MiniProfiler.Current.Storage为仅使用您在 中设置的第二个存储选项MultiStorageProvider(因为尝试保存 HttpCache 是没有意义的)。在这种情况下,配置文件DelegatingHandler将被保存到您的第二个存储选项中,并将被检索以与您的其他结果一起查看(因为MultiStorageProvider将从Load第一个位置获取结果 - 如果在 HttpCache 中找不到结果,它将转到第二个选项。

注意 - 在这种情况下,拥有多个存储选项很有用,但它可能会对检索配置文件的性能产生负面影响。