Skip to content

Commit

Permalink
Updated to the latest, including the hosted service configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
phatboyg committed Mar 20, 2020
1 parent 0f46e1c commit a8bcbf1
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 76 deletions.
12 changes: 6 additions & 6 deletions src/SampleBatch.Api/SampleBatch.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MassTransit.Analyzers" Version="6.2.1" />
<PackageReference Include="MassTransit.AspNetCore" Version="6.2.1" />
<PackageReference Include="MassTransit.Extensions.DependencyInjection" Version="6.2.1" />
<PackageReference Include="MassTransit.RabbitMQ" Version="6.2.1" />
<PackageReference Include="MassTransit.Azure.ServiceBus.Core" Version="6.2.1" />
<PackageReference Include="MassTransit.Analyzers" Version="6.2.3" />
<PackageReference Include="MassTransit.AspNetCore" Version="6.2.3" />
<PackageReference Include="MassTransit.Extensions.DependencyInjection" Version="6.2.3" />
<PackageReference Include="MassTransit.RabbitMQ" Version="6.2.3" />
<PackageReference Include="MassTransit.Azure.ServiceBus.Core" Version="6.2.3" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="3.1.2" />
<PackageReference Include="NSwag.AspNetCore" Version="13.2.3" />
<PackageReference Include="NSwag.AspNetCore" Version="13.2.5" />
</ItemGroup>

<ItemGroup>
Expand Down
49 changes: 24 additions & 25 deletions src/SampleBatch.Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -34,12 +35,16 @@ public void ConfigureServices(IServiceCollection services)

services.Configure<AppConfig>(options => Configuration.GetSection("AppConfig").Bind(options));

services.AddMassTransit(ConfigureBus, cfg =>
services.TryAddSingleton(KebabCaseEndpointNameFormatter.Instance);
services.AddMassTransit(cfg =>
{
cfg.AddRequestClient<SubmitBatch>();
cfg.AddRequestClient<BatchStatusRequested>();
cfg.AddBus(ConfigureBus);
});

services.AddMassTransitHostedService();

services.AddOpenApiDocument(cfg => cfg.PostProcess = d => d.Info.Title = "Sample-Batch");

services.AddStackExchangeRedisCache(options =>
Expand All @@ -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();

Expand All @@ -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);
});
}

Expand All @@ -83,14 +86,10 @@ static IBusControl ConfigureBus(IServiceProvider provider)
var appSettings = provider.GetRequiredService<IOptions<AppConfig>>().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");
}
Expand Down
2 changes: 1 addition & 1 deletion src/SampleBatch.Common/SampleBatch.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MassTransit" Version="6.2.1" />
<PackageReference Include="MassTransit" Version="6.2.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.2" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions src/SampleBatch.Components/SampleBatch.Components.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MassTransit.Analyzers" Version="6.2.1" />
<PackageReference Include="MassTransit.EntityFrameworkCore" Version="6.2.1" />
<PackageReference Include="MassTransit.Analyzers" Version="6.2.3" />
<PackageReference Include="MassTransit.EntityFrameworkCore" Version="6.2.3" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/SampleBatch.Contracts/SampleBatch.Contracts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MassTransit" Version="6.2.1" />
<PackageReference Include="MassTransit" Version="6.2.3" />
</ItemGroup>

</Project>
57 changes: 23 additions & 34 deletions src/SampleBatch.Service/Program.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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");
});
Expand All @@ -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");
});
Expand Down Expand Up @@ -107,28 +104,20 @@ static async Task Main(string[] args)
});

if (isService)
{
await builder.UseWindowsService().Build().RunAsync();
}
else
{
await builder.RunConsoleAsync();
}
}

static IBusControl ConfigureBus(IServiceProvider provider)
{
var appSettings = provider.GetRequiredService<IOptions<AppConfig>>().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");
}
Expand Down
12 changes: 6 additions & 6 deletions src/SampleBatch.Service/SampleBatch.Service.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MassTransit.Analyzers" Version="6.2.1" />
<PackageReference Include="MassTransit.EntityFrameworkCore" Version="6.2.1" />
<PackageReference Include="MassTransit.Extensions.DependencyInjection" Version="6.2.1" />
<PackageReference Include="MassTransit.Quartz" Version="6.2.1" />
<PackageReference Include="MassTransit.RabbitMQ" Version="6.2.1" />
<PackageReference Include="MassTransit.Azure.ServiceBus.Core" Version="6.2.1" />
<PackageReference Include="MassTransit.Analyzers" Version="6.2.3" />
<PackageReference Include="MassTransit.EntityFrameworkCore" Version="6.2.3" />
<PackageReference Include="MassTransit.Extensions.DependencyInjection" Version="6.2.3" />
<PackageReference Include="MassTransit.Quartz" Version="6.2.3" />
<PackageReference Include="MassTransit.RabbitMQ" Version="6.2.3" />
<PackageReference Include="MassTransit.Azure.ServiceBus.Core" Version="6.2.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="3.1.2" />
Expand Down
2 changes: 1 addition & 1 deletion src/SampleBatch.Tests/SampleBatch.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.2" />
<PackageReference Include="MassTransit" Version="6.2.1" />
<PackageReference Include="MassTransit" Version="6.2.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.2" />
<PackageReference Include="xunit" Version="2.4.1" />
Expand Down

0 comments on commit a8bcbf1

Please sign in to comment.