0

In .NET Framework we have app.config file that gets created when a project is created.But in .NET Core there is no application configuration file when a consoleapp is created.What is the recommended config file for .NET Core applications.

And if netstandard library is referenced to the .NET Core/.NET Framework application if the netstandard library has read the configuration details from the config file,then what classes have to be used. And i think .NET Core supports multiple config files

subbaraoc
  • 1,123
  • 1
  • 8
  • 27
  • They tried hard to keep System.Configuration from infecting .NETCore and .NETStandard. [But had to give up](https://www.nuget.org/packages/System.Configuration.ConfigurationManager/). – Hans Passant Aug 09 '18 at 16:40
  • @HansPassant Thank you.But the problem is while shipping the netstandard library along with the product will pre-req downloading this nuget package and this nuget package doesn't come along with the sdk – subbaraoc Aug 09 '18 at 16:54
  • No idea what that means, it is entirely normal to have a dependency on Nuget packages in a library or .NET project. Not a problem. – Hans Passant Aug 09 '18 at 17:02
  • 1
    Won't the product include the bits to the referenced nuget package? – Derek Beattie Aug 09 '18 at 17:27

1 Answers1

2

In .NET Core, configuration settings can come from different sources:

  • File formats (INI, JSON, and XML).
  • Command-line arguments.
  • Environment variables.
  • In-memory .NET objects.
  • The unencrypted Secret Manager storage.
  • An encrypted user store, such as Azure Key Vault.
  • Custom providers (installed or created).

As you see you have numerous options and you can even use multiple of them simultaneously. Choose whatever source you like or the one(s) fitting your scenario the best.

In ASP.NET Core applications JSON files are used usually. (However, settings from command line and environment variables is picked up by default, as well). The naming convention of the JSON config file is appsettings.json (but you can use another name if you want). So we can say, appsettings.json is roughly the equivalent for App.config in .NET Core.

The code sample below shows how you can read settings from a JSON file:

var builder = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json");

var configuration = builder.Build();

(To compile this, you'll need to have the Microsoft.Extensions.Configuration.Json NuGet package installed.)

Thereafter you can access the configuration data by the dictionary-like interface of the configuration object.

However, there are more convenient ways to consume the configuration data. The recommended method, which is used in ASP.NET Core extensively, is the Options API.

You find all the details in the MSDN docs.

Adam Simon
  • 2,762
  • 16
  • 22
  • Thank you.But the problem is while shipping the netstandard library along with the product will pre-req downloading this nuget package and this nuget package doesn't come along with the sdk – subbaraoc Aug 09 '18 at 16:52
  • @subbaraoc I've read your question again and I think I answered what you asked. Btw, what do you mean by "shipping the netstandard library"? Netstandard is rather an interface, a set of APIs, which are available on all the platforms which implements it. So you can't ship Netstandard but can ship .NET Core, classic .NET Framework, Mono, etc. runtime (or SDK?) – Adam Simon Aug 09 '18 at 18:35
  • When I say netstandard library I meant I build my code targered to netstandard so that the library can be used to access my API,'s in a .netframework or .netcore applications. – subbaraoc Aug 09 '18 at 22:05
  • @subbaraoc Ok, got it. Libraries usually don't read configuration settings directly, they provide some interface (e.g. option classes) to get the relevant settings from the end user of the library. It should be up to the end user where and how exactly it stores those settings. I suggest you to study how this is implemented in the framework libraries. (See for example [this](https://github.com/aspnet/Caching/blob/master/src/Microsoft.Extensions.Caching.Memory/MemoryCacheServiceCollectionExtensions.cs)) – Adam Simon Aug 10 '18 at 00:34