3
votes

I am using a SQL Server for ASP.Net session state. However, I am only able to retrieve the connection string at runtime and for that reason cannot store it in the web.config file. Usually it would be in:

<sessionState 
            mode="SQLServer"
            sqlConnectionString="data source=127.0.0.1;user id=<username>;password=<strongpassword>"
            cookieless="false" 
            timeout="20" 
    />

How can I set that connection string at runtime? (i.e. after the web app spins up.)

2

2 Answers

1
votes

Have you looked at doing it in the Application_Start event in Global.asax? This seems like the logical place to set something like that.

From MSDN's documentation:

Called when the first resource (such as a page) in an ASP.NET application is requested. The Application_Start method is called only one time during the life cycle of an application. You can use this method to perform startup tasks such as loading data into the cache and initializing static values.

You should set only static data during application start. Do not set any instance data because it will be available only to the first instance of the HttpApplication class that is created.

1
votes

I would think you can set that string somewhere within System.Web.SessionState, hopefully that will help you get to the right place. Sorry I can't give a better answer, I'm still trying to figure it out myself. If I do, I'll let you know. GL