|
1 | | -using System; |
2 | | -using Microsoft.Extensions.DependencyInjection; |
3 | | -using Microsoft.Extensions.Configuration; |
4 | | -using MassTransit; |
5 | | -using MassTransit.RabbitMqTransport; |
6 | | -using Microsoft.Extensions.Options; |
7 | | -using System.Collections.Generic; |
8 | | -using System.Linq; |
9 | | - |
10 | | -namespace MassTransitAbstractions.Extensions |
11 | | -{ |
12 | | - |
13 | | - public interface IRabbitMQReceiveEndpointRegistration |
14 | | - { |
15 | | - void AddReceiveEndpoint(IRabbitMqBusFactoryConfigurator cfg, IRabbitMqHost host); |
16 | | - } |
17 | | - public interface IBusControlContainer |
18 | | - { |
19 | | - IBusControl BusControl { get; set; } |
20 | | - } |
21 | | - public interface IRabbitMQContainer: IBusControlContainer |
22 | | - { |
23 | | - IRabbitMqHost RabbitMqHost { get; set; } |
24 | | - |
25 | | - } |
26 | | - public abstract class BusControlContainerBase : IBusControlContainer |
27 | | - { |
28 | | - public IBusControl BusControl { get; set; } |
29 | | - public IPublishEndpoint PublishEndpoint => BusControl; |
30 | | - public ISendEndpointProvider SendEndpointProvider => BusControl; |
31 | | - public IBus Bus => BusControl; |
32 | | - } |
33 | | - public abstract class RabbitMQContainerBase : BusControlContainerBase, IRabbitMQContainer |
34 | | - { |
35 | | - public IRabbitMqHost RabbitMqHost { get; set; } |
36 | | - } |
37 | | - public static class AspNetCoreServiceExtensions |
38 | | - { |
39 | | - public static void AddRabbitMqMassTransitOptions<TOptions>( |
40 | | - this IServiceCollection services,Action<MassTransitOptions> configureOptions) |
41 | | - where TOptions : MassTransitOptions, new() |
42 | | - { |
43 | | - services.Configure<TOptions>(configureOptions); |
44 | | - } |
45 | | - public static void AddRabbitMqMassTransitOptions<TOptions>( |
46 | | - this IServiceCollection services,IConfiguration config) |
47 | | - where TOptions : MassTransitOptions, new() |
48 | | - { |
49 | | - services.Configure<TOptions>(config); |
50 | | - } |
51 | | - |
52 | | - |
53 | | - public static void AddRabbitMQMassTransit<TOptions, TService, TImplementation, THandlerRegistrants>(this IServiceCollection services) |
54 | | - where TOptions : MassTransitOptions, new() |
55 | | - where TService : class, IRabbitMQContainer |
56 | | - where TImplementation : class, TService, new() |
57 | | - where THandlerRegistrants : class, IRabbitMQReceiveEndpointRegistration |
58 | | - |
59 | | - { |
60 | | - services.AddSingleton<TService>((sp) => { |
61 | | - |
62 | | - var options = sp.GetRequiredService<IOptions<TOptions>>(); |
63 | | - var opts = options.Value; |
64 | | - IBusControl bus = null; |
65 | | - TImplementation container = null; |
66 | | - switch (opts.TransportType) |
67 | | - { |
68 | | - case MassTransitOptions.Transport.RabbitMQ: |
69 | | - IRabbitMqHost host = null; |
70 | | - bus = Bus.Factory.CreateUsingRabbitMq(cfg => |
71 | | - { |
72 | | - var uriString = $"rabbitmq://{opts.Host}/"; |
73 | | - host = cfg.Host(new Uri(uriString), h => |
74 | | - { |
75 | | - h.Username(opts.Username); |
76 | | - h.Password(opts.Password); |
77 | | - }); |
78 | | - var registrants = sp.GetRequiredService<IEnumerable<THandlerRegistrants>>(); |
79 | | - if (registrants != null && registrants.Any()) |
80 | | - { |
81 | | - foreach (var registrant in registrants) |
82 | | - { |
83 | | - registrant.AddReceiveEndpoint(cfg, host); |
84 | | - } |
85 | | - } |
86 | | - }); |
87 | | - container = new TImplementation { BusControl = bus, RabbitMqHost = host }; |
88 | | - |
89 | | - break; |
90 | | - default: |
91 | | - throw new ArgumentOutOfRangeException($"MasstransitOptions.Transport:{opts.TransportType} is not supported"); |
92 | | - } |
93 | | -// bus.Start(); |
94 | | - |
95 | | - return container; |
96 | | - }); |
97 | | - } |
98 | | - } |
99 | | -} |
| 1 | +using System; |
| 2 | +using Microsoft.Extensions.DependencyInjection; |
| 3 | +using Microsoft.Extensions.Configuration; |
| 4 | +using MassTransit; |
| 5 | +using MassTransit.RabbitMqTransport; |
| 6 | +using Microsoft.Extensions.Options; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq; |
| 9 | + |
| 10 | +namespace MassTransitAbstractions.Extensions |
| 11 | +{ |
| 12 | + |
| 13 | + public interface IRabbitMQReceiveEndpointRegistration |
| 14 | + { |
| 15 | + void AddReceiveEndpoint(IRabbitMqBusFactoryConfigurator cfg, IRabbitMqHost host); |
| 16 | + } |
| 17 | + public interface IBusControlContainer |
| 18 | + { |
| 19 | + IBusControl BusControl { get; set; } |
| 20 | + } |
| 21 | + public interface IRabbitMQContainer: IBusControlContainer |
| 22 | + { |
| 23 | + IRabbitMqHost RabbitMqHost { get; set; } |
| 24 | + |
| 25 | + } |
| 26 | + public abstract class BusControlContainerBase : IBusControlContainer |
| 27 | + { |
| 28 | + public IBusControl BusControl { get; set; } |
| 29 | + public IPublishEndpoint PublishEndpoint => BusControl; |
| 30 | + public ISendEndpointProvider SendEndpointProvider => BusControl; |
| 31 | + public IBus Bus => BusControl; |
| 32 | + } |
| 33 | + public abstract class RabbitMQContainerBase : BusControlContainerBase, IRabbitMQContainer |
| 34 | + { |
| 35 | + public IRabbitMqHost RabbitMqHost { get; set; } |
| 36 | + } |
| 37 | + public static class AspNetCoreServiceExtensions |
| 38 | + { |
| 39 | + public static void AddRabbitMqMassTransitOptions<TOptions>( |
| 40 | + this IServiceCollection services,Action<MassTransitOptions> configureOptions) |
| 41 | + where TOptions : MassTransitOptions, new() |
| 42 | + { |
| 43 | + services.Configure<TOptions>(configureOptions); |
| 44 | + } |
| 45 | + public static void AddRabbitMqMassTransitOptions<TOptions>( |
| 46 | + this IServiceCollection services,IConfiguration config) |
| 47 | + where TOptions : MassTransitOptions, new() |
| 48 | + { |
| 49 | + services.Configure<TOptions>(config); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + public static void AddRabbitMQMassTransit<TOptions, TService, TImplementation, THandlerRegistrants>(this IServiceCollection services) |
| 54 | + where TOptions : MassTransitOptions, new() |
| 55 | + where TService : class, IRabbitMQContainer |
| 56 | + where TImplementation : class, TService, new() |
| 57 | + where THandlerRegistrants : class, IRabbitMQReceiveEndpointRegistration |
| 58 | + |
| 59 | + { |
| 60 | + services.AddSingleton<TService>((sp) => { |
| 61 | + |
| 62 | + var options = sp.GetRequiredService<IOptions<TOptions>>(); |
| 63 | + var opts = options.Value; |
| 64 | + IBusControl bus = null; |
| 65 | + TImplementation container = null; |
| 66 | + switch (opts.TransportType) |
| 67 | + { |
| 68 | + case MassTransitOptions.Transport.RabbitMQ: |
| 69 | + IRabbitMqHost host = null; |
| 70 | + bus = Bus.Factory.CreateUsingRabbitMq(cfg => |
| 71 | + { |
| 72 | + var uriString = $"rabbitmq://{opts.Host}/"; |
| 73 | + host = cfg.Host(new Uri(uriString), h => |
| 74 | + { |
| 75 | + h.Username(opts.Username); |
| 76 | + h.Password(opts.Password); |
| 77 | + }); |
| 78 | + var registrants = sp.GetRequiredService<IEnumerable<THandlerRegistrants>>(); |
| 79 | + if (registrants != null && registrants.Any()) |
| 80 | + { |
| 81 | + foreach (var registrant in registrants) |
| 82 | + { |
| 83 | + registrant.AddReceiveEndpoint(cfg, host); |
| 84 | + } |
| 85 | + } |
| 86 | + }); |
| 87 | + container = new TImplementation { BusControl = bus, RabbitMqHost = host }; |
| 88 | + |
| 89 | + break; |
| 90 | + default: |
| 91 | + throw new ArgumentOutOfRangeException($"MasstransitOptions.Transport:{opts.TransportType} is not supported"); |
| 92 | + } |
| 93 | +// bus.Start(); |
| 94 | + |
| 95 | + return container; |
| 96 | + }); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments