I want to my use the same .net standard library for .NET Core and .NET Framework applications. Different type of config files are supported for .NET Core. If i have to read the config files irrespective of if it is web.config/app.config/appsetting.json using .net standard library what is the class that i have to use.
See Configuration in ASP.NET Core | Microsoft Docs.
While labelled with ASP.NET Core, the implementation is based on .NET Standard, so you can follow the instructions there. HTH.
@gfoidl Thank you
@ramsubbaraoc when your issue is hereby resolved, please close this issue.
@gfoidl I'm not sure but I couldn't find anything about how to use/access/etc Microsoft.Extensions.Configuration in an library targeting netstandard2.0. The link provided seems to be only for ASP.NET Core and all the examples assume there is a WebHost (or something like that). However, a library tends not to have a WebHostBuilder and it doesn't look right to me to use new WebHostBuilder() in a library....
Do you happen to have a link for how to use Microsoft.Extensions.Configuration in a library targeting netstandard2.0? Thank you!
Update: Somewhat dated but this link may be helpful in case someone else needs to set up configuration in a libary, e.g. an assembly containing tests in which case you don't write code for a web app or a console application: https://msdn.microsoft.com/en-us/magazine/mt632279.aspx
In the above link search for ConfigurationBuilder in the code snippets, and use that. The part with WebHostBuilder can be omitted then.
So for example:
c#
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("config.json", optional: true, reloadOnChange: true)
.Build();
Now you're able to use config as your configuration.
Include Microsoft.Extensions.Configuration.Json instead of Microsoft.NETCore.App. Then you can get the configurationsettings like:
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
--- End of inner exception stack trace ---
at System.Text.Json.JsonDocument.Parse(ReadOnlyMemory1 json, JsonDocumentOptions options)
at System.Text.Json.JsonDocument.Parse(String json, JsonDocumentOptions options)
at Microsoft.Extensions.Configuration.Json.JsonConfigurationFileParser.ParseStream(Stream input)
at Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider.Load(Stream stream)
at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.Extensions.Configuration.FileConfigurationProvider.HandleException(ExceptionDispatchInfo info)
at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load()
at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList1 providers)
at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
at Program.Main()
? for it
Most helpful comment
In the above link search for
ConfigurationBuilderin the code snippets, and use that. The part withWebHostBuildercan be omitted then.So for example:
c# var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("config.json", optional: true, reloadOnChange: true) .Build();Now you're able to use
configas your configuration.