gpt4 book ai didi

c# - 从不同项目访问 appsettings.json

转载 作者:行者123 更新时间:2023-11-30 20:25:41 24 4
gpt4 key购买 nike

我正在使用 .Net Core 2.1 创建 Web API。我的解决方案包含不同的项目,主/启动项目中有一个 appsettings.json 文件。我想读取来自不同项目的应用程序设置。为此,我在引用所有其他项目的公共(public)项目中创建了一个类。

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 Controller 中再次调用上述函数的方式-

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,当我试图通过将它们作为参数传递(使用依赖注入(inject),如上所示)在不同的项目中使用它们时。

我做错了吗?如果我可以添加更多详细信息,请告诉我。

最佳答案

您已经为 ToolService 定义了两个构造函数目前您在其他项目中调用了错误的构造函数。您应该修改它,以便只有一个构造函数:

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

如果你想让你的类库使用依赖注入(inject),那么你需要用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()如果您正在使用依赖注入(inject)。你需要让依赖注入(inject)器为你创建类。对于 ASP.NET Core,这是 IServiceProvider界面。

您没有显示在哪里创建 ToolService类(class)。如果它在 ConfigureServices 中注册的类中那么你可以通过 ToolService作为构造函数参数,依赖注入(inject)器将为您解析引用,包括 ApplicationSettings ToolService 的参数类(class)想要例如

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

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

关于c# - 从不同项目访问 appsettings.json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51137702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com