绑定

创建一个绑定组件只需要几个基本步骤。

添加绑定命名空间

添加 using 语句来引用与绑定相关的命名空间。

  1. using Dapr.PluggableComponents.Components;
  2. using Dapr.PluggableComponents.Components.Bindings;

输入绑定: 实现IInputBinding

创建一个实现IInputBinding接口的类。

  1. internal sealed class MyBinding : IInputBinding
  2. {
  3. public Task InitAsync(MetadataRequest request, CancellationToken cancellationToken = default)
  4. {
  5. // Called to initialize the component with its configured metadata...
  6. }
  7. public async Task ReadAsync(MessageDeliveryHandler<InputBindingReadRequest, InputBindingReadResponse> deliveryHandler, CancellationToken cancellationToken = default)
  8. {
  9. // Until canceled, check the underlying store for messages and deliver them to the Dapr runtime...
  10. }
  11. }

ReadAsync() 方法的调用是“长时间运行”的,即该方法不会在取消之前返回(例如,通过 cancellationToken)。 当消息从组件的底层存储中被读取时,它们通过deliveryHandler回调函数传递给Dapr运行时。 Delivery 允许组件在应用程序(由 Dapr 运行时提供服务)确认处理消息后,接收通知。

  1. public async Task ReadAsync(MessageDeliveryHandler<InputBindingReadRequest, InputBindingReadResponse> deliveryHandler, CancellationToken cancellationToken = default)
  2. {
  3. TimeSpan pollInterval = // Polling interval (e.g. from initalization metadata)...
  4. // Poll the underlying store until canceled...
  5. while (!cancellationToken.IsCancellationRequested)
  6. {
  7. var messages = // Poll underlying store for messages...
  8. foreach (var message in messages)
  9. {
  10. // Deliver the message to the Dapr runtime...
  11. await deliveryHandler(
  12. new InputBindingReadResponse
  13. {
  14. // Set the message content...
  15. },
  16. // Callback invoked when application acknowledges the message...
  17. async request =>
  18. {
  19. // Process response data or error message...
  20. })
  21. }
  22. // Wait for the next poll (or cancellation)...
  23. await Task.Delay(pollInterval, cancellationToken);
  24. }
  25. }

输出绑定: 实现IOutputBinding

创建一个实现IOutputBinding接口的类。

  1. internal sealed class MyBinding : IOutputBinding
  2. {
  3. public Task InitAsync(MetadataRequest request, CancellationToken cancellationToken = default)
  4. {
  5. // Called to initialize the component with its configured metadata...
  6. }
  7. public Task<OutputBindingInvokeResponse> InvokeAsync(OutputBindingInvokeRequest request, CancellationToken cancellationToken = default)
  8. {
  9. // Called to invoke a specific operation...
  10. }
  11. public Task<string[]> ListOperationsAsync(CancellationToken cancellationToken = default)
  12. {
  13. // Called to list the operations that can be invoked.
  14. }
  15. }

输入和输出绑定组件

一个组件可以通过实现两个接口,同时成为输入和输出绑定。

  1. internal sealed class MyBinding : IInputBinding, IOutputBinding
  2. {
  3. // IInputBinding Implementation...
  4. // IOutputBinding Implementation...
  5. }

注册绑定组件

在主程序文件中(例如,Program.cs),在应用程序服务中注册绑定组件。

  1. using Dapr.PluggableComponents;
  2. var app = DaprPluggableComponentsApplication.Create();
  3. app.RegisterService(
  4. "<socket name>",
  5. serviceBuilder =>
  6. {
  7. serviceBuilder.RegisterBinding<MyBinding>();
  8. });
  9. app.Run();

注意

实现IInputBindingIOutputBinding接口的组件将同时注册为输入和输出绑定。