본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Lifetimes of .NET Dapr pluggable components

원문 보기 위키 갱신

This is documentation on a preview feature.

How to control the lifetime of a .NET pluggable component There are two ways to register a component:

  • The component operates as a singleton, with lifetime managed by the SDK
  • A component’s lifetime is determined by the pluggable component and can be multi-instance or a singleton, as needed

출처: Dapr 문서

본문

Singleton components

Components registered by type are singletons: one instance will serve all configured components of that type associated with that socket. This approach is best when only a single component of that type exists and is shared amongst Dapr applications.

var app = DaprPluggableComponentsApplication.Create();

app.RegisterService(
"service-a",
serviceBuilder =>
{
serviceBuilder.RegisterStateStore<SingletonStateStore>();
});

app.Run();

class SingletonStateStore : IStateStore
{
// ...
}

Multi-instance components

Components can be registered by passing a “factory method”. This method will be called for each configured component of that type associated with that socket. The method returns the instance to associate with that component (whether shared or not). This approach is best when multiple components of the same type may be configured with different sets of metadata, when component operations need to be isolated from one another, etc. The factory method will be passed context, such as the ID of the configured Dapr component, that can be used to differentiate component instances.

var app = DaprPluggableComponentsApplication.Create();

app.RegisterService(
"service-a",
serviceBuilder =>
{
serviceBuilder.RegisterStateStore(
context =>
{
return new MultiStateStore(context.InstanceId);
});
});

app.Run();

class MultiStateStore : IStateStore
{
private readonly string instanceId;

public MultiStateStore(string instanceId)
{
this.instanceId = instanceId;
}

// ...
}

Next steps

  • Learn more about the application environment
  • Learn more about multiple services
  • Learn more about using the Pluggable Component .NET SDK for:Bindings
  • Pub/sub
  • State store

더 알아보기 (Learn more)