【发布时间】:2019-02-20 17:04:58
【问题描述】:
我已阅读有关在 .Net Core 2.1 中设置和访问配置的不同方法的文档,以及似乎推荐的选项模式 (https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-2.1)。但是,我似乎无法得到我想要的工作:
我做了以下事情:
应用设置:
{
"ConnectionStrings": {
"DefaultConnStr": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;MultipleActiveResultSets=true;Integrated Security=true",
"AW2012ConnStr": "Server=localhost;Database=AW2012;Trusted_Connection=True;MultipleActiveResultSets=true;Integrated Security=true"
}
}
我的配置:
public class MyConfig
{
public string AWConnStr { get; }
public string DefaultConnStr { get; }
}
启动:
public class Startup
{
public IConfiguration _config { get; set; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
_config = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
//add config to services for dependency injection
//services.AddTransient<IMyConfig, MyConfig>();
//services.AddScoped<IMyConfig, MyConfig>();
var section = _config.GetSection("ConnectionStrings");
services.Configure<MyConfig>(section);
}
private static void HandleGetData(IApplicationBuilder app)
{
//DataHelper dataHelper = new DataHelper(_dataHelper);
var _dataHelper = app.ApplicationServices.GetService<DataHelper>();
app.Run(async context =>
{
//await context.Response.WriteAsync("<b>Get Data</b>");
//await context.Response.WriteAsync(dataHelper.GetCompetitions(context.Request.QueryString.ToString()));
await context.Response.WriteAsync(_dataHelper.GetCompetitions(context.Request.QueryString.ToString()));
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.Map("/Route1", HandleRoute1);
app.Map("/Route2", HandleRoute2);
app.Map("/GetData", HandleGetData);
app.Run(async (context) =>
{
await context.Response.WriteAsync("Non Mapped Default");
});
}
}
然后我想在我的代码中的任何位置访问任何类中的配置。因此,例如,我有以下课程,我只想阅读配置信息:
public interface IDataHelper
{
string GetCompetitions(string val);
}
public class DataHelper : IDataHelper
{
private readonly MyConfig _settings;
public DataHelper(IOptions<MyConfig> options)
{
_settings = options.Value;
}
public string GetCompetitions( string queryStringVals)
{
return _settings.AWConnStr;
}
}
如上面我的 Startup 类所示,然后我想在启动时访问/调用 HandleGetData 函数中的某些内容,以便当我浏览到以下路线时:http://localhost:xxxxx/getdata 我从 Something.GetData 函数中得到响应.
这是正确的吗?我遇到的问题是,当我创建Something 类的实例时,它要求我传入配置对象,但这并没有违背注入它的目的。我应该如何设置它以类似于 DBContext 如何获取注入配置选项的上下文。 services.AddTransient 和 services.AddScoped 之间有什么区别?我将两者都视为注册服务的一种方式。
【问题讨论】:
-
使用
services.Configure<MyConfig>(configuration.GetSection("ConnectionStrings"))而不是services.AddXXX<...>()。并在服务中,在构造函数参数中添加IOptions<MyConfig> options
标签: asp.net-core dependency-injection .net-core