D
David Jackson
Hello,
Following advice from Gregory A. Beamer, I have written a C# class library
which contains several base classes to provide functionality such as
database access, mail, encryption and so on.
This class library is intended to be reused unmodified in all subsequent
WinForms and WebForms development in Visual Studio.NET 2008 / C'#.
For WinForms projects, I want the class library to fetch the database
ConnectionString from the main application's config file. No problem with
that:
connectionString =
System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString);
But, for WebForms, I want the class library to fetch it from the main web
app's Application cache. We're a small company and have only one database
so, to avoid opening and reading web.config every time we need to access the
database, we fetch the ConnectionString out of web.config in
Application_Start and store it as an Application variable:
protected void Application_Start(Object sender, EventArgs e)
{
Application["SQLConnectionString"] =
System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString);
}
Then when it's needed again it can be fetched out of Application memory:
connectionString =
System.Web.HttpContext.Current.Application["SQLConnectionString"].ToString();
This now presents me with two problems.
1. I can't seem to find any way for the class library to work out if it's
being used by a WinForms project or a WebForms project. I could include an
extra parameter with all the class library's methods, I suppose, but at that
point I may as well just pass the connection string anyway! Can anyone
please tell me if a class library knows what type of project it is being
referenced in? I have tried to find a way with Reflection but confess that
that is a bit out of my league at the moment.
2. When the class library is being used in a WebForms project it seems to be
able to work with the main web app's Application cache only when running on
my local development machine. When I deploy it to the test webserver it is
unable to find it. Has anyone seen this behaviour before and knows what I've
missed?
Thank you.
DJ
Following advice from Gregory A. Beamer, I have written a C# class library
which contains several base classes to provide functionality such as
database access, mail, encryption and so on.
This class library is intended to be reused unmodified in all subsequent
WinForms and WebForms development in Visual Studio.NET 2008 / C'#.
For WinForms projects, I want the class library to fetch the database
ConnectionString from the main application's config file. No problem with
that:
connectionString =
System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString);
But, for WebForms, I want the class library to fetch it from the main web
app's Application cache. We're a small company and have only one database
so, to avoid opening and reading web.config every time we need to access the
database, we fetch the ConnectionString out of web.config in
Application_Start and store it as an Application variable:
protected void Application_Start(Object sender, EventArgs e)
{
Application["SQLConnectionString"] =
System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString);
}
Then when it's needed again it can be fetched out of Application memory:
connectionString =
System.Web.HttpContext.Current.Application["SQLConnectionString"].ToString();
This now presents me with two problems.
1. I can't seem to find any way for the class library to work out if it's
being used by a WinForms project or a WebForms project. I could include an
extra parameter with all the class library's methods, I suppose, but at that
point I may as well just pass the connection string anyway! Can anyone
please tell me if a class library knows what type of project it is being
referenced in? I have tried to find a way with Reflection but confess that
that is a bit out of my league at the moment.
2. When the class library is being used in a WebForms project it seems to be
able to work with the main web app's Application cache only when running on
my local development machine. When I deploy it to the test webserver it is
unable to find it. Has anyone seen this behaviour before and knows what I've
missed?
Thank you.
DJ