I have a web application deployed on IIS. This web application is consuming a library which wants to access the Web.config.
Example :
Foo.dll is the web application deployed on IIS
Foo.Utility.dll is consumed by Foo.dll
There is a piece of code in Foo.Utility namepsace which wants to access the web.config from Foo application and read the config values
Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
string cacheDir = config.AppSettings.Settings["abc"].Value;
Currently config.FilePath = C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\web.config
Changed my code to :
Configuration config = WebConfigurationManager.OpenWebConfiguration(Assembly.GetCallingAssembly().Location);
string cacheDir = config.AppSettings.Settings["abc"].Value;
Now my Assembly.GetCallingAssembly().Location is : C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\62f5c902\849205ff\assembly\dl3\c28d4647\10e128d3_7449d001\foo.dll
Can someone help me to understand how to read the web.config from the place where my application is deployed using IIS?
For more information or if the question is not clear then comment below. Will update it
You have to use ConfigurationManager from System.Configuration. First, you'll have to add a reference to System.Configuration.dll assembly, and then use it like this:
using System.Configuration;
...
string valueOfAbc = ConfigurationManager.AppSettings["abc"];
ConfigurationManager will read the config file from the application's host. In your case, the web.config file.
Reference:
ConfigurationManager Class
Application Configuration Files
Related
I have folder structure as below
ConsoleApp
app.config
WindowService
service
app.config
DataLayer
I need to get app.config data such as connection string in DataLayer.I am not able to get. It throws null reference. In consoleApp and WindowService project, its is working.
please suggest me how to resolve this issue.
By default .NET application uses it's own app.config file when you access it via ConfigurationManager. In order to open another project's configuration you have to load it manually.
The following code snippet opens an external app.config located under the data layer project and accesses the ConnectionStrings section of that file:
var map = new ExeConfigurationFileMap() { ExeConfigFilename = #"path\to\datalayer\app.config" };
var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
var connectionString = config.ConnectionStrings.ConnectionStrings["myDBConn"];
I have two web api projects
Yugasat.Authentication.ServiceLayer
Yugasat.User.ServiceLayer
each with its own web.config files
For some reason the Yugasat.Authentication.ServiceLayer project is using the web.config file of the Yugasat.User.ServiceLayer project. When I add invalid text to the web.config file of the Yugasat.User.ServiceLayer project
and run the Yugasat.Authentication.ServiceLayer I can see that it is using the wrong web.config file
Is there any reason why this will happen?
I'm creating a web application, which calls a DLL to run unit tests, I also have another DLL(DataAccessLayer) which performs connections and performs queries to SQL which references the main DLL. Both the DLLs use the same config file to read settings.
When running application from VS, the application is working fine. However when the web app is deployed to IIS, it seems the DLLs are unable to read the settings from the config file.
After some research I found that I might have to explicitly define the configuration elements in the web.config file, however I don't know how to implement this. Can someone please point me in the right direction?
I'm actually retrieving the settings using the ConfigurationManager with the following code:-
public string GetValue(string key)
{
var appConfig = ConfigurationManager.OpenExeConfiguration("path to dll");
strKeyValue = appConfig.AppSettings.Settings[key].Value;
return strKeyValue;
}
Thanks.
Use WebConfigurationManager.AppSettings["HelloWorldKey"]; to read AppSettings from the web.config.
Just set all the appSettings values used by the DLL you mention, directly in the web.config PRIOR to deploying the app. You don't need to modify this at run-time (and you shouldn't anyway, since any modification to the web.config will cause the application to restart)
Add the connectionstring or AppSetting or ApplicationSettings used in you app.config into your web.config, I understand this is a manual task but is the only way that the config will read the settings.
Use following code to access connection string
string filePath= WebConfigurationManager.AppSettings["Pathfile"].ToString();
Web config Fie
<configuration>
....
<appSettings>
<add key="Pathfile" value="Path to dll"/>
</appSettings>
....
</configuration>
I have a Windows application called TechReader. Its configuration file is TechReader.exe.config. Some parts of the configuration are kept in other configuration files. So I refer to that config file from the config section created in the exe.config.
<TechReader.ProviderConfiguration file="localProvider.config"/>
Now I want to load the whole configuration of my application using reflection. I use code like this.
Assembly techReaderAssembly = Assembly.GetAssembly(typeof(TechReaderStarter));
ConfigurationManager.OpenExeConfiguration(techReaderAssembly .Location);
TechReaderStarter class is defined in the project whose output is windows application and not the library.
When I use above code, I get TargetInvocationException and ConfigurationErrorsException
Is the approach correct?
Will OpenExeConfiguration load the final configuration generated by merging the exe.config and other referenced config file?
How can I achieve the things?
Note: I want to use this Windows application to install as a Windows Service. I am trying to read the configuration in the ServiceInstaller class (a class which inherits ServiceInstaller) so that the details like service name mentioned in the configuration of the Service will be available to the installutil. For this I have to use reflection to get the exact configuration of the service.
I have a .NET 3.5 class library I built that reads an App.config file for values it needs. It can pull the config values just fine when I test it in Visual Studio. To test it, I just change the project to a console application and execute a method call.
I have the need to call this class library from many other .NET programs, and I want the class library to be self sufficient (I should be able to call it from any other program, and it should use its own config file, not know about any calling config file etc.).
I can add a reference to the dll (since I am still development I am using VS 2008, haven't thrown anything into the GAC yet) but the App.config that the class library is reading is from the calling program's App.config, not the class library's App.config.
The class library dll has it's config file in the same directory, so it should be able to find it just fine, and the calling application is named differently. I am using the standard key value pairs in the App.config (e.g. name of config file myClassLibrary.dll.config) and getting values out with the following line of code:
String myVal = ConfigurationSettings.AppSettings["myConfigSetting"];
Does anyone know how to fix this?
An app domain in C# can have only one assembly level app.config file. See here on MSDN. An executable will always start up an AppDomain and by default look for a config file with name: EXECUTABLE_NAME.config. For example, SampleApp01.exe will look for SampleApp01.exe.config as its configuration file.
you can place your configs in the machine.config file inside the framework folder by this way you can globally use your configuration in all .Net applications running in that machine,
I believe app.config will always be used by the executable. Just drop it in that directory.
They would do that to ensure the dll can be shared and not have to share the same .config file.
You might be able to create a link from the executable .config file
<appSettings configSource="\lib\app.config">
Or change its name, i don't understand how you can have both app.config files in the same directory..don't they have the same name?
<appSettings configSource="\lib.app.config">
I can't find a way to avoid getting the app.config for the calling dll/exe etc. The only way I have found is to use a hardcoded path and load it that way. Here is code I am using to do that:
using System.Configuration;
...
public static KeyValueConfigurationCollection getAppSettingsFromAppConfig(String appConfigPath) {
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = appConfigPath;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
AppSettingsSection section = config.AppSettings;
KeyValueConfigurationCollection appsettings = section.Settings;
return appsettings;
}
You then have a collection of KeyValueConfigurationElement, which you can use .Value to get the string from config file with.