1

I am trying to integrate mvc-mini-profiler to asp.net mvc application with Llblgen used for data access. I have tried to override llblgen's CommonDaoBase.CreateConnection:

public override DbConnection CreateConnection(string connectionString)
{
  return MvcMiniProfiler.Data.ProfiledDbConnection.Get(base.CreateConnection(connectionString));
}

but this resulted in exception, saying 'Cannot cast to SqlConnection...'. Has someone got mvc-mini-profiler working with llblgen?

Adas Petrovas
  • 575
  • 5
  • 7

2 Answers2

1

Miha Markic just posted the answer in his blog: http://blog.rthand.com/post/2011/07/24/Integrating-MvcMiniProfiler-and-LLBLGenPro.aspx

Community
  • 1
  • 1
David Elizondo
  • 1,123
  • 1
  • 7
  • 16
0

You need LLBLGen Pro v3. V2 does use dbproviderfactory for sqlserver but the generated code has some hardcoded casts still to SqlClient classes. v3 doesn't. I assume you use v2?

Additionally, a tiny change has to be made to the ProfiledDbProviderFactory.cs of the profiler:

/// <summary>
/// Extension mechanism for additional services;  
/// </summary>
/// <returns>requested service provider or null.</returns>
object IServiceProvider.GetService(Type serviceType)
{
    if(serviceType == typeof(ProfiledDbProviderFactory))
    {
        // For LLBLGen Pro v3 and up.
        return tail;
    }
    IServiceProvider tailProvider = tail as IServiceProvider;
    if (tailProvider == null) return null;
    var svc = tailProvider.GetService(serviceType);
    if (svc == null) return null;

#if ENTITY_FRAMEWORK
    if (serviceType == typeof(DbProviderServices))
    {
        svc = new ProfiledDbProviderServices((DbProviderServices)svc, profiler);
    }
#endif
    return svc;
}

EDIT. I am not sure whether this will work, as our framework uses the DbProviderFactory obtained from ADO.NET. That factory is the real Sqlclient's factory not the ProfiledDbProviderFactory. You can create a profiled connection here, but that's not going to work: the factory isn't obtained from the connection, the connection is created from the factory obtained from ADO.NET (DbProviderFactories.GetFactory()) and all other elements are created with that factory as well.

mvc profiler has to overwrite the datatable in DbProviderFactories through reflection with wrappers which wrap the actual factory types. This is kind of nasty but the only way to make things work with data-access code which does things by the book: get the factory, create elements through the factory.

it surprises me Linq to sql and Entity framework apparently work with the factory obtained from a connection object: how did one create that connection object in the first place?

Frans Bouma
  • 8,259
  • 1
  • 27
  • 28
  • I am using LLBLGen Pro v3.1.11.0207. I've tried to overwrite DbProviderFactories table, but it turns out, that ProfiledDbProviderFactory provided by MvcMiniProfiler is not suitable for this purpose - it is internal type and it does not have parameterless constructor. Exception I got: [InvalidOperationException: The requested .Net Framework Data Provider's implementation does not have an Instance field of a System.Data.Common.DbProviderFactory derived type.]. So, it looks like to much effort is needed and I am skipping this task for a while. – Adas Petrovas Jun 17 '11 at 14:45
  • Hmmm... that indeed is a showstopper. We're busy with our own profiler but it's not out for a while (later this year). You could try LLBLGenProf for the time being, it's not as tied to MVC as so's profiler but it will profile your llblgen pro app :) Oh, and update your llblgen pro installation with the latest build from the customer area, you're using the first v3.1 release, which is outdated, we had a couple of bugfixes since then. – Frans Bouma Jun 19 '11 at 10:01