바인딩
바인딩 (Bindings)
이 문서는 프리뷰 기능에 대한 문서예요.
바인딩 컴포넌트를 만드는 데는 몇 가지 기본 단계만 거치면 돼요.
출처: Dapr 문서
본문
바인딩 네임스페이스 추가하기
바인딩과 관련된 네임스페이스에 대한 using 문을 추가해요.
using Dapr.PluggableComponents.Components;
using Dapr.PluggableComponents.Components.Bindings;
입력 바인딩: IInputBinding 구현하기
IInputBinding 인터페이스를 구현하는 클래스를 만들어요.
internal sealed class MyBinding : IInputBinding
{
public Task InitAsync(MetadataRequest request, CancellationToken cancellationToken = default)
{
// Called to initialize the component with its configured metadata...
}
public async Task ReadAsync(MessageDeliveryHandler<InputBindingReadRequest, InputBindingReadResponse> deliveryHandler, CancellationToken cancellationToken = default)
{
// Until canceled, check the underlying store for messages and deliver them to the Dapr runtime...
}
}
ReadAsync() 메서드에 대한 호출은 "수명이 긴(long-lived)" 호출이에요. 즉, 메서드는 취소될 때까지(예를 들어 cancellationToken을 통해) 반환되지 않을 거예요. 컴포넌트의 기본 저장소에서 메시지를 읽으면 deliveryHandler 콜백을 통해 Dapr 런타임으로 전달돼요. 전달은 (Dapr 런타임이 제공하는) 애플리케이션이 메시지 처리를 확인(acknowledge)했을 때 컴포넌트가 알림을 받을 수 있게 해줘요.
public async Task ReadAsync(MessageDeliveryHandler<InputBindingReadRequest, InputBindingReadResponse> deliveryHandler, CancellationToken cancellationToken = default)
{
TimeSpan pollInterval = // Polling interval (e.g. from initalization metadata)...
// Poll the underlying store until canceled...
while (!cancellationToken.IsCancellationRequested)
{
var messages = // Poll underlying store for messages...
foreach (var message in messages)
{
// Deliver the message to the Dapr runtime...
await deliveryHandler(
new InputBindingReadResponse
{
// Set the message content...
},
// Callback invoked when application acknowledges the message...
async request =>
{
// Process response data or error message...
})
}
// Wait for the next poll (or cancellation)...
await Task.Delay(pollInterval, cancellationToken);
}
}
출력 바인딩: IOutputBinding 구현하기
IOutputBinding 인터페이스를 구현하는 클래스를 만들어요.
internal sealed class MyBinding : IOutputBinding
{
public Task InitAsync(MetadataRequest request, CancellationToken cancellationToken = default)
{
// Called to initialize the component with its configured metadata...
}
public Task<OutputBindingInvokeResponse> InvokeAsync(OutputBindingInvokeRequest request, CancellationToken cancellationToken = default)
{
// Called to invoke a specific operation...
}
public Task<string[]> ListOperationsAsync(CancellationToken cancellationToken = default)
{
// Called to list the operations that can be invoked.
}
}
입력 및 출력 바인딩 컴포넌트
컴포넌트는 두 인터페이스를 모두 구현하기만 하면 입력 바인딩이자 출력 바인딩이 될 수 있어요.
internal sealed class MyBinding : IInputBinding, IOutputBinding
{
// IInputBinding Implementation...
// IOutputBinding Implementation...
}
바인딩 컴포넌트 등록하기
메인 프로그램 파일(예: Program.cs)에서 애플리케이션 서비스에 바인딩 컴포넌트를 등록해요.
using Dapr.PluggableComponents;
var app = DaprPluggableComponentsApplication.Create();
app.RegisterService(
"<socket name>",
serviceBuilder =>
{
serviceBuilder.RegisterBinding<MyBinding>();
});
app.Run();
참고
IInputBinding과 IOutputBinding을 모두 구현하는 컴포넌트는 입력 바인딩이자 출력 바인딩으로 등록돼요.
다음 단계
- 플러그형 컴포넌트 .NET SDK의 고급 단계 알아보기
- 다음 용도로 플러그형 컴포넌트 .NET SDK를 사용하는 방법 알아보기:Pub/sub
- 상태 저장소