The documentation you are viewing is for Dapr v1.13 which is an older version of Dapr. For up-to-date documentation, see the latest version.
This is documentation on a preview feature.
绑定
创建一个绑定组件只需要几个基本步骤。
添加绑定命名空间
添加 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()
方法的调用是“长时间运行”的,即该方法不会在取消之前返回(例如,通过 cancellationToken
)。 当消息从组件的底层存储中被读取时,它们通过deliveryHandler
回调函数传递给Dapr运行时。 Delivery 允许组件在应用程序(由 Dapr 运行时提供服务)确认处理消息后,接收通知。
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
接口的组件将同时注册为输入和输出绑定。
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.