MiniProfiler.Stop() sometimes throws HttpException

Sometimes throws “Server cannot append header after HTTP headers have been send.” exception.

I had this happen when my WebForms page through an exception but I don’t think that’s a reliable way to reproduce this.

I’m calling MiniProfiler.Stop() in my EndRequest handler.

Little experience with MiniProfiler so grain of salt time, but I was browsing about that exception on MSDN and I was also looking through the Stop() functionality. It calls Stop from an implementation of IProfilerProvider but, admittedly, I don’t know where to go from there as Stop is a stub except that BaseProfilerProvider implements IProfilerProvider (which, again, is a stub).

In regards to what I’ve found via MSDN, the exception in question appears to be typically related to a HTTPResponse object using the AddHeader function (apparently, where this has previously been set). While I’m not 100% positive that somewhere down the line Stop() doesn’t call this, just to be thorough, I’d look to see if there is anything in your EndRequest calling that function?

Fixing this is a tricky, essentially MiniProfiler uses special headers to indicate the profiling session ids.

If for any reason you are operating in a streaming mode (see http streaming, or for .net Response.Write, Response.Flush) then you will force the app to flush its headers, at that point we can not add any more headers.

I think we should simply avoid adding headers if we detect a flush happened.