This is documentation on a preview feature.
.NET Dapr 可插拔组件的应用环境
如何配置 .NET 可插拔组件的环境
.NET Dapr 可插拔组件应用可以像 ASP.NET 应用一样配置依赖注入、日志和配置值。DaprPluggableComponentsApplication 暴露了一组与 WebApplicationBuilder 相似的配置属性。
依赖注入
注册到服务的组件可以参与依赖注入。在创建组件时,组件构造函数中的参数将被注入,前提是这些类型已在应用中注册。你可以通过 DaprPluggableComponentsApplication 暴露的 IServiceCollection 注册它们。
var app = DaprPluggableComponentsApplication.Create();
// 将 MyService 注册为 IService 的单例实现。
app.Services.AddSingleton<IService, MyService>();
app.RegisterService(
"<service name>",
serviceBuilder =>
{
serviceBuilder.RegisterStateStore<MyStateStore>();
});
app.Run();
interface IService
{
// ...
}
class MyService : IService
{
// ...
}
class MyStateStore : IStateStore
{
// 在创建状态存储时注入 IService。
public MyStateStore(IService service)
{
// ...
}
// ...
}
警告
不建议使用IServiceCollection.AddScoped()。此类实例的生命周期绑定到单个 gRPC 方法调用,这与单个组件实例的生命周期不匹配。日志
.NET Dapr 可插拔组件可以使用标准 .NET 日志机制。DaprPluggableComponentsApplication 暴露了一个 ILoggingBuilder,可以通过它进行配置。
注意
与 ASP.NET 一样,日志记录器服务(例如ILogger<T>)已预先注册。var app = DaprPluggableComponentsApplication.Create();
// 清除默认日志记录器并设置新的日志记录器。
app.Logging.ClearProviders();
app.Logging.AddConsole();
app.RegisterService(
"<service name>",
serviceBuilder =>
{
serviceBuilder.RegisterStateStore<MyStateStore>();
});
app.Run();
class MyStateStore : IStateStore
{
// 在创建状态存储时注入日志记录器。
public MyStateStore(ILogger<MyStateStore> logger)
{
// ...
}
// ...
}
配置值
由于 .NET 可插拔组件基于 ASP.NET 构建,它们可以使用其标准配置机制,并默认使用同一组预先注册的提供程序。DaprPluggableComponentsApplication 暴露了一个 IConfigurationManager,可以通过它进行配置。
var app = DaprPluggableComponentsApplication.Create();
// 清除默认配置提供程序并添加新的提供程序。
((IConfigurationBuilder)app.Configuration).Sources.Clear();
app.Configuration.AddEnvironmentVariables();
// 在启动时获取配置值。
const value = app.Configuration["<name>"];
app.RegisterService(
"<service name>",
serviceBuilder =>
{
serviceBuilder.RegisterStateStore<MyStateStore>();
});
app.Run();
class MyStateStore : IStateStore
{
// 在创建状态存储时注入配置。
public MyStateStore(IConfiguration configuration)
{
// ...
}
// ...
}
后续步骤
- 了解组件生命周期的更多信息
- 了解多服务的更多信息
- 了解如何使用可插拔组件 .NET SDK: