Using a .NET Framework 4.8 as an API application for a website.
//web config
<add key="id" value="123" />
//code inside main project solution, this works
var Id = ConfigurationManager.AppSettings["id"];
//code inside extra class library project, this is NULL
var Id = ConfigurationManager.AppSettings["id"];
We are in the process of publishing to AppServices but i'm having some issues with accesing a variable.
So our project has a web.config file. I know I can set the variables here for local developent then, keeping the same name of each variable add to the Configuration section of the App service and this will override the local, on app service That is fine and working.
Inside our solution we have many extra class libary projects that are part of the solution as a whole, but not part of the actual main project itself.
When accesing one of these class library projects I can't access the variables from the web.config in the original project. (currently the value is set in code, but we want to change it to config settings so we can use it from app service configuration) (I know we could use a keyVault, which would solve the acces problem, but we arn't using this apporach)
I could add the variable to the app.config of the class library project, but then would it get picked up in the app service config settings? I'm assuming this only applies to the web.config file
I have tried many things, I was able to use the hardcoded location of the web.config file (from inside the class library project) to:
var path = @"C:\Users\PathNameOnLocalMachine";
string file = Path.GetFileName(path);
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
string id = config.AppSettings.Settings["id"].Value;
this worked but I can't use this when we go to app service I thought about trying to find the location of the web.config on app service and use that, but google says no.
My question I am getting around to asking is: is there anyway I can set a variable in app settings of the configuration settings in app serivce, and dont have it mentioned in web config file, but I can use it in the class library solution code. (which would return null on local dev) but show the correct value on app service.
I have tried this and it didn't work, still returning null. It appears the var has to be mentioned in both the web.config and config settings on appService.
Are there any other work arounds? Or any ideas please? Thank you for any advice
You have:
In the main project, accessing AppSettings works as expected:
However, the same code returns
nullin the class library project:So you need to access the
idsetting in the class library without duplicating it in anapp.configfor the library, and making sure it works both locally and when deployed to Azure App Service.Try and create a configuration access layer in the main project. That layer will serve as the single point of truth for accessing configuration settings. That way, you would make sure all parts of your application, including class libraries, retrieve configuration values through a consistent interface.
You can then use dependency injection (DI) (more flexible and testable approach than a a static access pattern) to provide configuration values to your class library projects.
Access the
idsetting via theConfigurationHelperfrom the main project:Make sure the class library project references the main project. If using dependency injection, inject the configuration settings as needed. That (DI) means the class library should not directly reference the main project. Instead, it should define an interface that the main project implements and registers with DI.
As an example, in your class library project, define an interface that represents the configuration access mechanism:
Implement this interface in the main project, where you have access to
ConfigurationManageror any other configuration source you are using:In your main project's startup class or wherever you configure services, register the
ConfigurationServicewith the DI container:In your class library, where you need to access the configuration setting, inject the
IConfigurationService:Make sure when you instantiate classes in your class library that require
IConfigurationService, you do so through mechanisms that respect DI (e.g., using constructor injection as shown above).If you are using ASP.NET Core or .NET 5/6+, the configuration system is a bit different, and you might inject
IConfigurationdirectly into your service implementation:The registration of services and the use of
IConfigurationServicein the class library remains the same.In Azure App Service, settings added in the Configuration section override the app's settings in web.config at runtime.
That means you do not need to duplicate the settings in web.config if they are specified in the Azure App Service Configuration.
When deployed to Azure App Service, make sure the
idsetting is added in the Configuration section. That setting will automatically override theweb.configsetting forid, without needing to specify it in theweb.configfile of the main project.