gpt4 book ai didi

asp.net-core - .Net Core 如何在应用程序中的任何位置访问配置

转载 作者:行者123 更新时间:2023-12-03 18:40:58 25 4
gpt4 key购买 nike

我已经阅读了有关在 .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");
});
}
}

然后我想在我的代码中的任何地方访问任何类中的配置。因此,例如,我有以下类(class),我只想阅读配置信息:
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 类的实例时,它要求我传入配置对象,但这并没有违背注入(inject)它的目的。我应该如何设置它以类似于 DBContext 如何获取注入(inject)配置选项的上下文。 services.AddTransient 和 services.AddScoped 之间有什么区别?我将两者都视为注册服务的一种方式。

最佳答案

我会说,在 .Net Core 应用程序中,您不应该传递 IConfiguration 的实例到您的 Controller 或其他类。您应该使用通过 IOtions<T> 注入(inject)的强类型设置。反而。将其应用于您的案例,修改 MyConfig 类(属性名称也应与配置中的名称匹配,因此您必须重命名配置(DefaultConnection->DefaultConnStr、AW2012ConnStr->AWConnStr 或属性,反之亦然):

public class MyConfig
{
public string AWConnStr { get; set; }
public string DefaultConnStr { get; set; }
}

注册它:
public void ConfigureServices(IServiceCollection services)
{
// in case config properties specified at root level of config file
// services.Configure<MyConfig>(Configuration);

// in case there are in some section (seems to be your case)
var section = Configuration.GetSection("ConnectionStrings");
services.Configure<MyConfig>(section);
}

将其注入(inject)所需的服务:
public class MyService
{
private readonly MyConfig _settings;

public MyService(IOptions<MyConfig> options)
{
_settings = options.Value;
}
}

And what's the difference between services.AddTransient and services.AddScoped? I've seen both as a way to register the service.



transient 每次请求时都会创建终身服务。

范围 每个请求创建一次生命周期服务。

关于asp.net-core - .Net Core 如何在应用程序中的任何位置访问配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52356449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com