Link to home
Start Free TrialLog in
Avatar of jeffhodgeson
jeffhodgeson

asked on

Class Module using Session State Access now also needs to be used by windows application

I have a class module called by a web application that will set its connection string based on a session state variable set by the web app when a user logs on (actually multiple class modules).  The connection string is set when the class is created.  I now need to use the same class module in a windows application.  

Best solution I've come up with so far is to test for the existance of the System.Web.HttpContext.Current object at initialization time of the class module.  If it exists then get the connection string from the session state variable, else read the connection string from the app.config for the windows app creating the class.

Does anyone have any other ideas?  I suppose I could pass the connection string when creating the class modules.
Avatar of daveamour
daveamour
Flag of United Kingdom of Great Britain and Northern Ireland image

Personally I would just read from the config file then this should work for an App.config and a Web.config without any change to the code.
Alternativeley have the client code which creates the class pass in the connection string into the constructor that way the class is indifferent to the client code which is better architecture.
Avatar of jeffhodgeson
jeffhodgeson

ASKER

Reading from whichever config file does work fine except that there are 2 possible connection strings in the config file and the one to select is determined by the web app which creates these class modules.  This web app will set a session state variable for use by the class modules now.  The class modules do not have the needed information to determine which of the 2 connection strings to use.

For now I'm I'm checking for the existance of the System.Web.HttpContext.Current object.  If it exists I use it to get the connection string from the session state variable as the module is being consumed by the web app.  If the object does not exists the object is being consumed by the windows app and it just pulls in the primary connection string from the app.config file.

If System.Web.HttpContext.Current Is Nothing Then
   connString = ConfigurationManager.ConnectionStrings("ConnString1").ConnectionString
Else
   Dim currContext As HttpContext = System.Web.HttpContext.Current
   connString = currContext.Session("ConnString").ToString
End If
ASKER CERTIFIED SOLUTION
Avatar of daveamour
daveamour
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial