![](/img/trans.png)
[英]Read multiple connection strings from appsettings.json from different projects
[英]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.