Created
February 27, 2019 16:12
-
-
Save watfordgnf/7008f9fc1aef3fc2d4a284b63b6eec2a to your computer and use it in GitHub Desktop.
NATS from ASP.Net Core DI
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
using System; | |
using NATS.Client; | |
namespace Microsoft.Extensions.DependencyInjection | |
{ | |
/// <summary> | |
/// Provides extension methods for NATS registration with ASP.Net Core DI | |
/// </summary> | |
public static class NatsServiceCollectionExtensions | |
{ | |
/// <summary> | |
/// Adds a NATS connection singleton to the services collection. | |
/// </summary> | |
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param> | |
/// <param name="options">An optional <see cref="Action{T}" /> to configure the provided <see cref="Options" />.</param> | |
/// <returns>A reference to the <see cref="IServiceCollection" /> after the operation has completed.</returns> | |
public static IServiceCollection AddNats(this IServiceCollection services, Action<Options> options = null) | |
{ | |
var opts = ConnectionFactory.GetDefaultOptions(); | |
options?.Invoke(opts); | |
return services.AddSingleton<IConnection>(_ => new ConnectionFactory().CreateConnection(opts)); | |
} | |
/// <summary> | |
/// Adds a scoped NATS connection to the services collection. | |
/// </summary> | |
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param> | |
/// <param name="options">An optional <see cref="Action{T}" /> to configure the provided <see cref="Options" />.</param> | |
/// <returns>A reference to the <see cref="IServiceCollection" /> after the operation has completed.</returns> | |
public static IServiceCollection AddNatsScoped(this IServiceCollection services, Action<Options> options = null) | |
{ | |
return services.AddScoped<IConnection>(_ => | |
{ | |
var opts = ConnectionFactory.GetDefaultOptions(); | |
options?.Invoke(opts); | |
return new ConnectionFactory().CreateConnection(opts); | |
}); | |
} | |
/// <summary> | |
/// Adds a transient NATS connection to the services collection. | |
/// </summary> | |
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param> | |
/// <param name="options">An optional <see cref="Action{T}" /> to configure the provided <see cref="Options" />.</param> | |
/// <returns>A reference to the <see cref="IServiceCollection" /> after the operation has completed.</returns> | |
public static IServiceCollection AddNatsTransient(this IServiceCollection services, Action<Options> options = null) | |
{ | |
return services.AddTransient<IConnection>(_ => | |
{ | |
var opts = ConnectionFactory.GetDefaultOptions(); | |
options?.Invoke(opts); | |
return new ConnectionFactory().CreateConnection(opts); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment