|
1 |
| -// ------------------------------------------------------------------------ |
| 1 | +// ------------------------------------------------------------------------ |
2 | 2 | // Copyright 2021 The Dapr Authors
|
3 | 3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
4 | 4 | // you may not use this file except in compliance with the License.
|
|
11 | 11 | // limitations under the License.
|
12 | 12 | // ------------------------------------------------------------------------
|
13 | 13 |
|
14 |
| -namespace Microsoft.Extensions.DependencyInjection |
15 |
| -{ |
16 |
| - using System; |
17 |
| - using System.Linq; |
18 |
| - using Dapr.Client; |
19 |
| - using Extensions; |
| 14 | +#nullable enable |
| 15 | + |
| 16 | +namespace Microsoft.Extensions.DependencyInjection; |
| 17 | + |
| 18 | +using System; |
| 19 | +using Dapr; |
| 20 | +using Dapr.Client; |
| 21 | +using Extensions; |
| 22 | +using Configuration; |
20 | 23 |
|
| 24 | +/// <summary> |
| 25 | +/// Provides extension methods for <see cref="IServiceCollection" />. |
| 26 | +/// </summary> |
| 27 | +public static class DaprServiceCollectionExtensions |
| 28 | +{ |
21 | 29 | /// <summary>
|
22 |
| - /// Provides extension methods for <see cref="IServiceCollection" />. |
| 30 | + /// Adds Dapr client services to the provided <see cref="IServiceCollection" />. This does not include integration |
| 31 | + /// with ASP.NET Core MVC. Use the <c>AddDapr()</c> extension method on <c>IMvcBuilder</c> to register MVC integration. |
23 | 32 | /// </summary>
|
24 |
| - public static class DaprServiceCollectionExtensions |
| 33 | + /// <param name="services">The <see cref="IServiceCollection" />.</param> |
| 34 | + /// <param name="configure"></param> |
| 35 | + public static void AddDaprClient(this IServiceCollection services, Action<DaprClientBuilder>? configure = null) |
25 | 36 | {
|
26 |
| - /// <summary> |
27 |
| - /// Adds Dapr client services to the provided <see cref="IServiceCollection" />. This does not include integration |
28 |
| - /// with ASP.NET Core MVC. Use the <c>AddDapr()</c> extension method on <c>IMvcBuilder</c> to register MVC integration. |
29 |
| - /// </summary> |
30 |
| - /// <param name="services">The <see cref="IServiceCollection" />.</param> |
31 |
| - /// <param name="configure"></param> |
32 |
| - public static void AddDaprClient(this IServiceCollection services, Action<DaprClientBuilder> configure = null) |
| 37 | + ArgumentNullException.ThrowIfNull(services, nameof(services)); |
| 38 | + |
| 39 | + services.TryAddSingleton(serviceProvider => |
33 | 40 | {
|
34 |
| - ArgumentNullException.ThrowIfNull(services, nameof(services)); |
| 41 | + var builder = CreateDaprClientBuilder(serviceProvider); |
| 42 | + configure?.Invoke(builder); |
| 43 | + return builder.Build(); |
| 44 | + }); |
| 45 | + } |
35 | 46 |
|
36 |
| - services.TryAddSingleton(_ => |
37 |
| - { |
38 |
| - var builder = new DaprClientBuilder(); |
39 |
| - configure?.Invoke(builder); |
| 47 | + /// <summary> |
| 48 | + /// Adds Dapr client services to the provided <see cref="IServiceCollection"/>. This does not include integration |
| 49 | + /// with ASP.NET Core MVC. Use the <c>AddDapr()</c> extension method on <c>IMvcBuilder</c> to register MVC integration. |
| 50 | + /// </summary> |
| 51 | + /// <param name="services">The <see cref="IServiceCollection"/>.</param> |
| 52 | + /// <param name="configure"></param> |
| 53 | + public static void AddDaprClient(this IServiceCollection services, |
| 54 | + Action<IServiceProvider, DaprClientBuilder> configure) |
| 55 | + { |
| 56 | + ArgumentNullException.ThrowIfNull(services, nameof(services)); |
40 | 57 |
|
41 |
| - return builder.Build(); |
42 |
| - }); |
43 |
| - } |
44 |
| - |
45 |
| - /// <summary> |
46 |
| - /// Adds Dapr client services to the provided <see cref="IServiceCollection"/>. This does not include integration |
47 |
| - /// with ASP.NET Core MVC. Use the <c>AddDapr()</c> extension method on <c>IMvcBuilder</c> to register MVC integration. |
48 |
| - /// </summary> |
49 |
| - /// <param name="services">The <see cref="IServiceCollection"/>.</param> |
50 |
| - /// <param name="configure"></param> |
51 |
| - public static void AddDaprClient(this IServiceCollection services, |
52 |
| - Action<IServiceProvider, DaprClientBuilder> configure) |
| 58 | + services.TryAddSingleton(serviceProvider => |
53 | 59 | {
|
54 |
| - ArgumentNullException.ThrowIfNull(services, nameof(services)); |
| 60 | + var builder = CreateDaprClientBuilder(serviceProvider); |
| 61 | + configure?.Invoke(serviceProvider, builder); |
| 62 | + return builder.Build(); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + private static DaprClientBuilder CreateDaprClientBuilder(IServiceProvider serviceProvider) |
| 67 | + { |
| 68 | + var builder = new DaprClientBuilder(); |
| 69 | + var configuration = serviceProvider.GetService<IConfiguration>(); |
| 70 | + |
| 71 | + // Set the HTTP endpoint, if provided, else use the default endpoint |
| 72 | + builder.UseHttpEndpoint(DaprDefaults.GetDefaultHttpEndpoint(configuration)); |
55 | 73 |
|
56 |
| - services.TryAddSingleton(serviceProvider => |
57 |
| - { |
58 |
| - var builder = new DaprClientBuilder(); |
59 |
| - configure?.Invoke(serviceProvider, builder); |
| 74 | + // Set the gRPC endpoint, if provided |
| 75 | + builder.UseGrpcEndpoint(DaprDefaults.GetDefaultGrpcEndpoint(configuration)); |
60 | 76 |
|
61 |
| - return builder.Build(); |
62 |
| - }); |
| 77 | + // Set the API token, if provided |
| 78 | + var apiToken = DaprDefaults.GetDefaultDaprApiToken(configuration); |
| 79 | + if (!string.IsNullOrWhiteSpace(apiToken)) |
| 80 | + { |
| 81 | + builder.UseDaprApiToken(apiToken); |
63 | 82 | }
|
| 83 | + |
| 84 | + return builder; |
64 | 85 | }
|
65 | 86 | }
|
0 commit comments