- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 .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/
我可以拥有吗?我的 app.config 中的部分包含许多设置,但也引用了 不同文件中的部分? 这将允许我保留只有开发人员应该感兴趣的配置选项,例如在主窗口上显示调试输出的选项(非常困惑但对我有用)或
我正在使用通过控制台应用程序创建的 app.config 文件,我可以使用 ConfigurationSettings.AppSettings["key1"].ToString()
我搜索了该站点,虽然我找到了一些非常有用的信息,但我无法弄清楚我的代码发生了什么。我有以下 web.config: 我
我正在使用 az functionapp config appsettings 自动创建我的 Azure Function 设置。我的设置之一是其中包含空格的 SQL 连接字符串。连接字符串中的空格导
我正在使用 az functionapp config appsettings 自动创建我的 Azure Function 设置。我的设置之一是其中包含空格的 SQL 连接字符串。连接字符串中的空格导
工具:VS2017、ASP.NET Core 2、Entity Framework Core 2、ASP.NET Core JavaScript 服务 我正在使用以下 BuildWebHost 方法:
我正在使用 ASP.NET Core 2.1。我在 appsettings.json 中有设置我使用选项模式将它们绑定(bind)到类。我想在 appsettings.Production.json
我在 appsettings.Development.json 上有点挣扎覆盖或以其他方式与 appsettings.json 合并.我不确定如何使用 appsettings.Development.
我真的很希望能够有一种方法来获取当前使用 ConfigurationManager.AppSettings [“mysettingkey”] 来获取其设置的应用程序,以使这些设置实际上来自中央数据库而
我有一些需要从多个文件访问的字符串常量。由于这些常量的值可能会不时更改,因此我决定将它们放在 AppSettings 而不是常量类中,这样我就不必在每次更改常量时都重新编译。 有时我需要处理单个字符串
我的 App.config 中有这样的代码片段 - 在 master_AppSettings.config 文件中,我将“ApplicationDSN”设置为“ABCConnectionStri
我有一个希望非常简单的问题。 如果我有值,我想从 Web.config 的 AppSettings 中找到某个键 为了简单起见,是否可以通过指定值而不是键来找到 AppSettings 条目
我正在使用 .NET Web API(4.6 框架)编写应用 我有一个我使用的属性:[ApiExplorerSettings(IgnoreApi = true)] 从我的 Swagger 中隐藏某些
在我的办公室,我们认为我们要将 Web.Config 的 AppSettings 放入数据库中。因此,我创建了以下内容,但对代码的几个方面有一些疑问。 所以我的问题是: UTILITY 类中包含“Ca
有人能帮我理解为什么在向配置文件添加值后,我不能立即将其读入应用程序吗?我做了刷新,但这不起作用。见下文: public void AddConfig(string key_value, st
我想从名为 MyAssembly.dll.config 的程序集配置文件中检索 AppSetting key 。这是配置文件的示例: 这是检索它的代码: va
我正在为 .NET 2.0 中的配置和设置类而苦苦挣扎 如果以下内容包含在名为 app.config 的文件中 我知道我可以通过以下方式访问 appSetting // this retur
我想从名为 secrets.config 的外部配置文件中读取控制台应用程序的 appSettings 的一部分,而其余部分我想从 app.config 中读取。 目前,我已经设置好了,但它似乎没有从
我有一个配置文件 app.exe.config 和 appSettings 部分有这样的东西: app.file.config 文件有这样的东西: 我需要在运行时
我目前正在从事 asp.net core (c#) Web API 项目。 对 Web API 的每次调用都将有一个唯一的 key ,以帮助识别进行调用的用户。 每个调用者都会有一组特定的配置值与之关
我是一名优秀的程序员,十分优秀!