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
{
// 在创建 state 存储时注入 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
{
// 在创建 state 存储时注入日志记录器。
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
{
// 在创建 state 存储时注入配置。
public MyStateStore(IConfiguration configuration)
{
// ...
}
// ...
}
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.