Skip to content

Commit

Permalink
Added registration extension methods
Browse files Browse the repository at this point in the history
Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
  • Loading branch information
WhitWaldo committed Oct 30, 2024
1 parent 16374e3 commit 2a51b6e
Showing 1 changed file with 38 additions and 55 deletions.
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;
}
}

0 comments on commit 2a51b6e

Please sign in to comment.