When using the web site model, I was able to access the appSettings items in my web.config file with statements such as this:
If IsProductionWeb() Then Return ConfigurationManager.AppSettings("Production")
Now with the web application project model, I have built a "class library" project and copied several class modules into this project.
I am having problems with code like above that wants to use the ConfigurationManager to retrieve AppSettings items.
I've tried adding:
Imports System.Web.Configuration
...but I am still getting "ConfigurationManager is not declared" errors
My old "web site" model shows the ConfigurationManager as belonging to
System.Configuration so I added an Imports for that too but I still get the same error at compile time.
The documentation states "Using WebConfigurationManager is the preferred way to work with configuration files related to Web applications; all other methods are deprecated". So, I am confused now. In my web site model, the ConfigurationManager gave
me a nice easy way to retrieve values from the web.config file. Now with this switch to the web application project model, this retrieval seems to not be possible in such a neat and easy way. I am also wondering how would code in this class know which web.config
file from which to retrieve items. It is compiled into a DLL separately from the web application project DLL.
Please advise how to do this the best way. Thanks.
I tried the fully qualified syntax like you suggest above and I get an immediate compiler error stating 'ConfigurationManager' is not a member of 'Configuration'.
Now I am suspecting something is wrong with my setup here. I found this in some documentation for .Net Framework 2.0 :
"The ConfigurationManager class allows you to access machine, application, and user configuration information. This class replaces the
ConfigurationSettings class, which is deprecated."
This is documented as a class member in System.Configuration like you describe above. So your code should work for me. My intellisense exposes
System.Configuration.ConfigurationSettings only (nothing for ConfigurationManager).
Please note that above where I contrast how ConfigurationManager is a member of System.Configuration but only in my web site model. Now that I am trying to use this code to access appSettings items from a class library project, there is no
System.Configuration.ConfigurationManager
I am very confused with this web application project model.
1. I had to add a reference in my class library project to System.Configuration because the syntax of System.Configuration!System.Configuration.ConfigurationManager will not compile.
2. I changed the code to read as follows:
If IsProductionWeb() Then Return System.Configuration.ConfigurationManager.AppSettings("Production")
<<< compared to the original code in my web "site" which was: >>>
If IsProductionWeb() Then Return ConfigurationManager.AppSettings("Production")
So this fellow Scott Cate explained on his blog that this is a totally different ConfigurationManager. No wonder this is confusing. I just got rid of the compiler complaints so I don't even know if this will work correctly (I am a little unsure of whether
this will pointing at the web.config of the web project that will consume this class library project.
John
Marked as answer by jjamjatra on Oct 12, 2007 01:52 PM
I was about to suggest that. Sometimes you have to add more references to the project. For example a class library might need a reference to the system.web dll for access to the web controls, or page class, etc.
I had to upload a VB project built in VS 2005 professional edition to Team Suite. They have used ConfigurationManager.AppSettings in VB class and it works fine with them. But when I ran my program, errors out saying
ConfigurationManager is not declared. I tried Imports System.Configuration and added reference too. It does not work. I have used ConfigurationManager inside .asmx file it worked fine. How do I access it inside VB class for asp.net application??????
I found something via Google about a "new" configuration manager that had a syntax with a bang symbol in it that was disallowed by the compiler (weird..cannot explain fully). Yet I think I had the same problem and this blog entry I found somewhere explained
it better. Anyway, when I had a web "site" I had a VB class module in the App_Code folder that needed access to the AppSettings entry of my web.config file. For that setup I simply accessed ConfigurationManager.AppSettings("NameOfKey") as in this example:
Public Overloads Function DetermineProperDataBase() As String
If HMDAData() Then
If IsProductionWeb() Then Return ConfigurationManager.AppSettings("HMDAProduction")
Return ConfigurationManager.AppSettings("HMDADevelopment")
Else
If IsProductionWeb() Then Return ConfigurationManager.AppSettings("Production")
If IsStagingWeb() Then Return ConfigurationManager.AppSettings("Staging")
If IsDevelopmentWeb() Then Return ConfigurationManager.AppSettings("Development")
End If
Return ConfigurationManager.AppSettings("Production")
End Function
When I moved to a "web application project", I found I could not use App_Code anymore and I ended up putting all of this type of code in a separately compiled "class library project" and then referencing that bin\debug\myClassLib.dll in my main web app project. With a setup like that, I had to make the following changes (note the extra syntax and the explicit Imports statement):
Imports System.Web.Configuration
Public Overloads Function DetermineProperDataBase() As String If HMDAData() Then If IsProductionWeb() Then ReturnSystem.Configuration.ConfigurationManager.AppSettings("HMDAProduction") Return System.Configuration.ConfigurationManager.AppSettings("HMDADevelopment") Else If IsProductionWeb() Then Return System.Configuration.ConfigurationManager.AppSettings("Production") If IsStagingWeb() Then Return System.Configuration.ConfigurationManager.AppSettings("Staging") If IsDevelopmentWeb() Then Return System.Configuration.ConfigurationManager.AppSettings("Development") End If Return System.Configuration.ConfigurationManager.AppSettings("Production") End Function
This change worked for me. I wish I could explain more about the "WHY" of this change. Good luck.
Thank you for taking your time to reply. I was trying your solution and then it showed the error as System.Web.Extension cannot be found and nuinit framework was not found. I installed it. then my build failed with no errors!!! So I removed the whole
project and restarted the system. Then I unzipped the project and build it. It worked fine. So the Error Message was misleading me all the time.
jjamjatra
Member
185 Points
203 Posts
How to access configuration appSettings from a class library project
Oct 11, 2007 05:31 PM|LINK
When using the web site model, I was able to access the appSettings items in my web.config file with statements such as this:
If IsProductionWeb() Then Return ConfigurationManager.AppSettings("Production")
Now with the web application project model, I have built a "class library" project and copied several class modules into this project.
I am having problems with code like above that wants to use the ConfigurationManager to retrieve AppSettings items.
I've tried adding:
Imports System.Web.Configuration
...but I am still getting "ConfigurationManager is not declared" errors
My old "web site" model shows the ConfigurationManager as belonging to System.Configuration so I added an Imports for that too but I still get the same error at compile time.
The documentation states "Using WebConfigurationManager is the preferred way to work with configuration files related to Web applications; all other methods are deprecated". So, I am confused now. In my web site model, the ConfigurationManager gave me a nice easy way to retrieve values from the web.config file. Now with this switch to the web application project model, this retrieval seems to not be possible in such a neat and easy way. I am also wondering how would code in this class know which web.config file from which to retrieve items. It is compiled into a DLL separately from the web application project DLL.
Please advise how to do this the best way. Thanks.
docluv
Star
12685 Points
2005 Posts
ASPInsiders
MVP
Re: How to access configuration appSettings from a class library project
Oct 11, 2007 06:46 PM|LINK
This works for me, I think I had to explicitally call the full namespace to get it to work. I never knew why...
System.Configuration.ConfigurationManager.AppSettings([VarName])
jjamjatra
Member
185 Points
203 Posts
Re: How to access configuration appSettings from a class library project
Oct 11, 2007 07:14 PM|LINK
Thanks.
I tried the fully qualified syntax like you suggest above and I get an immediate compiler error stating 'ConfigurationManager' is not a member of 'Configuration'.
jjamjatra
Member
185 Points
203 Posts
Re: How to access configuration appSettings from a class library project
Oct 11, 2007 07:36 PM|LINK
Chris,
Now I am suspecting something is wrong with my setup here. I found this in some documentation for .Net Framework 2.0 :
This is documented as a class member in System.Configuration like you describe above. So your code should work for me. My intellisense exposes System.Configuration.ConfigurationSettings only (nothing for ConfigurationManager).
Please note that above where I contrast how ConfigurationManager is a member of System.Configuration but only in my web site model. Now that I am trying to use this code to access appSettings items from a class library project, there is no System.Configuration.ConfigurationManager
I am very confused with this web application project model.
jjamjatra
Member
185 Points
203 Posts
Re: How to access configuration appSettings from a class library project
Oct 11, 2007 08:19 PM|LINK
I found something here:
http://weblogs.asp.net/scottcate/archive/2005/06/13/Looking-for-the-new-ConfigurationManager_3F00_.aspx
Here is how I fixed the compiler complaints:
1. I had to add a reference in my class library project to System.Configuration because the syntax of System.Configuration!System.Configuration.ConfigurationManager will not compile.
2. I changed the code to read as follows:
If IsProductionWeb() Then Return ConfigurationManager.AppSettings("Production")So this fellow Scott Cate explained on his blog that this is a totally different ConfigurationManager. No wonder this is confusing. I just got rid of the compiler complaints so I don't even know if this will work correctly (I am a little unsure of whether this will pointing at the web.config of the web project that will consume this class library project.
docluv
Star
12685 Points
2005 Posts
ASPInsiders
MVP
Re: How to access configuration appSettings from a class library project
Oct 11, 2007 11:40 PM|LINK
I was about to suggest that. Sometimes you have to add more references to the project. For example a class library might need a reference to the system.web dll for access to the web controls, or page class, etc.
albear99
Member
2 Points
1 Post
Re: How to access configuration appSettings from a class library project
Oct 22, 2007 09:08 AM|LINK
Just to clear up all you need to do is add a reference to system.configuration to your class library references.
mnirmala@hot...
Member
27 Points
11 Posts
Re: How to access configuration appSettings from a class library project
Nov 07, 2007 08:12 PM|LINK
I had to upload a VB project built in VS 2005 professional edition to Team Suite. They have used ConfigurationManager.AppSettings in VB class and it works fine with them. But when I ran my program, errors out saying
ConfigurationManager is not declared. I tried Imports System.Configuration and added reference too. It does not work. I have used ConfigurationManager inside .asmx file it worked fine. How do I access it inside VB class for asp.net application??????
jjamjatra
Member
185 Points
203 Posts
Re: How to access configuration appSettings from a class library project
Nov 07, 2007 09:04 PM|LINK
I found something via Google about a "new" configuration manager that had a syntax with a bang symbol in it that was disallowed by the compiler (weird..cannot explain fully). Yet I think I had the same problem and this blog entry I found somewhere explained it better. Anyway, when I had a web "site" I had a VB class module in the App_Code folder that needed access to the AppSettings entry of my web.config file. For that setup I simply accessed ConfigurationManager.AppSettings("NameOfKey") as in this example:
Public Overloads Function DetermineProperDataBase() As String If HMDAData() Then If IsProductionWeb() Then Return ConfigurationManager.AppSettings("HMDAProduction") Return ConfigurationManager.AppSettings("HMDADevelopment") Else If IsProductionWeb() Then Return ConfigurationManager.AppSettings("Production") If IsStagingWeb() Then Return ConfigurationManager.AppSettings("Staging") If IsDevelopmentWeb() Then Return ConfigurationManager.AppSettings("Development") End If Return ConfigurationManager.AppSettings("Production") End FunctionWhen I moved to a "web application project", I found I could not use App_Code anymore and I ended up putting all of this type of code in a separately compiled "class library project" and then referencing that bin\debug\myClassLib.dll in my main web app project. With a setup like that, I had to make the following changes (note the extra syntax and the explicit Imports statement):
This change worked for me. I wish I could explain more about the "WHY" of this change. Good luck.System.Configuration.ConfigurationManager.AppSettings
mnirmala@hot...
Member
27 Points
11 Posts
Re: How to access configuration appSettings from a class library project
Nov 08, 2007 02:16 PM|LINK
Thank you for taking your time to reply. I was trying your solution and then it showed the error as System.Web.Extension cannot be found and nuinit framework was not found. I installed it. then my build failed with no errors!!! So I removed the whole project and restarted the system. Then I unzipped the project and build it. It worked fine. So the Error Message was misleading me all the time.