//code inside main project solution, this wo" /> //code inside main project solution, this wo" /> //code inside main project solution, this wo"/>

Set & use variable name in app service configuration that doesn't exist in web.config

164 Views Asked by At

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

1

There are 1 best solutions below

1
VonC On BEST ANSWER

You have:

[ Main Project (web.config) ] ---> [ Class Library Project ]
                  |
         Azure App Service Config

In the main project, accessing AppSettings works as expected:

var Id = ConfigurationManager.AppSettings["id"]; // Works in Main Project

However, the same code returns null in the class library project:

var Id = ConfigurationManager.AppSettings["id"]; // Returns NULL in Class Library

So you need to access the id setting in the class library without duplicating it in an app.config for 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.

public static class ConfigurationHelper
{
    public static string GetSetting(string key)
    {
        return ConfigurationManager.AppSettings[key];
    }
}

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 id setting via the ConfigurationHelper from the main project:

var Id = MainProject.ConfigurationHelper.GetSetting("id");

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:

// In your Class Library project
public interface IConfigurationService
{
    string GetSetting(string key);
}

Implement this interface in the main project, where you have access to ConfigurationManager or any other configuration source you are using:

// In your Main Project
public class ConfigurationService : IConfigurationService
{
    public string GetSetting(string key)
    {
        return ConfigurationManager.AppSettings[key]; // Or use IConfiguration for .NET Core/5+
    }
}

In your main project's startup class or wherever you configure services, register the ConfigurationService with the DI container:

// In your Startup.cs or Program.cs (for .NET Core/5+)
public void ConfigureServices(IServiceCollection services)
{
    // Other service registrations
    services.AddSingleton<IConfigurationService, ConfigurationService>();
}

In your class library, where you need to access the configuration setting, inject the IConfigurationService:

// In your Class Library project
public class SomeClass
{
    private readonly IConfigurationService _configService;

    public SomeClass(IConfigurationService configService)
    {
        _configService = configService;
    }

    public void DoSomething()
    {
        var id = _configService.GetSetting("id");
        // Use the id as needed
    }
}

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 IConfiguration directly into your service implementation:

public class ConfigurationService : IConfigurationService
{
    private readonly IConfiguration _configuration;

    public ConfigurationService(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public string GetSetting(string key)
    {
        return _configuration[key]; // Accessing configuration directly
    }
}

The registration of services and the use of IConfigurationService in 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 id setting is added in the Configuration section. That setting will automatically override the web.config setting for id, without needing to specify it in the web.config file of the main project.