在 .net 6 中,当服务初始化发生在值的配置绑定之前时,如何将参数传递给构造函数?
In .net 6, how do I pass the parameter to the constructor when the service initialization comes before the configuration binding of values?
我似乎无法找到我的确切问题的解决方案,因为我需要在调用构建器之前调用依赖注入,但这会导致控制器中的新对象实例化 class 和值丢失。
如果我在绑定后立即放置此行,我会收到一条错误消息,指出服务已构建后无法修改。
在旧版本的 .net 中,由于存在 Startup.cs,由于方法 ConfigureService 和 Configure 的分离,这似乎不是问题。
AuthenticationBind.cs
public class AuthenticationBind
{
public int AuthenticationId { get; set; }
public string AuthenticationName { get; set; }
}
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"TestAuthenticationBind": {
"AuthenticationId": "1324556666",
"AuthenticationName": "Test Authentication Name"
},
"AllowedHosts": "*"
}
Program.cs
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();
builder.Services.AddSingleton<AuthenticationBind>();
var app = builder.Build();
AuthenticationBind tb = new AuthenticationBind();
IConfiguration configuration = app.Configuration;
configuration.Bind("TestAuthenticationBind", tb);
AuthenticationController.cs
private readonly AuthenticationBind authenticationBind;
public AuthenticationController(AuthenticationBind authenticationBind)
{
this.authenticationBind = authenticationBind;
}
此外,我可以使用对象实例传递给 services.AddSingleton 方法,而不是 class 本身,如下所示吗?
builder.Services.AddSingleton<tb>();
您似乎正在尝试 bind configuration values into models。您可以通过调用 IServiceCollection.Configure<T>()
来完成此操作 - 对于您的代码,它看起来像这样:
builder.Services.Configure<AuthenticationBind>(builder.Configuration.GetSection("TestAuthenticationBind"));
之后,您可以使用控制器中的 IOptions<T>
接口访问(绑定)对象:
public AuthenticationController(
IOptions<AuthenticationBind> authOptions
)
{
// You can access authOptions.Value here
}
启动时也是如此class,你可以像这样请求IOptions
接口:
var authOptions = app.Services.GetRequiredService<IOptions<AuthenticationBind>>();
我似乎无法找到我的确切问题的解决方案,因为我需要在调用构建器之前调用依赖注入,但这会导致控制器中的新对象实例化 class 和值丢失。 如果我在绑定后立即放置此行,我会收到一条错误消息,指出服务已构建后无法修改。
在旧版本的 .net 中,由于存在 Startup.cs,由于方法 ConfigureService 和 Configure 的分离,这似乎不是问题。
AuthenticationBind.cs
public class AuthenticationBind
{
public int AuthenticationId { get; set; }
public string AuthenticationName { get; set; }
}
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"TestAuthenticationBind": {
"AuthenticationId": "1324556666",
"AuthenticationName": "Test Authentication Name"
},
"AllowedHosts": "*"
}
Program.cs
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();
builder.Services.AddSingleton<AuthenticationBind>();
var app = builder.Build();
AuthenticationBind tb = new AuthenticationBind();
IConfiguration configuration = app.Configuration;
configuration.Bind("TestAuthenticationBind", tb);
AuthenticationController.cs
private readonly AuthenticationBind authenticationBind;
public AuthenticationController(AuthenticationBind authenticationBind)
{
this.authenticationBind = authenticationBind;
}
此外,我可以使用对象实例传递给 services.AddSingleton 方法,而不是 class 本身,如下所示吗?
builder.Services.AddSingleton<tb>();
您似乎正在尝试 bind configuration values into models。您可以通过调用 IServiceCollection.Configure<T>()
来完成此操作 - 对于您的代码,它看起来像这样:
builder.Services.Configure<AuthenticationBind>(builder.Configuration.GetSection("TestAuthenticationBind"));
之后,您可以使用控制器中的 IOptions<T>
接口访问(绑定)对象:
public AuthenticationController(
IOptions<AuthenticationBind> authOptions
)
{
// You can access authOptions.Value here
}
启动时也是如此class,你可以像这样请求IOptions
接口:
var authOptions = app.Services.GetRequiredService<IOptions<AuthenticationBind>>();