diff --git a/src/SampleBatch.Api/SampleBatch.Api.csproj b/src/SampleBatch.Api/SampleBatch.Api.csproj index 75984b8..da673e9 100644 --- a/src/SampleBatch.Api/SampleBatch.Api.csproj +++ b/src/SampleBatch.Api/SampleBatch.Api.csproj @@ -13,13 +13,13 @@ - - - - - - - + + + + + + + diff --git a/src/SampleBatch.Api/Startup.cs b/src/SampleBatch.Api/Startup.cs index f4194d3..def7ce3 100644 --- a/src/SampleBatch.Api/Startup.cs +++ b/src/SampleBatch.Api/Startup.cs @@ -5,17 +5,13 @@ using System.Text; using Contracts; using MassTransit; - using MassTransit.Azure.ServiceBus.Core; - using MassTransit.Definition; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; - using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; - using Microsoft.Extensions.Options; public class Startup @@ -33,14 +29,40 @@ public void ConfigureServices(IServiceCollection services) services.AddHealthChecks(); services.AddMvc(); + var appConfig = Configuration.GetSection(nameof(AppConfig)).Get(); services.Configure(options => Configuration.GetSection("AppConfig").Bind(options)); - services.TryAddSingleton(KebabCaseEndpointNameFormatter.Instance); services.AddMassTransit(cfg => { + cfg.SetKebabCaseEndpointNameFormatter(); cfg.AddRequestClient(); cfg.AddRequestClient(); - cfg.AddBus(ConfigureBus); + + if (appConfig.AzureServiceBus != null) + { + cfg.UsingAzureServiceBus((x, y) => + { + y.Host(appConfig.AzureServiceBus.ConnectionString); + y.ConfigureEndpoints(x); + }); + } + else if (appConfig.RabbitMq != null) + { + cfg.UsingRabbitMq((x, y) => + { + y.Host(appConfig.RabbitMq.HostAddress, appConfig.RabbitMq.VirtualHost, h => + { + h.Username(appConfig.RabbitMq.Username); + h.Password(appConfig.RabbitMq.Password); + }); + + y.ConfigureEndpoints(x); + }); + } + else + { + throw new ApplicationException("Invalid Bus configuration. Couldn't find Azure or RabbitMq config"); + } }); services.AddMassTransitHostedService(); @@ -80,42 +102,5 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApp cache.Set("cachedTimeUTC", encodedCurrentTimeUtc, options); }); } - - static IBusControl ConfigureBus(IServiceProvider provider) - { - var appSettings = provider.GetRequiredService>().Value; - - if (appSettings.AzureServiceBus != null) - return ConfigureAzureSb(provider, appSettings); - - if (appSettings.RabbitMq != null) - return ConfigureRabbitMqBus(provider, appSettings); - - throw new ApplicationException("Invalid Bus configuration. Couldn't find Azure or RabbitMq config"); - } - - static IBusControl ConfigureRabbitMqBus(IServiceProvider provider, AppConfig appConfig) - { - return Bus.Factory.CreateUsingRabbitMq(cfg => - { - cfg.Host(appConfig.RabbitMq.HostAddress, appConfig.RabbitMq.VirtualHost, h => - { - h.Username(appConfig.RabbitMq.Username); - h.Password(appConfig.RabbitMq.Password); - }); - - cfg.ConfigureEndpoints(provider); - }); - } - - static IBusControl ConfigureAzureSb(IServiceProvider provider, AppConfig appConfig) - { - return Bus.Factory.CreateUsingAzureServiceBus(cfg => - { - cfg.Host(appConfig.AzureServiceBus.ConnectionString); - - cfg.ConfigureEndpoints(provider); - }); - } } } \ No newline at end of file diff --git a/src/SampleBatch.Common/SampleBatch.Common.csproj b/src/SampleBatch.Common/SampleBatch.Common.csproj index 353aadb..ba907f7 100644 --- a/src/SampleBatch.Common/SampleBatch.Common.csproj +++ b/src/SampleBatch.Common/SampleBatch.Common.csproj @@ -6,8 +6,8 @@ - - + + diff --git a/src/SampleBatch.Components/SampleBatch.Components.csproj b/src/SampleBatch.Components/SampleBatch.Components.csproj index 857d25b..af4eb72 100644 --- a/src/SampleBatch.Components/SampleBatch.Components.csproj +++ b/src/SampleBatch.Components/SampleBatch.Components.csproj @@ -6,8 +6,8 @@ - - + + diff --git a/src/SampleBatch.Contracts/SampleBatch.Contracts.csproj b/src/SampleBatch.Contracts/SampleBatch.Contracts.csproj index 9df6298..aa4404b 100644 --- a/src/SampleBatch.Contracts/SampleBatch.Contracts.csproj +++ b/src/SampleBatch.Contracts/SampleBatch.Contracts.csproj @@ -6,7 +6,7 @@ - + diff --git a/src/SampleBatch.Service/Program.cs b/src/SampleBatch.Service/Program.cs index feec0c6..6483b1a 100644 --- a/src/SampleBatch.Service/Program.cs +++ b/src/SampleBatch.Service/Program.cs @@ -10,17 +10,12 @@ using Components.StateMachines; using Contracts; using MassTransit; - using MassTransit.Azure.ServiceBus.Core; - using MassTransit.Definition; using MassTransit.EntityFrameworkCoreIntegration; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; - using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; - using Microsoft.Extensions.Options; - class Program { @@ -41,11 +36,12 @@ static async Task Main(string[] args) }) .ConfigureServices((hostContext, services) => { + var appConfig = hostContext.Configuration.GetSection(nameof(AppConfig)).Get(); services.Configure(options => hostContext.Configuration.GetSection("AppConfig").Bind(options)); - services.TryAddSingleton(KebabCaseEndpointNameFormatter.Instance); services.AddMassTransit(cfg => { + cfg.SetKebabCaseEndpointNameFormatter(); cfg.AddSagaStateMachine(typeof(BatchStateMachineDefinition)) .EntityFrameworkRepository(r => { @@ -81,7 +77,42 @@ static async Task Main(string[] args) cfg.AddConsumersFromNamespaceContaining(); cfg.AddActivitiesFromNamespaceContaining(); - cfg.AddBus(ConfigureBus); + if (appConfig.AzureServiceBus != null) + { + cfg.UsingAzureServiceBus((x, y) => + { + y.Host(appConfig.AzureServiceBus.ConnectionString); + + var endpointNameFormatter = x.GetRequiredService(); + EndpointConvention.Map(new Uri($"queue:{endpointNameFormatter.Consumer()}")); + + y.UseServiceBusMessageScheduler(); + + y.ConfigureEndpoints(x); + }); + } + else if (appConfig.RabbitMq != null) + { + cfg.UsingRabbitMq((x, y) => + { + y.Host(appConfig.RabbitMq.HostAddress, appConfig.RabbitMq.VirtualHost, h => + { + h.Username(appConfig.RabbitMq.Username); + h.Password(appConfig.RabbitMq.Password); + }); + + var endpointNameFormatter = x.GetRequiredService(); + EndpointConvention.Map(new Uri($"queue:{endpointNameFormatter.Consumer()}")); + + y.UseInMemoryScheduler(); + + y.ConfigureEndpoints(x); + }); + } + else + { + throw new ApplicationException("Invalid Bus configuration. Couldn't find Azure or RabbitMq config"); + } }); services.AddDbContext(x => x.UseSqlServer(hostContext.Configuration.GetConnectionString("sample-batch"))); @@ -108,54 +139,5 @@ static async Task Main(string[] args) else await builder.RunConsoleAsync(); } - - static IBusControl ConfigureBus(IServiceProvider provider) - { - var appSettings = provider.GetRequiredService>().Value; - - if (appSettings.AzureServiceBus != null) - return ConfigureAzureSb(provider, appSettings); - - if (appSettings.RabbitMq != null) - return ConfigureRabbitMqBus(provider, appSettings); - - throw new ApplicationException("Invalid Bus configuration. Couldn't find Azure or RabbitMq config"); - } - - static IBusControl ConfigureRabbitMqBus(IServiceProvider provider, AppConfig appConfig) - { - var endpointNameFormatter = provider.GetService() ?? KebabCaseEndpointNameFormatter.Instance; - - return Bus.Factory.CreateUsingRabbitMq(cfg => - { - cfg.Host(appConfig.RabbitMq.HostAddress, appConfig.RabbitMq.VirtualHost, h => - { - h.Username(appConfig.RabbitMq.Username); - h.Password(appConfig.RabbitMq.Password); - }); - - EndpointConvention.Map(new Uri($"queue:{endpointNameFormatter.Consumer()}")); - - cfg.UseInMemoryScheduler(); - - cfg.ConfigureEndpoints(provider, endpointNameFormatter); - }); - } - - static IBusControl ConfigureAzureSb(IServiceProvider provider, AppConfig appConfig) - { - var endpointNameFormatter = provider.GetService() ?? KebabCaseEndpointNameFormatter.Instance; - - return Bus.Factory.CreateUsingAzureServiceBus(cfg => - { - cfg.Host(appConfig.AzureServiceBus.ConnectionString); - - EndpointConvention.Map(new Uri($"queue:{endpointNameFormatter.Consumer()}")); - - cfg.UseServiceBusMessageScheduler(); - - cfg.ConfigureEndpoints(provider, endpointNameFormatter); - }); - } } } \ No newline at end of file diff --git a/src/SampleBatch.Service/SampleBatch.Service.csproj b/src/SampleBatch.Service/SampleBatch.Service.csproj index 5fde563..07fcba3 100644 --- a/src/SampleBatch.Service/SampleBatch.Service.csproj +++ b/src/SampleBatch.Service/SampleBatch.Service.csproj @@ -17,21 +17,21 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + diff --git a/src/SampleBatch.Tests/SampleBatch.Tests.csproj b/src/SampleBatch.Tests/SampleBatch.Tests.csproj index 4caf613..88e9983 100644 --- a/src/SampleBatch.Tests/SampleBatch.Tests.csproj +++ b/src/SampleBatch.Tests/SampleBatch.Tests.csproj @@ -6,12 +6,15 @@ - - - - + + + + - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive +