Skip to content

Commit e68fd9c

Browse files
committed
adding MassTransit abstractions
1 parent 4a17c51 commit e68fd9c

11 files changed

Lines changed: 120 additions & 0 deletions
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Configuration;
4+
5+
namespace MassTransitAbstractions.Extensions
6+
{
7+
public static class AspNetCoreServiceExtensions
8+
{
9+
public static void AddMassTransitOptions(this IServiceCollection services,
10+
Action<MassTransitOptions> configureOptions)
11+
{
12+
services.Configure(configureOptions);
13+
}
14+
public static void AddMassTransitOptions(this IServiceCollection services,
15+
IConfiguration config)
16+
{
17+
services.Configure<MassTransitOptions>(config);
18+
}
19+
}
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.2</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<PackageReference Include="MassTransit.RabbitMQ" Version="5.5.4" />
8+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" />
9+
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.2.0" />
10+
11+
12+
</ItemGroup>
13+
</Project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace MassTransitAbstractions
2+
{
3+
public class MassTransitOptions
4+
{
5+
public enum Transport
6+
{
7+
RabbitMQ, AzureServiceBus, AmazonSQS, AmazonMQ, InMemory
8+
}
9+
public Transport TransportType { get; set; }
10+
public string Username { get; set; }
11+
public string Password { get; set; }
12+
13+
}
14+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using MassTransit;
5+
using MassTransit.RabbitMqTransport;
6+
using Microsoft.Extensions.Hosting;
7+
using Microsoft.Extensions.Options;
8+
9+
namespace MassTransitAbstractions
10+
{
11+
public abstract class MessageQueueServiceBase : BackgroundService
12+
{
13+
protected abstract void OnAddReceiveEndpoint(IRabbitMqBusFactoryConfigurator cfg, IRabbitMqHost host);
14+
15+
protected MassTransitOptions _options;
16+
protected IBusControl _bus;
17+
18+
public MessageQueueServiceBase(IOptions<MassTransitOptions> options)
19+
{
20+
_options = options.Value;
21+
switch (_options.TransportType)
22+
{
23+
case MassTransitOptions.Transport.RabbitMQ:
24+
_bus = Bus.Factory.CreateUsingRabbitMq(cfg =>
25+
{
26+
var host = cfg.Host(new Uri("rabbitmq://localhost/"), h =>
27+
{
28+
h.Username(_options.Username);
29+
h.Password(_options.Password);
30+
});
31+
OnAddReceiveEndpoint(cfg, host);
32+
});
33+
break;
34+
default:
35+
throw new ArgumentOutOfRangeException($"MasstransitOptions.Transport:{_options.TransportType} is not supported");
36+
}
37+
38+
}
39+
protected override Task ExecuteAsync(CancellationToken stoppingToken)
40+
{
41+
return _bus.StartAsync();
42+
}
43+
44+
public override Task StopAsync(CancellationToken cancellationToken)
45+
{
46+
return Task.WhenAll(base.StopAsync(cancellationToken), _bus.StopAsync());
47+
}
48+
}
49+
}

PitStopPro.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Eventing", "Eventing", "{AE
5252
EndProject
5353
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pitstop.Infrastructure.Messaging", "src\Pitstop.Infrastructure.Messaging\Pitstop.Infrastructure.Messaging.csproj", "{A8B77573-1C51-492E-A979-CC411163C186}"
5454
EndProject
55+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MassTransitAbstractions", "MasstransitAbstractions\MassTransitAbstractions.csproj", "{9A652B8D-48DC-47E4-A9F8-CAE814F08758}"
56+
EndProject
5557
Global
5658
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5759
Debug|Any CPU = Debug|Any CPU
@@ -122,6 +124,10 @@ Global
122124
{A8B77573-1C51-492E-A979-CC411163C186}.Debug|Any CPU.Build.0 = Debug|Any CPU
123125
{A8B77573-1C51-492E-A979-CC411163C186}.Release|Any CPU.ActiveCfg = Release|Any CPU
124126
{A8B77573-1C51-492E-A979-CC411163C186}.Release|Any CPU.Build.0 = Release|Any CPU
127+
{9A652B8D-48DC-47E4-A9F8-CAE814F08758}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
128+
{9A652B8D-48DC-47E4-A9F8-CAE814F08758}.Debug|Any CPU.Build.0 = Debug|Any CPU
129+
{9A652B8D-48DC-47E4-A9F8-CAE814F08758}.Release|Any CPU.ActiveCfg = Release|Any CPU
130+
{9A652B8D-48DC-47E4-A9F8-CAE814F08758}.Release|Any CPU.Build.0 = Release|Any CPU
125131
EndGlobalSection
126132
GlobalSection(SolutionProperties) = preSolution
127133
HideSolutionNode = FALSE
@@ -143,6 +149,7 @@ Global
143149
{F393D369-F9B7-4AD1-BE5F-2EDDB01E27D9} = {6BB94A19-F5D7-4CEF-88A3-9E8610CB564A}
144150
{6069BCCE-159D-48F0-BC19-D39FF0874AEA} = {6BB94A19-F5D7-4CEF-88A3-9E8610CB564A}
145151
{A8B77573-1C51-492E-A979-CC411163C186} = {AE5A72AB-69BF-4365-B251-39EDB0B452C6}
152+
{9A652B8D-48DC-47E4-A9F8-CAE814F08758} = {AE5A72AB-69BF-4365-B251-39EDB0B452C6}
146153
EndGlobalSection
147154
GlobalSection(ExtensibilityGlobals) = postSolution
148155
SolutionGuid = {40CA8DF3-11D5-48E1-9D3D-DE4FF71A97B4}

src/CustomerManagementAPI.Host/CustomerManagementAPI.Host.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232

3333
<ItemGroup>
34+
<ProjectReference Include="..\..\MasstransitAbstractions\MassTransitAbstractions.csproj" />
3435
<ProjectReference Include="..\AuthRequiredDemo.GraphQL\AuthRequiredDemo.GraphQL.csproj" />
3536
<ProjectReference Include="..\CustomerManagementAPI\CustomerManagementAPI.csproj" />
3637
<ProjectReference Include="..\GQL.GraphQLHost.Core\GQL.GraphQLHost.Core.csproj" />

src/CustomerManagementAPI.Host/Pages/Index.cshtml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using Microsoft.AspNetCore.Mvc;
77
using Microsoft.AspNetCore.Mvc.RazorPages;
88
using Microsoft.Extensions.Caching.Distributed;
9+
using Microsoft.Extensions.Options;
10+
using MassTransitAbstractions;
911

1012
namespace CustomerManagementAPI.Host.Pages
1113
{

src/CustomerManagementAPI.Host/Startup.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.Extensions.Logging;
1414
using Pitstop.Infrastructure.Messaging.Extensions;
1515
using Serilog;
16+
using MassTransitAbstractions.Extensions;
1617

1718
namespace CustomerManagementAPI.Host
1819
{
@@ -47,6 +48,8 @@ protected override void AddHealthChecks(HealthCheckBuilder checks)
4748

4849
protected override void AddAdditionalServices(IServiceCollection services)
4950
{
51+
services.AddMassTransitOptions(Configuration.GetSection("MassTransitOptions"));
52+
5053
var configSection = Configuration.GetSection("RabbitMQ");
5154
string host = configSection["Host"];
5255
string userName = configSection["UserName"];

src/CustomerManagementAPI.Host/appsettings.Development.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
{
2+
"MassTransitOptions": {
3+
"TransportType": "RabbitMQ",
4+
"Username": "rabbitmquser",
5+
"Password": "DEBmbwkSrzy9D1T9cJfa"
6+
},
27
"RabbitMQ": {
38
"Host": "localhost",
49
"Username": "rabbitmquser",

src/CustomerManagementAPI.Host/appsettings.Production.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
{
2+
"MassTransitOptions": {
3+
"TransportType": "RabbitMQ",
4+
"Username": "rabbitmquser",
5+
"Password": "DEBmbwkSrzy9D1T9cJfa"
6+
},
27
"RabbitMQ": {
38
"Host": "rabbitmq",
49
"Username": "rabbitmquser",

0 commit comments

Comments
 (0)