Ryan Davis
Ryan Davis

Reputation: 721

MiniProfiler with Web.API 2; is there a global magic request context object?

I'm trying to setup MiniProfiler on my web api site, and having a hard time getting MiniProfiler.Current to work.

I followed the directions at miniprofiler.com, and have the following in global.asax:

protected void Application_Start()
{
  MiniProfilerEF6.Initialize();
  // other setup
}
protected void Application_BeginRequest() {
  // need to start one here in order to render out the UI
  MiniProfiler.Start();
}
protected void Application_EndRequest() {
  MiniProfiler.Stop();
}

This uses the default WebRequestProfilerProvider, which stores the actual profile object in HttpContext.Current.Items.

When I ask for MiniProfiler.Current, it looks to HttpContext.Current.

When I make a request for one of my web api URLs:

I dug through the code, and I can create my own IProfileProvider, to store the profiler object somewhere more reliable than HttpContext.Current, but I don't know where that could be.

I spent a few hours trying things out, but couldn't find a workable solution. The problems:

I'm at a loss. What I really want is a special variable.

Upvotes: 5

Views: 3372

Answers (1)

Ryan Davis
Ryan Davis

Reputation: 721

The process of putting the question together I figured it out. HttpContext.Current can get lost when you async/await things: Why is HttpContext.Current null after await?

I had to make the web.config change listed there, and adjusted my filters to use Miniprofiler.Current before any awaiting.

Also discussed at https://www.trycatchfail.com/2014/04/25/using-httpcontext-safely-after-async-in-asp-net-mvc-applications/

Upvotes: 2

Related Questions