-
Notifications
You must be signed in to change notification settings - Fork 514
Expand file tree
/
Copy pathEventBusModule.cs
More file actions
49 lines (45 loc) · 1.8 KB
/
EventBusModule.cs
File metadata and controls
49 lines (45 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
namespace EvolutionaryArchitecture.Fitnet.Contracts.Infrastructure.EventBus;
using MassTransit;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
internal static class EventBusModule
{
private const string EventBusConfiguration = "EventBus";
internal static IServiceCollection AddEventBus(this IServiceCollection services, IConfiguration configuration)
{
services.Configure<EventBusOptions>(options => configuration.GetSection(EventBusConfiguration).Bind(options));
services.AddMassTransit(configurator =>
{
configurator.SetSnakeCaseEndpointNameFormatter();
configurator.UsingRabbitMq((context, factoryConfigurator) =>
{
var options = context.GetRequiredService<IOptions<EventBusOptions>>();
var externalEventBusConfigured = options.Value is not null;
if (!externalEventBusConfigured)
{
return;
}
var uri = options.Value?.Uri;
var username = options.Value?.Username;
var password = options.Value?.Password;
if (!string.IsNullOrEmpty(uri))
{
factoryConfigurator.Host(uri, h =>
{
if (!string.IsNullOrEmpty(username))
{
h.Username(username);
}
if (!string.IsNullOrEmpty(password))
{
h.Password(password);
}
});
}
factoryConfigurator.ConfigureEndpoints(context);
});
});
return services;
}
}