ProfiledDbProviderFactory doesn't work / is useless

Internally DbProviderFactories looks for (and returns) the .Instance singleton from your DbProviderFactory.

This implies that to be used – e.g. by ASP.NET SqlDataSource – it has to be thread safe, which is usually easily the case, being just factories (i.e. return new SqlConnection).

But ProfiledDbProviderFactory has state: it needs an IDbProfiler (i.e. a MiniProfiler instance). You can set it with a call to InitProfiledDbProviderFactory, but everything is going to fall apart when several requests are handled at the same time by the web server…

This makes MiniProfiler unusable in an ASP.NET context, just where it should be used!
Well, sure… not all of MiniProfiler, but the ProfiledDbProviderFactory.

The fix is rather easy, I think:

  • the tail DbProviderFactory can be set once during the Application startup by the ASP.NET application. Since it will never change it’s threadsafe.

  • every access to the IDbProfiler profiler member should be replaced by an access to MiniProfiler.Current (doesn’t that make sense?).

Now the ProfieldDbProviderFactory can be set inside web.config and everything based on DbProviderFactories just work!