diff --git a/docs/en/Dapr/Index.md b/docs/en/Dapr/Index.md index 5904b85508..060b31aac3 100644 --- a/docs/en/Dapr/Index.md +++ b/docs/en/Dapr/Index.md @@ -62,6 +62,27 @@ Alternatively, you can configure the options in the `Dapr` section of your `apps ### Injecting DaprClient +ABP registers the `DaprClient` class to the [dependency injection](../Dependency-Injection.md) system. So, you can inject and use it whenever you need: + +````csharp +public class MyService : ITransientDependency +{ + private readonly DaprClient _daprClient; + + public MyService(DaprClient daprClient) + { + _daprClient = daprClient; + } + + public async Task DoItAsync() + { + // TODO: Use the injected _daprClient object + } +} +```` + +Injecting `DaprClient` is the recommended way of using it in your application code. When you inject it, the `IAbpDaprClientFactory` service is used to create it, which is explained in the next section. + ### IAbpDaprClientFactory `IAbpDaprClientFactory` can be used to create `DaprClient` or `HttpClient` objects to perform operations on Dapr. It uses `AbpDaprOptions`, so you can configure the settings in a central place. @@ -249,18 +270,15 @@ In addition to ABP's standard distributed event bus system, you can also use Dap ````csharp public class MyService : ITransientDependency { - private readonly IAbpDaprClientFactory _daprClientFactory; + private readonly DaprClient _daprClient; - public MyService(IAbpDaprClientFactory daprClientFactory) + public MyService(DaprClient daprClient) { - _daprClientFactory = daprClientFactory; + _daprClient = daprClient; } public async Task DoItAsync() { - // Create a DaprClient object with default options - DaprClient _daprClient = await _daprClientFactory.CreateAsync(); - await _daprClient.PublishEventAsync( "pubsub", // pubsub name "StockChanged", // topic name diff --git a/docs/zh-Hans/Dapr/Index.md b/docs/zh-Hans/Dapr/Index.md index c4306f2c9f..64a9451737 100644 --- a/docs/zh-Hans/Dapr/Index.md +++ b/docs/zh-Hans/Dapr/Index.md @@ -62,6 +62,27 @@ Configure(options => ### 注入DaprClient +ABP 将 `DaprClient` 类注册到 [依赖注入](../Dependency-Injection.md) 系统中.因此,你可以在需要时注入并使用它: + +````csharp +public class MyService : ITransientDependency +{ + private readonly DaprClient _daprClient; + + public MyService(DaprClient daprClient) + { + _daprClient = daprClient; + } + + public async Task DoItAsync() + { + // TODO: Use the injected _daprClient object + } +} +```` + +注入 `DaprClient` 是在应用程序代码中使用它的推荐方法.当你注入它时,将使用 `IAbpDaprClientFactory` 服务创建它,这会在下一节中将进行说明. + ### IAbpDaprClientFactory `IAbpDaprClientFactory` 可用于创建 `DaprClient` 或 `HttpClient` 对象来执行对 Dapr 的操作.它使用 `AbpDaprOptions`,因此你可以配置设置. @@ -249,19 +270,16 @@ public class MyHandler : ````csharp public class MyService : ITransientDependency { - private readonly IAbpDaprClientFactory _daprClientFactory; + private readonly DaprClient _daprClient; - public MyService(IAbpDaprClientFactory daprClientFactory) + public MyService(DaprClient daprClient) { - _daprClientFactory = daprClientFactory; + _daprClient = daprClient; } public async Task DoItAsync() { - // Create a DaprClient object with default options - DaprClient daprClient = await _daprClientFactory.CreateAsync(); - - await daprClient.PublishEventAsync( + await _daprClient.PublishEventAsync( "pubsub", // pubsub name "StockChanged", // topic name new StockCountChangedEto // event data