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

1

15

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?

Minni answered 15/12, 2011 at 21:9 Comment(0)
K
12

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.

Kokoschka answered 15/12, 2011 at 21:14 Comment(1)
Most excellent. I see now it isn't until WebRequestProfilerProvider that we see a reference to the HTTP Context, and that's underneath some parent abstractions that don't make any HTTP assumptions. Thanks!Minni

© 2022 - 2024 — McMap. All rights reserved.