Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Queues/RabbitMq/src/IQueueClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public interface IQueueClient : IAsyncDisposable
{
/// <summary>
/// Connects to the Queue.
/// This is not required as the client will auto connect, but is useful for forcing a connection.
/// This is not required as the client will auto-connect, but is useful for forcing a connection.
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Expand Down
16 changes: 16 additions & 0 deletions src/Queues/RabbitMq/src/PostRabbitMqOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace ClickView.GoodStuff.Queues.RabbitMq;

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

internal class PostRabbitMqOptions(IServiceProvider serviceProvider) : IPostConfigureOptions<RabbitMqClientOptions>
{
public void PostConfigure(string? name, RabbitMqClientOptions options)
{
if (options.LoggerFactory is null)
{
if (serviceProvider.GetService(typeof(ILoggerFactory)) is ILoggerFactory loggerFactory)
options.LoggerFactory = loggerFactory;
}
}
}
3 changes: 1 addition & 2 deletions src/Queues/RabbitMq/src/RabbitMqClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,7 @@ private async Task<IChannel> GetChannelAsync(bool enablePublisherConfirms,

private void CheckDisposed()
{
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
ObjectDisposedException.ThrowIf(_disposed, this);
}

private static ConnectionFactory CreateConnectionFactory(RabbitMqClientOptions options)
Expand Down
39 changes: 39 additions & 0 deletions src/Queues/RabbitMq/src/RabbitMqClientBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace ClickView.GoodStuff.Queues.RabbitMq;

using System.Text.Json;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Serialization;

public class RabbitMqClientBuilder(IServiceCollection services, string name = "")
{
public IServiceCollection Services { get; } = services;

public RabbitMqClientBuilder UseNewtonsoftJsonMessageSerializer(Action<JsonSerializerSettings>? configure = null)
{
Services.Configure<RabbitMqClientOptions>(name, o =>
{
var settings = NewtonsoftJsonMessageSerializer.GetDefaultSettings();

configure?.Invoke(settings);

o.Serializer = new NewtonsoftJsonMessageSerializer(settings);
});

return this;
}

public RabbitMqClientBuilder UseSystemTextJsonSerializer(Action<JsonSerializerOptions>? configure = null)
{
Services.Configure<RabbitMqClientOptions>(name, o =>
{
var options = SystemTextJsonMessageSerializer.GetDefaultOptions();

configure?.Invoke(options);

o.Serializer = new SystemTextJsonMessageSerializer(options);
});

return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public NewtonsoftJsonMessageSerializer(JsonSerializerSettings settings)
_settings = settings;
}

private static JsonSerializerSettings GetDefaultSettings()
public static JsonSerializerSettings GetDefaultSettings()
{
return new JsonSerializerSettings();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public SystemTextJsonMessageSerializer(JsonSerializerOptions options)
_options = options;
}

private static JsonSerializerOptions GetDefaultOptions()
public static JsonSerializerOptions GetDefaultOptions()
{
return new JsonSerializerOptions
{
Expand Down
37 changes: 37 additions & 0 deletions src/Queues/RabbitMq/src/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace ClickView.GoodStuff.Queues.RabbitMq;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

public static class ServiceCollectionExtensions
{
extension(IServiceCollection services)
{
public RabbitMqClientBuilder AddKeyedRabbitMq(string name, Action<RabbitMqClientOptions> configure)
{
ArgumentNullException.ThrowIfNull(services);

services.Configure(name, configure);
services.AddSingleton<IPostConfigureOptions<RabbitMqClientOptions>, PostRabbitMqOptions>();
services.AddKeyedSingleton<IQueueClient>(name, (provider, o) =>
{
var optionsName = (string) o!;
var options = provider.GetRequiredService<IOptionsMonitor<RabbitMqClientOptions>>().Get(optionsName);
return new RabbitMqClient(options);
});

return new RabbitMqClientBuilder(services, name);
}

public RabbitMqClientBuilder AddRabbitMq(Action<RabbitMqClientOptions> configure)
{
ArgumentNullException.ThrowIfNull(services);

services.Configure(configure);
services.AddSingleton<IPostConfigureOptions<RabbitMqClientOptions>, PostRabbitMqOptions>();
services.AddSingleton<IQueueClient, RabbitMqClient>();

return new RabbitMqClientBuilder(services);
}
}
}
Loading