Does MvcMiniProfiler require a web application to work, or can it be used in pure libraries, such as in unit tests?

六月ゝ 毕业季﹏ 提交于 2019-12-06 17:29:06

问题


I really like what I've seen regarding MvcMiniProfiler. I'd love to use the SQL monitoring features on non-web applications (windows services, unit tests, etc). Is this possible, or does it require an HTTP environment? The obvious difference would be results delivery, but if I can serialize the results to JSON or somesuch, that would be fine.

I threw together a simple unit test trying to get it to work, but MiniProfiler.Current appears to always be null, even after executing MiniProfiler.Start().

Am I barking up the wrong tree here?


回答1:


Excellent question. The core (the profiler instance and the connection wrappers) are not tied to http. If you can create your own instance, it should all work. However, all of the UI code is geared around web applications.

I know of people using it in WPF, winforms, etc - so it should work fine. However, you would also need to figure out a logical "unit of thing to me measured". In a web app it is obvious: a request.

Note that MiniProfiler.Current is purely a convenience (you could put an instance anywhere), but I'm pretty sure we made that possible to create your own abstraction (rather than http context).

(goes to check the code)

ah yes, here we go:

public static MiniProfiler Current
{
  get
  {
    MiniProfiler.Settings.EnsureProfilerProvider();
    return MiniProfiler.Settings.ProfilerProvider.GetCurrentProfiler();
  }
}

so you can provide your own ProfileProvider and then .Current should work.



来源:https://stackoverflow.com/questions/8526609/does-mvcminiprofiler-require-a-web-application-to-work-or-can-it-be-used-in-pur

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!