-
Notifications
You must be signed in to change notification settings - Fork 516
Expand file tree
/
Copy pathEventBusModule.cs
More file actions
53 lines (49 loc) · 1.95 KB
/
EventBusModule.cs
File metadata and controls
53 lines (49 loc) · 1.95 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
50
51
52
53
namespace EvolutionaryArchitecture.Fitnet.Passes.Api.Common.EventBus;
using System.Reflection;
using MassTransit;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Outbox;
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.AddConsumers(Assembly.GetExecutingAssembly());
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);
});
configurator.ConfigureOutbox();
});
return services;
}
}