Skip to content

Commit

Permalink
Update to MT V7
Browse files Browse the repository at this point in the history
  • Loading branch information
maldworth committed Jul 9, 2020
1 parent ca91e77 commit d01be29
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 131 deletions.
14 changes: 7 additions & 7 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.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.5" />
<PackageReference Include="MassTransit.Analyzers" Version="7.0.0" />
<PackageReference Include="MassTransit.AspNetCore" Version="7.0.0" />
<PackageReference Include="MassTransit.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="MassTransit.RabbitMQ" Version="7.0.0" />
<PackageReference Include="MassTransit.Azure.ServiceBus.Core" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="3.1.5" />
<PackageReference Include="NSwag.AspNetCore" Version="13.6.2" />
</ItemGroup>

<ItemGroup>
Expand Down
71 changes: 28 additions & 43 deletions src/SampleBatch.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,14 +29,40 @@ public void ConfigureServices(IServiceCollection services)
services.AddHealthChecks();
services.AddMvc();

var appConfig = Configuration.GetSection(nameof(AppConfig)).Get<AppConfig>();
services.Configure<AppConfig>(options => Configuration.GetSection("AppConfig").Bind(options));

services.TryAddSingleton(KebabCaseEndpointNameFormatter.Instance);
services.AddMassTransit(cfg =>
{
cfg.SetKebabCaseEndpointNameFormatter();
cfg.AddRequestClient<SubmitBatch>();
cfg.AddRequestClient<BatchStatusRequested>();
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();
Expand Down Expand Up @@ -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<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");
}

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);
});
}
}
}
4 changes: 2 additions & 2 deletions src/SampleBatch.Common/SampleBatch.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
</PropertyGroup>

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

</Project>
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.3" />
<PackageReference Include="MassTransit.EntityFrameworkCore" Version="6.2.3" />
<PackageReference Include="MassTransit.Analyzers" Version="7.0.0" />
<PackageReference Include="MassTransit.EntityFrameworkCore" Version="7.0.0" />
</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.3" />
<PackageReference Include="MassTransit" Version="7.0.0" />
</ItemGroup>

</Project>
94 changes: 38 additions & 56 deletions src/SampleBatch.Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -41,11 +36,12 @@ static async Task Main(string[] args)
})
.ConfigureServices((hostContext, services) =>
{
var appConfig = hostContext.Configuration.GetSection(nameof(AppConfig)).Get<AppConfig>();
services.Configure<AppConfig>(options => hostContext.Configuration.GetSection("AppConfig").Bind(options));

services.TryAddSingleton(KebabCaseEndpointNameFormatter.Instance);
services.AddMassTransit(cfg =>
{
cfg.SetKebabCaseEndpointNameFormatter();
cfg.AddSagaStateMachine<BatchStateMachine, BatchState>(typeof(BatchStateMachineDefinition))
.EntityFrameworkRepository(r =>
{
Expand Down Expand Up @@ -81,7 +77,42 @@ static async Task Main(string[] args)
cfg.AddConsumersFromNamespaceContaining<ConsumerAnchor>();
cfg.AddActivitiesFromNamespaceContaining<ActivitiesAnchor>();

cfg.AddBus(ConfigureBus);
if (appConfig.AzureServiceBus != null)
{
cfg.UsingAzureServiceBus((x, y) =>
{
y.Host(appConfig.AzureServiceBus.ConnectionString);

var endpointNameFormatter = x.GetRequiredService<IEndpointNameFormatter>();
EndpointConvention.Map<ProcessBatchJob>(new Uri($"queue:{endpointNameFormatter.Consumer<ProcessBatchJobConsumer>()}"));

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<IEndpointNameFormatter>();
EndpointConvention.Map<ProcessBatchJob>(new Uri($"queue:{endpointNameFormatter.Consumer<ProcessBatchJobConsumer>()}"));

y.UseInMemoryScheduler();

y.ConfigureEndpoints(x);
});
}
else
{
throw new ApplicationException("Invalid Bus configuration. Couldn't find Azure or RabbitMq config");
}
});

services.AddDbContext<SampleBatchDbContext>(x => x.UseSqlServer(hostContext.Configuration.GetConnectionString("sample-batch")));
Expand All @@ -108,54 +139,5 @@ static async Task Main(string[] args)
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");
}

static IBusControl ConfigureRabbitMqBus(IServiceProvider provider, AppConfig appConfig)
{
var endpointNameFormatter = provider.GetService<IEndpointNameFormatter>() ?? 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<ProcessBatchJob>(new Uri($"queue:{endpointNameFormatter.Consumer<ProcessBatchJobConsumer>()}"));

cfg.UseInMemoryScheduler();

cfg.ConfigureEndpoints(provider, endpointNameFormatter);
});
}

static IBusControl ConfigureAzureSb(IServiceProvider provider, AppConfig appConfig)
{
var endpointNameFormatter = provider.GetService<IEndpointNameFormatter>() ?? KebabCaseEndpointNameFormatter.Instance;

return Bus.Factory.CreateUsingAzureServiceBus(cfg =>
{
cfg.Host(appConfig.AzureServiceBus.ConnectionString);

EndpointConvention.Map<ProcessBatchJob>(new Uri($"queue:{endpointNameFormatter.Consumer<ProcessBatchJobConsumer>()}"));

cfg.UseServiceBusMessageScheduler();

cfg.ConfigureEndpoints(provider, endpointNameFormatter);
});
}
}
}
30 changes: 15 additions & 15 deletions src/SampleBatch.Service/SampleBatch.Service.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@
</ItemGroup>

<ItemGroup>
<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" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.2" />
<PackageReference Include="MassTransit.Analyzers" Version="7.0.0" />
<PackageReference Include="MassTransit.EntityFrameworkCore" Version="7.0.0" />
<PackageReference Include="MassTransit.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="MassTransit.Quartz" Version="7.0.0" />
<PackageReference Include="MassTransit.RabbitMQ" Version="7.0.0" />
<PackageReference Include="MassTransit.Azure.ServiceBus.Core" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.5" />
</ItemGroup>

<ItemGroup>
Expand Down
13 changes: 8 additions & 5 deletions src/SampleBatch.Tests/SampleBatch.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.2" />
<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="FluentAssertions" Version="5.10.3" />
<PackageReference Include="MassTransit" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.5" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit d01be29

Please sign in to comment.