从不同的项目访问appsettings.json



我正在使用.Net Core 2.1来创建Web API。我的解决方案包含不同的项目,并且在主/启动项目中有一个appsettings.json文件。 我想从不同的项目中读取应用程序设置。为此,我在引用所有其他项目的 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"];
}
}
}

然后我尝试在启动项目的启动类中配置它-

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定义了两个构造函数,当前您在另一个项目中调用了错误的构造函数。您应该修改它,以便只有一个构造函数:

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);
}
}