C# 从不同的项目访问appsettings.json
我正在使用.NETCore2.1创建一个Web API。我的解决方案包含不同的项目,主/启动项目中有一个appsettings.json文件。 我想从不同的项目中读取appsettings。为此,我在一个公共项目中创建了一个类,该类引用了所有其他项目C# 从不同的项目访问appsettings.json,c#,asp.net-core,asp.net-core-2.0,asp.net-core-webapi,C#,Asp.net Core,Asp.net Core 2.0,Asp.net Core Webapi,我正在使用.NETCore2.1创建一个Web API。我的解决方案包含不同的项目,主/启动项目中有一个appsettings.json文件。 我想从不同的项目中读取appsettings。为此,我在一个公共项目中创建了一个类,该类引用了所有其他项目 namespace Common { public sealed class ApplicationSettings { public ApplicationSettings(IConfiguration confi
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项目的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 ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
IToolService _toolService = new ToolService();
_toolService.ReadToolDetail(1);
//...
return new string[] { "value1", "value2" };
}
}
最后使用它-
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 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 ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
IToolService _toolService = new ToolService();
_toolService.ReadToolDetail(1);
//...
return new string[] { "value1", "value2" };
}
}
公共类值控制器:控制器库
{
//获取api/值
[HttpGet]
公共行动结果获取()
{
IToolService_toolService=新工具服务();
_toolService.ReadToolDetail(1);
//...
返回新字符串[]{“value1”,“value2”};
}
}
当我试图通过将它们作为参数传递(使用依赖项注入,如上所示)在不同的项目中使用它们时,AppSettings
对象为空
我做错了吗?请告诉我是否可以添加更多详细信息。您已为
工具服务定义了两个构造函数,当前您在其他项目中调用了错误的构造函数。您应该修改它,以便只有一个构造函数:
public ToolService(Common.ApplicationSettings settings)
{
_dataContext = new DataContext();
_settings = settings; //settings is null here
}
如果希望类库使用依赖项注入,则需要在服务
集合中注册它,例如
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>();
}
什么类型的项目是不同的项目?控制台,ASP.NET核心?@SimplyGed类库项目谢谢。我已经编辑了我的帖子。基本上,我是在API控制器类中实例化ToolService
。我不能将ToolService
注入我相信的API控制器类?你能建议一个更好的方法吗?再次感谢。您当然可以将ToolService
注入API控制器:-)。只要您已经在ConfigureServices
方法中注册了ToolService
,您就可以将其注入到已注册到IServiceCollection
的任何其他类中(注:控制器由框架自动注册),因此每当我必须访问MyMethod()
I必须通过传递ToolService
reference来实例化MyClass
?如果您想手动创建类,即使用new MyClass(…)
,那么是的。稍后可能会变得复杂。是否有一种更简单的方法可以访问我所有项目中的应用程序设置?据我所知,作为一种实践,除了启动项目,其他项目都不应该直接访问appsettings.json。是这样吗?