Application Environment of a .NET Dapr pluggable component

How to configure the environment of a .NET pluggable component

A .NET Dapr pluggable component application can be configured for dependency injection, logging, and configuration values similarly to ASP.NET applications. The DaprPluggableComponentsApplication exposes a similar set of configuration properties to that exposed by WebApplicationBuilder.

Dependency injection

Components registered with services can participate in dependency injection. Arguments in the components constructor will be injected during creation, assuming those types have been registered with the application. You can register them through the IServiceCollection exposed by DaprPluggableComponentsApplication.

  1. var app = DaprPluggableComponentsApplication.Create();
  2. // Register MyService as the singleton implementation of IService.
  3. app.Services.AddSingleton<IService, MyService>();
  4. app.RegisterService(
  5. "<service name>",
  6. serviceBuilder =>
  7. {
  8. serviceBuilder.RegisterStateStore<MyStateStore>();
  9. });
  10. app.Run();
  11. interface IService
  12. {
  13. // ...
  14. }
  15. class MyService : IService
  16. {
  17. // ...
  18. }
  19. class MyStateStore : IStateStore
  20. {
  21. // Inject IService on creation of the state store.
  22. public MyStateStore(IService service)
  23. {
  24. // ...
  25. }
  26. // ...
  27. }

Warning

Use of IServiceCollection.AddScoped() is not recommended. Such instances’ lifetimes are bound to a single gRPC method call, which does not match the lifetime of an individual component instance.

Logging

.NET Dapr pluggable components can use the standard .NET logging mechanisms. The DaprPluggableComponentsApplication exposes an ILoggingBuilder, through which it can be configured.

Note

Like with ASP.NET, logger services (for example, ILogger<T>) are pre-registered.

  1. var app = DaprPluggableComponentsApplication.Create();
  2. // Reset the default loggers and setup new ones.
  3. app.Logging.ClearProviders();
  4. app.Logging.AddConsole();
  5. app.RegisterService(
  6. "<service name>",
  7. serviceBuilder =>
  8. {
  9. serviceBuilder.RegisterStateStore<MyStateStore>();
  10. });
  11. app.Run();
  12. class MyStateStore : IStateStore
  13. {
  14. // Inject a logger on creation of the state store.
  15. public MyStateStore(ILogger<MyStateStore> logger)
  16. {
  17. // ...
  18. }
  19. // ...
  20. }

Configuration Values

Since .NET pluggable components are built on ASP.NET, they can use its standard configuration mechanisms and default to the same set of pre-registered providers. The DaprPluggableComponentsApplication exposes an IConfigurationManager through which it can be configured.

  1. var app = DaprPluggableComponentsApplication.Create();
  2. // Reset the default configuration providers and add new ones.
  3. ((IConfigurationBuilder)app.Configuration).Sources.Clear();
  4. app.Configuration.AddEnvironmentVariables();
  5. // Get configuration value on startup.
  6. const value = app.Configuration["<name>"];
  7. app.RegisterService(
  8. "<service name>",
  9. serviceBuilder =>
  10. {
  11. serviceBuilder.RegisterStateStore<MyStateStore>();
  12. });
  13. app.Run();
  14. class MyStateStore : IStateStore
  15. {
  16. // Inject the configuration on creation of the state store.
  17. public MyStateStore(IConfiguration configuration)
  18. {
  19. // ...
  20. }
  21. // ...
  22. }