Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ConfigurationManager in .NET Core

I have a shared .NET Standard library that was originally being referenced by a .NET 4.8 MVC 4 project. There is a lot of code in this shared library that uses the ConfigurationManager, like:

var sze = ConfigurationManager.AppSettings["MaxAttachmentSize"];

This value for MaxAttachmentSize (and others) was being stored in the web.config.

Now I have a .NET 6 project that I'm building, which will use this same shared project, and I need to find a way to make these configuration app settings work in the new .NET Core application. What's the best way to do this?

My questions I guess are:

  • Is there a way to get ConfigurationManager to read from the .NET Core's appsettings.json file like it reads from web.config in the ASP.NET MVC 4 project?
  • If not, is there another configuration reader that I can use instead that will do double duty being able to work for both projects like I need?

The big spanner in the works though is the fact that all calls to ConfigurationManger right now are static, so if the .NET Core option could be static as well that would be incredibly helpful. If not, it'll just be more work moving the ASP.NET MVC 4 project to make those settings dependency injection available.

like image 569
Phil Avatar asked Feb 21 '26 07:02

Phil


2 Answers

now you can get it even more easy

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

string sze = Configuration.GetSection("AppSettings:MaxAttachmentSize").Value;

after adding to appsettings.json

  "AppSettings": {
    "MaxAttachmentSize": "size1",
    "MinAttachmentSize": "size2"
  }
like image 181
Serge Avatar answered Feb 23 '26 20:02

Serge


Using ASP .NET6

Install NuGet package System.Configuration.ConfigurationManager

The name of my configuration file is App.Config

Content of App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <appSettings>
        <add key="DBConnectionString" value="aa" />
        <add key="DBName" value="bb" />
        <add key="AuthKey" value="cc" />
    </appSettings>
</configuration>

File reading values, i.e. SettingsService.cs:

using namespc = System.Configuration;

namespace Ecommerce_server.Services.SetupOperation
{
    public interface ISettingsService
    {
        string MongoDBConnectionString { get => namespc.ConfigurationManager.AppSettings["DBConnectionString"]!; }

        string DataBaseName { get => namespc.ConfigurationManager.AppSettings["DBName"]!; }

        public string AuthKey { get => namespc.ConfigurationManager.AppSettings["AuthKey"]!; }

    }

    public class SettingsService : ISettingsService
    {
        public string MongoDBConnectionString { get => namespc.ConfigurationManager.AppSettings["DBConnectionString"]!; }

        public string DataBaseName { get => namespc.ConfigurationManager.AppSettings["DBName"]!; }

        public string AuthKey { get => namespc.ConfigurationManager.AppSettings["AuthKey"]!; }

    }
}

Note: We had to create a specialized namespace for ConfigurationManger to solve ambiguity.

like image 27
Franco Avatar answered Feb 23 '26 19:02

Franco