Do you have a problem on accessing appsetting . JSON file of the main project From Class library?
Below I have provided one of the procedure on Getting Appsetting value.
Let assume We create the project on .net core web application and
Added the Class Library. In my Case project structure looks like this.- Install the following Nuget
Packege in Class library
- Microsoft
. Extensions. Configuration - Microsoft
. Extensions. Configuration. Abstractions - Microsoft
. Extensions. Configuration. Json
Create a class AppConfiguration in class library . Put the Database connection string in appsetting . json in main project .
"ConnectionStrings ": {
"DataConnection": “Connection String”
} ,
Now we need to accessing the this connection string value from our AppConfiguration. cs in class library .
using Microsoft.Extensions.Configuration;
using System.IO;
namespace LogicLayers.AppConfig
{
public class AppConfiguration
{
public readonly string _connectionString = string.Empty;
public AppConfiguration()
{
var configurationBuilder = new ConfigurationBuilder();
var path = Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json");
configurationBuilder.AddJsonFile(path, false);
var root = configurationBuilder.Build();
_connectionString = root.GetSection("ConnectionString").GetSection("DataConnection").Value;
var appSetting = root.GetSection("ApplicationSettings");
}
public string ConnectionString
{
get => _connectionString;
}
}
}
Nguon : https://answers.unity.com/questions/238756/calling-a-parent-constructor-after-the-childs-in-c.html
Không có nhận xét nào:
Đăng nhận xét