Search code examples
c#asp.net-mvc-3entity-frameworkappharborsql-server-mars

multiple active result set(MARS) on Appharbor how to enable?


this is my first question at stackoverflow, so bear with me.

In my local project I use ASP.NET MVC 3 and Entity Framework 4.3 with SQL Express.

in my connection string i have MARS set to true;

but when i am deploying my project on to Appharbor, Appharbor injects its own connection string,

where MARS is not set in the connectionstring. So i get this:

There is already an open DataReader associated with this Command which must be closed first.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first. 

So far best solution i have seen on this: http://support.appharbor.com/kb/add-ons/using-sequelizer

where they have this code:

var configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
var connectionString = configuration.ConnectionStrings.ConnectionStrings["ConnectionstringAlias"].ConnectionString;
if (!connectionString.Contains("MultipleActiveResultSets=True;"))
{
    connectionString += "MultipleActiveResultSets=True;";
}

configuration.ConnectionStrings.ConnectionStrings["ConnectionstringAlias"].ConnectionString = connectionString;
configuration.Save();

I can not figure out where to place this, have tried to put it in global under

Aplication_Start() method

But that gives me this error:

Server Error in '/' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 50: 
Line 51:             var configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
Line 52:             var connectionString = configuration.ConnectionStrings.ConnectionStrings["ConnectionstringAlias"].ConnectionString;
Line 53:             if (!connectionString.Contains("MultipleActiveResultSets=True;"))
Line 54:             {

Source File: C:\Users\William\Documents\Visual Studio 2010\Projects\xxx\xxx\Global.asax.cs    Line: 52 

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   Educationexpander.MvcApplication.Application_Start() in C:\Users\William\Documents\Visual Studio 2010\Projects\xxx\xxx\Global.asax.cs:52

Is there anyone out there with a solution to this problem ?


Solution

  • Update: Multiple Active Result Sets (MARS) can now be enabled for the injected connection string by using the Sequelizer admin panel. This is the recommended approach since the web.config no longer needs to be modified, which causes an AppDomain reload during startup

    Answer is in the above comments, this is just to formalise.

    You need to use the correct ConnectionstringAlias (thanks @chris-sainty) and you have to enable file system writes in application settings for the updated configuration to be written to disk.