-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added registration extension methods
Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
- Loading branch information
Showing
1 changed file
with
38 additions
and
55 deletions.
There are no files selected for viewing
93 changes: 38 additions & 55 deletions
93
...Dapr.Messaging/PublishSubscribe/Extensions/PublishSubscribeServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,38 @@ | ||
// using Microsoft.Extensions.DependencyInjection; | ||
// | ||
// namespace System.Runtime.CompilerServices.PublishSubscribe.Extensions; | ||
// | ||
// /// <summary> | ||
// /// Contains extension methods for using Dapr Publish/Subscribe with dependency injection. | ||
// /// </summary> | ||
// public static class PublishSubscribeServiceCollectionExtensions | ||
// { | ||
// /// <summary> | ||
// /// Adds Dapr Publish/Subscribe support to the service collection. | ||
// /// </summary> | ||
// /// <param name="services">The <see cref="IServiceCollection"/>.</param> | ||
// /// <returns></returns> | ||
// public static IServiceCollection AddDaprPubSubClient(this IServiceCollection services) => | ||
// AddDaprPubSubClient(services, (_, _) => { }); | ||
// | ||
// /// <summary> | ||
// /// Adds Dapr Publish/Subscribe support to the service collection. | ||
// /// </summary> | ||
// /// <param name="services">The <see cref="IServiceCollection"/>.</param> | ||
// /// <param name="configure">Optionally allows greater configuration of the <see cref="DaprPublishSubscribeClientBuilder"/>.</param> | ||
// /// <returns></returns> | ||
// public static IServiceCollection AddDaprPubSubClient(this IServiceCollection services, | ||
// Action<DaprPublishSubscribeClientBuilder>? configure) => | ||
// services.AddDaprPubSubClient((_, builder) => configure?.Invoke(builder)); | ||
// | ||
// /// <summary> | ||
// /// Adds Dapr Publish/Subscribe support to the service collection. | ||
// /// </summary> | ||
// /// <param name="services">The <see cref="IServiceCollection"/>.</param> | ||
// /// <param name="configure">Optionally allows greater configuration of the <see cref="DaprPublishSubscribeClient"/> using injected services.</param> | ||
// /// <returns></returns> | ||
// public static IServiceCollection AddDaprPubSubClient(this IServiceCollection services, Action<IServiceProvider, DaprPublishSubscribeClientBuilder>? configure) | ||
// { | ||
// ArgumentNullException.ThrowIfNull(services, nameof(services)); | ||
// | ||
// //Register the IHttpClientFactory implementation | ||
// services.AddHttpClient(); | ||
// | ||
// services.TryAddSingleton(serviceProvider => | ||
// { | ||
// var httpClientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>(); | ||
// | ||
// var builder = new DaprPublishSubscribeClientBuilder(); | ||
// builder.UseHttpClientFactory(httpClientFactory); | ||
// | ||
// configure?.Invoke(serviceProvider, builder); | ||
// | ||
// return builder.Build(); | ||
// }); | ||
// | ||
// return services; | ||
// } | ||
// } | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
||
namespace Dapr.Messaging.PublishSubscribe.Extensions; | ||
|
||
/// <summary> | ||
/// Contains extension methods for using Dapr Publish/Subscribe with dependency injection. | ||
/// </summary> | ||
public static class PublishSubscribeServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Adds Dapr Publish/Subscribe support to the service collection. | ||
/// </summary> | ||
/// <param name="services">The <see cref="IServiceCollection"/>.</param> | ||
/// <param name="configure">Optionally allows greater configuration of the <see cref="DaprPublishSubscribeClient"/> using injected services.</param> | ||
/// <returns></returns> | ||
public static IServiceCollection AddDaprPubSubClient(this IServiceCollection services, Action<IServiceProvider, DaprPublishSubscribeClientBuilder>? configure = null) | ||
{ | ||
ArgumentNullException.ThrowIfNull(services, nameof(services)); | ||
|
||
//Register the IHttpClientFactory implementation | ||
services.AddHttpClient(); | ||
|
||
services.TryAddSingleton(serviceProvider => | ||
{ | ||
var httpClientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>(); | ||
var builder = new DaprPublishSubscribeClientBuilder(); | ||
builder.UseHttpClientFactory(httpClientFactory); | ||
configure?.Invoke(serviceProvider, builder); | ||
return builder.Build(); | ||
}); | ||
|
||
return services; | ||
} | ||
} |