簡體   English   中英

從不同的項目訪問appsettings.json

[英]Accessing appsettings.json from different projects

我正在使用.Net Core 2.1創建Web API。 我的解決方案包含不同的項目,並且在主/啟動項目中有一個appsettings.json文件。 我想閱讀來自不同項目的appsettings。 為此,我在Common項目中創建了一個引用其他項目的類。

namespace Common
{
    public sealed class ApplicationSettings
    {
        public ApplicationSettings(IConfiguration configuration)
        {
            InitSettings(configuration);
        }

        public string CloudStorageConnectionString { get; private set; }

        private void InitSettings(IConfiguration configuration)
        {
            CloudStorageConnectionString = configuration["CloudStorageAccountConnection"];
        }
    }
}

然后,我嘗試在啟動項目的Startup類中進行配置-

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

    //I can read the AppSettings values here via Configuration..

    var settings = new ApplicationSettings(Configuration);
    services.AddSingleton(settings);
}

最終使用它-

public class ToolService : IToolService
{
    private DataContext _dataContext = null;
    private readonly Common.ApplicationSettings _settings;
    public ToolService()
    {
        _dataContext = new DataContext();
    }

    public ToolService(Common.ApplicationSettings settings)
    {
        _settings = settings; //settings is null here
    }

    public ToolDetailModel ReadToolDetail(int toolDetailID)
    {
        ToolDetailModel toolDetailModel = null;
        try
        {
            var cloudConnection = _settings.CloudConnectionString; //settings is null here      
            //...
        }
        catch
        {

        }
        return toolDetailModel;
    }
}

這就是我在主要啟動項目中的API控制器中再次調用上述函數的方式-

public class ValuesController : ControllerBase
{
    // GET api/values
    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
        IToolService _toolService = new ToolService();
        _toolService.ReadToolDetail(1);
        //...
        return new string[] { "value1", "value2" };
    }
}

當我嘗試通過將它們作為參數傳遞給另一個項目(如上所示,使用依賴注入)時, AppSettings對象為null。

我做錯了嗎? 如果可以添加更多詳細信息,請告訴我。

您已經為ToolService定義了兩個構造ToolService ,當前您在其他項目中調用了錯誤的構造函數。 您應該對其進行修改,以便只有一個構造函數:

public ToolService(Common.ApplicationSettings settings)
{
    _dataContext = new DataContext();
    _settings = settings; //settings is null here
}

如果您希望您的類庫使用依賴注入,則需要在services集合中注冊它,例如

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

    //I can read the AppSettings values here via Configuration..

    var settings = new ApplicationSettings(Configuration);
    services.AddSingleton(settings);

    // use whatever lifetime you prefer
    services.AddScoped<ToolService>();
}

現在,當您嘗試調用new ToolService()它會抱怨說您需要提供ApplicationSettings參數。 問題是,如果您使用依賴項注入,則不能使用new ToolService() 您需要讓依賴注入器為您創建類。 對於ASP.NET Core,這是IServiceProvider接口。

您不會顯示要在哪里創建ToolService類。 如果它在ConfigureServices注冊的類中,則只需將ToolService作為構造函數參數傳遞,並且依賴關系注入程序將為您解析引用,包括ToolService類想要的ApplicationSettings參數,例如

public class MyClass
{
    public MyClass(ToolService toolService)
    {
        _toolService = toolService;
    }

    public void MyMethod()
    {
        _toolService.ReadToolDetail(1);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM