3

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 ?

  • In the code snippet from AppHarbor did you replace "ConnectionstringAlias" with your own connection string name? You have placed the code correctly, though you should move the last two lines inside the if statement. – Chris Sainty May 20 '12 at 02:23
  • show how you create your context. You either need to provide the name of the connection string in form of "name={ConnectionStringName}" as it is in the config file or you need to provide the connection string as the parameter. – Pawel May 20 '12 at 04:12
  • @ChrisSainty thanks for your answer i tried your solution, but it gives me this error: Server Error in '/' Application. Configuration Error Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. Parser Error Message: An error occurred loading a configuration file: Access to the path 'D:\websites\96\24c56\0x0049\_PublishedWebsites\Myproject\jwmgj11b.tmp' is denied. Source Error: [No relevant source lines] – William Holm Jacobsen May 21 '12 at 09:22
  • But that works fine on my local version. So guess it is something with appharbor ? – William Holm Jacobsen May 21 '12 at 10:13
  • @WilliamHJacobsen if you're writing to the instance filesystems, you need to enable that feature in application settings. – friism May 22 '12 at 18:53
  • We should probably have a setting for this, to do it automatically... – friism May 22 '12 at 18:53
  • @friism thanks alot, now it works - and yeah it would be great if it was automatically done. So your answer combined with ChrisSainty answer is the solution if anyone else struggles with the same. – William Holm Jacobsen May 22 '12 at 20:44

1 Answers1

3

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.

runesoerensen
  • 3,234
  • 2
  • 22
  • 21
friism
  • 19,068
  • 5
  • 80
  • 116
  • I had some difficulty in getting this working and thought I would share my (somewhat flakey) [solution](http://stackoverflow.com/a/12377842/609176) which I added to `App_Start()`. The code sample [here](http://support.appharbor.com/kb/add-ons/using-sequelizer) invalidates the format of my connection string as `MultipleActiveResets=true;` is added after the closing quote. Also saving the connection string every time was sending my application into an infinite loop due to the web.config update -> save -> restart -> save -> restart etc so I only save if it needs updated. – David Spence Sep 20 '12 at 11:07