From a8bcbf1fd829d4fae43009ee88eeb5afb760a0cc Mon Sep 17 00:00:00 2001 From: Chris Patterson Date: Fri, 20 Mar 2020 13:37:47 -0500 Subject: [PATCH] Updated to the latest, including the hosted service configuration. --- src/SampleBatch.Api/SampleBatch.Api.csproj | 12 ++-- src/SampleBatch.Api/Startup.cs | 49 ++++++++-------- .../SampleBatch.Common.csproj | 2 +- .../SampleBatch.Components.csproj | 4 +- .../SampleBatch.Contracts.csproj | 2 +- src/SampleBatch.Service/Program.cs | 57 ++++++++----------- .../SampleBatch.Service.csproj | 12 ++-- .../SampleBatch.Tests.csproj | 2 +- 8 files changed, 64 insertions(+), 76 deletions(-) diff --git a/src/SampleBatch.Api/SampleBatch.Api.csproj b/src/SampleBatch.Api/SampleBatch.Api.csproj index 7cd5191..75984b8 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 13592e8..f4194d3 100644 --- a/src/SampleBatch.Api/Startup.cs +++ b/src/SampleBatch.Api/Startup.cs @@ -1,21 +1,22 @@ -using System; -using MassTransit; -using MassTransit.AspNetCoreIntegration; -using MassTransit.Azure.ServiceBus.Core; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Options; -using SampleBatch.Contracts; - - -namespace SampleBatch.Api +namespace SampleBatch.Api { + using System; + using System.Globalization; + 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 System.Text; + using Microsoft.Extensions.Configuration; + using Microsoft.Extensions.DependencyInjection; + using Microsoft.Extensions.DependencyInjection.Extensions; + using Microsoft.Extensions.Hosting; + using Microsoft.Extensions.Options; + public class Startup { @@ -34,12 +35,16 @@ public void ConfigureServices(IServiceCollection services) services.Configure(options => Configuration.GetSection("AppConfig").Bind(options)); - services.AddMassTransit(ConfigureBus, cfg => + services.TryAddSingleton(KebabCaseEndpointNameFormatter.Instance); + services.AddMassTransit(cfg => { cfg.AddRequestClient(); cfg.AddRequestClient(); + cfg.AddBus(ConfigureBus); }); + services.AddMassTransitHostedService(); + services.AddOpenApiDocument(cfg => cfg.PostProcess = d => d.Info.Title = "Sample-Batch"); services.AddStackExchangeRedisCache(options => @@ -52,9 +57,7 @@ public void ConfigureServices(IServiceCollection services) public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime, IDistributedCache cache) { if (env.IsDevelopment()) - { app.UseDeveloperExceptionPage(); - } app.UseRouting(); @@ -70,11 +73,11 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApp lifetime.ApplicationStarted.Register(() => { - var currentTimeUTC = DateTime.UtcNow.ToString(); - byte[] encodedCurrentTimeUTC = Encoding.UTF8.GetBytes(currentTimeUTC); + var currentTimeUtc = DateTime.UtcNow.ToString(CultureInfo.InvariantCulture); + byte[] encodedCurrentTimeUtc = Encoding.UTF8.GetBytes(currentTimeUtc); var options = new DistributedCacheEntryOptions() .SetSlidingExpiration(TimeSpan.FromSeconds(60)); - cache.Set("cachedTimeUTC", encodedCurrentTimeUTC, options); + cache.Set("cachedTimeUTC", encodedCurrentTimeUtc, options); }); } @@ -83,14 +86,10 @@ 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"); } diff --git a/src/SampleBatch.Common/SampleBatch.Common.csproj b/src/SampleBatch.Common/SampleBatch.Common.csproj index 38a076c..353aadb 100644 --- a/src/SampleBatch.Common/SampleBatch.Common.csproj +++ b/src/SampleBatch.Common/SampleBatch.Common.csproj @@ -6,7 +6,7 @@ - + diff --git a/src/SampleBatch.Components/SampleBatch.Components.csproj b/src/SampleBatch.Components/SampleBatch.Components.csproj index 3e12622..857d25b 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 9fc9502..9df6298 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 ae92d1a..bee937f 100644 --- a/src/SampleBatch.Service/Program.cs +++ b/src/SampleBatch.Service/Program.cs @@ -1,28 +1,25 @@ -using MassTransit; -using MassTransit.Definition; -using MassTransit.EntityFrameworkCoreIntegration; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using SampleBatch.Components; -using SampleBatch.Components.Consumers; -using SampleBatch.Components.StateMachines; -using SampleBatch.Contracts; -using System; -using System.Diagnostics; -using System.Linq; -using System.Threading.Tasks; -using SampleBatch.Common; -using SampleBatch.Components.Activities; -using MassTransit.Azure.ServiceBus.Core; - - -namespace SampleBatch.Service +namespace SampleBatch.Service { + using System; + using System.Diagnostics; + using System.Linq; + using System.Threading.Tasks; + using Components; + using Components.Activities; + using Components.Consumers; + 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 @@ -36,7 +33,7 @@ static async Task Main(string[] args) var builder = new HostBuilder() .ConfigureAppConfiguration((hostingContext, config) => { - config.AddJsonFile("appsettings.json", optional: true); + config.AddJsonFile("appsettings.json", true); config.AddEnvironmentVariables(); if (args != null) @@ -60,7 +57,7 @@ static async Task Main(string[] args) }); // I specified the MsSqlLockStatements because in my State Entities EFCore EntityConfigurations, I changed the column name from CorrelationId, to "BatchId" and "BatchJobId" - // Otherwise I could just use r.UseSqlServer(), which uses the default, which are "... WHERE CorrelationId = @p0" + // Otherwise, I could just use r.UseSqlServer(), which uses the default, which are "... WHERE CorrelationId = @p0" r.LockStatementProvider = new CustomSqlLockStatementProvider("select * from {0}.{1} WITH (UPDLOCK, ROWLOCK) WHERE BatchId = @p0"); }); @@ -76,7 +73,7 @@ static async Task Main(string[] args) }); // I specified the MsSqlLockStatements because in my State Entities EFCore EntityConfigurations, I changed the column name from CorrelationId, to "BatchId" and "BatchJobId" - // Otherwise I could just use r.UseSqlServer(), which uses the default, which are "... WHERE CorrelationId = @p0" + // Otherwise, I could just use r.UseSqlServer(), which uses the default, which are "... WHERE CorrelationId = @p0" r.LockStatementProvider = new CustomSqlLockStatementProvider("select * from {0}.{1} WITH (UPDLOCK, ROWLOCK) WHERE BatchJobId = @p0"); }); @@ -107,13 +104,9 @@ static async Task Main(string[] args) }); if (isService) - { await builder.UseWindowsService().Build().RunAsync(); - } else - { await builder.RunConsoleAsync(); - } } static IBusControl ConfigureBus(IServiceProvider provider) @@ -121,14 +114,10 @@ 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"); } diff --git a/src/SampleBatch.Service/SampleBatch.Service.csproj b/src/SampleBatch.Service/SampleBatch.Service.csproj index b0b4410..5fde563 100644 --- a/src/SampleBatch.Service/SampleBatch.Service.csproj +++ b/src/SampleBatch.Service/SampleBatch.Service.csproj @@ -17,12 +17,12 @@ - - - - - - + + + + + + diff --git a/src/SampleBatch.Tests/SampleBatch.Tests.csproj b/src/SampleBatch.Tests/SampleBatch.Tests.csproj index 072240c..4caf613 100644 --- a/src/SampleBatch.Tests/SampleBatch.Tests.csproj +++ b/src/SampleBatch.Tests/SampleBatch.Tests.csproj @@ -7,7 +7,7 @@ - +