MiniProfiler / dotnet

A simple but effective mini-profiler for ASP.NET (and Core) websites
https://miniprofiler.com/dotnet/
MIT License
2.93k stars 603 forks source link

MiniProfiler V3 - WebAPI profiling Issue possibly related thread safety #415

Closed rubans closed 4 years ago

rubans commented 5 years ago

I'm Currently using MiniProfiler V3 with WebAPI (Owin), how I'm seeing at times intermittently errors related to the using MiniProfiler.Stop(). Actual Exception:

Object reference not set to an instance of an object. at StackExchange.Profiling.MiniProfiler.StopImpl() in c:\TeamCity\buildAgent\work\a743336045964cbc\StackExchange.Profiling\MiniProfiler.cs:line 485 I'm using the SingletonProfileProvider, from what I read this is not considered thread safe but not sure what the alternative as I had a look around but seems a bit all over the place. If I don't use this particular Provider then find my actual MiniProfiler.Current instance is always null. Any ideas?

NickCraver commented 5 years ago

I would suggest using MiniProfiler v4 - lots of changes since then, especially around async if that's a factor for you. The way we keep the context in general has fundamentally changed (to AsyncLocal<T>)...and it's very unlikely a v3 update will ever be shipped :)

If we need to add something to make v4 work for you, let's chat! I don't personally use WebAPI for anything, so it's possible there's a gap that needs filling...if so, let's fill it.

rubans commented 5 years ago

Hi Nick, Thanks for the quick response, so in theory v4 should work with WebAPI? Will try it out. Do I need to still worry about explicitly setting the ProfilerProvider?

NickCraver commented 5 years ago

Can you share your current config setup? That'd help me advise!

rubans commented 5 years ago

Presumably the app.config? app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net configSource="log4net.config" />
  <appSettings configSource="CommonAppSettings.config" />
  <connectionStrings configSource="CommonConnectionStrings.config" />
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" />
      </dependentAssembly>

</configuration>
NickCraver commented 5 years ago

Are you doing any config in code, e.g. setting miniprofiler options today? Docs on how to set this up at all at https://miniprofiler.com/dotnet/AspDotNet - but admittedly I haven't tried this scenario since we don't use WebAPI in general...I believe others are though. If you get it working I'd love to add a documentation section/page to help others with the same.

rubans commented 5 years ago

Ok Nick, I was able to use it with v4 and seemed to be working a lot better across concurrent threads but still need a lot more testing on this. However, I was also using MiniProfiler.Integrations for SQL command profiling and had incompatibility issues with this and latest version so had to stash the code for now. I will get back to it when I have a moment.

NickCraver commented 4 years ago

@rubans Just checking in here - any update?

rubans commented 4 years ago

@NickCraver The v4 on last analysis was working but wasn't a simple change to swap to that due to the upgrade required with .net framework but it seemed to be working a lot better across threads. I had to shelf it due to the nature of the project. Happy to close this issue based on that for now.

NickCraver commented 4 years ago

@rubans Thanks for the update - I hope a future upgrade is possible!