Multiple services in a .NET Dapr pluggable component

How to expose multiple services from a .NET pluggable component

A pluggable component can host multiple components of varying types. You might do this:

  • To minimize the number of sidecars running in a cluster
  • To group related components that are likely to share libraries and implementation, such as:
    • A database exposed both as a general state store, and
    • Output bindings that allow more specific operations.

Each Unix Domain Socket can manage calls to one component of each type. To host multiple components of the same type, you can spread those types across multiple sockets. The SDK binds each socket to a “service”, with each service composed of one or more component types.

Registering multiple services

Each call to RegisterService() binds a socket to a set of registered components, where one of each type of component can be registered per service.

  1. var app = DaprPluggableComponentsApplication.Create();
  2. app.RegisterService(
  3. "service-a",
  4. serviceBuilder =>
  5. {
  6. serviceBuilder.RegisterStateStore<MyDatabaseStateStore>();
  7. serviceBuilder.RegisterBinding<MyDatabaseOutputBinding>();
  8. });
  9. app.RegisterService(
  10. "service-b",
  11. serviceBuilder =>
  12. {
  13. serviceBuilder.RegisterStateStore<AnotherStateStore>();
  14. });
  15. app.Run();
  16. class MyDatabaseStateStore : IStateStore
  17. {
  18. // ...
  19. }
  20. class MyDatabaseOutputBinding : IOutputBinding
  21. {
  22. // ...
  23. }
  24. class AnotherStateStore : IStateStore
  25. {
  26. // ...
  27. }

Configuring Multiple Components

Configuring Dapr to use the hosted components is the same as for any single component - the component YAML refers to the associated socket.

  1. #
  2. # This component uses the state store associated with socket `state-store-a`
  3. #
  4. apiVersion: dapr.io/v1alpha1
  5. kind: Component
  6. metadata:
  7. name: state-store-a
  8. spec:
  9. type: state.service-a
  10. version: v1
  11. metadata: []
  1. #
  2. # This component uses the state store associated with socket `state-store-b`
  3. #
  4. apiVersion: dapr.io/v1alpha1
  5. kind: Component
  6. metadata:
  7. name: state-store-b
  8. spec:
  9. type: state.service-b
  10. version: v1
  11. metadata: []