Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MiniProfiler, EntityFramework code first and background tasks nullreference

I use EF 4.2 code first in my mvc3 project.

miniprofiler works fine (sql + mvc), but I've got an issue with async tasks.

I perform 'em this way (is this method ok? I feel a bit uneasy with this new DatabaseContext())

public static void PerformAsycAction(this User user, Action<User> action)
{
   ThreadPool.QueueUserWorkItem(_ =>
   {
     var context = new DatabaseContext();
     MiniProfilerEF.Initialize();
     var consistantUser = context.Set<User>().Get(user.Id);
     action(consistantUser);
     context.SaveChanges();
   });
}

I've got proper line in Application_Start:

  protected void Application_Start()
    {
        MiniProfilerEF.Initialize();
        ...
    }

The excpetion is thrown during the first operation with db in action(consistantUser); here is the trace:

at MvcMiniProfiler.MiniProfiler.AddSqlTiming(SqlTiming stats) in C:\Users\sam\Desktop\mvc-mini-profiler\MvcMiniProfiler\MiniProfiler.cs:line 274 at MvcMiniProfiler.SqlTiming..ctor(DbCommand command, ExecuteType type, MiniProfiler profiler) in C:\Users\sam\Desktop\mvc-mini-profiler\MvcMiniProfiler\SqlTiming.cs:line 137 at MvcMiniProfiler.SqlProfiler.ExecuteStartImpl(DbCommand command, ExecuteType type) in C:\Users\sam\Desktop\mvc-mini-profiler\MvcMiniProfiler\SqlProfiler.cs:line 39 at MvcMiniProfiler.SqlProfilerExtensions.ExecuteStart(SqlProfiler sqlProfiler, DbCommand command, ExecuteType type) in C:\Users\sam\Desktop\mvc-mini-profiler\MvcMiniProfiler\SqlProfiler.cs:line 93 at MvcMiniProfiler.MiniProfiler.MvcMiniProfiler.Data.IDbProfiler.ExecuteStart(DbCommand profiledDbCommand, ExecuteType executeType) in C:\Users\sam\Desktop\mvc-mini-profiler\MvcMiniProfiler\MiniProfiler.IDbProfiler.cs:line 14 at MvcMiniProfiler.Data.ProfiledDbCommand.ExecuteDbDataReader(CommandBehavior behavior) in C:\Users\sam\Desktop\mvc-mini-profiler\MvcMiniProfiler\Data\ProfiledDbCommand.cs:line 158 at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)

what am I doing wrong? Any help will be appreciated.

EDIT: I tried to initialize MiniProfiler (MiniProfilerEF.Initialize();) again, in the thread where backgroung task is performd (before initiating DatabaseContext), and there is another exception now:

Unable to cast object of type 'MvcMiniProfiler.Data.EFProfiledDbConnection' to type 'System.Data.SqlClient.SqlConnection

In fact, it's not neccessary to profile the queries in the background thread, but it crashes the whole thread, so the application doesn't work properly, and I have to disavble the whole profiler. Is there a way to disable it for that, background, thread to prevent it from crashing?

like image 803
roman-roman Avatar asked Nov 13 '22 09:11

roman-roman


1 Answers

The code breaks when it tries to access your connection string because the connection string is an Entity Framework style connection string.

When passing connection string to the mini-profiler you have to extract the actual connection string from the EF connection string.

EntityConnection connection = 
    new EntityConnection(ConfigurationManager
                   .ConnectionStrings["ConnectionStringName"].ConnectionString));

string connectionString = connection.StoreConnection.ConnectionString;
like image 76
bleepzter Avatar answered Nov 16 '22 03:11

bleepzter