@ -0,0 +1,22 @@ | |||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
namespace Ordering.API.Application.IntegrationEvents.Events | |||||
{ | |||||
public class OrderStatusChangedToCancelledIntegrationEvent : IntegrationEvent | |||||
{ | |||||
public int OrderId { get; } | |||||
public string OrderStatus { get; } | |||||
public string BuyerName { get; } | |||||
public OrderStatusChangedToCancelledIntegrationEvent(int orderId, string orderStatus, string buyerName) | |||||
{ | |||||
OrderId = orderId; | |||||
OrderStatus = orderStatus; | |||||
BuyerName = buyerName; | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,22 @@ | |||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
namespace Ordering.API.Application.IntegrationEvents.Events | |||||
{ | |||||
public class OrderStatusChangedToShippedIntegrationEvent : IntegrationEvent | |||||
{ | |||||
public int OrderId { get; } | |||||
public string OrderStatus { get; } | |||||
public string BuyerName { get; } | |||||
public OrderStatusChangedToShippedIntegrationEvent(int orderId, string orderStatus, string buyerName) | |||||
{ | |||||
OrderId = orderId; | |||||
OrderStatus = orderStatus; | |||||
BuyerName = buyerName; | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,22 @@ | |||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
namespace Ordering.API.Application.IntegrationEvents.Events | |||||
{ | |||||
public class OrderStatusChangedToSubmittedIntegrationEvent : IntegrationEvent | |||||
{ | |||||
public int OrderId { get; } | |||||
public string OrderStatus { get; } | |||||
public string BuyerName { get; } | |||||
public OrderStatusChangedToSubmittedIntegrationEvent(int orderId, string orderStatus, string buyerName) | |||||
{ | |||||
OrderId = orderId; | |||||
OrderStatus = orderStatus; | |||||
BuyerName = buyerName; | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,30 @@ | |||||
using Autofac; | |||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; | |||||
using Ordering.SignalrHub.IntegrationEvents; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Reflection; | |||||
using System.Threading.Tasks; | |||||
namespace Ordering.SignalrHub.AutofacModules | |||||
{ | |||||
public class ApplicationModule | |||||
: Autofac.Module | |||||
{ | |||||
public string QueriesConnectionString { get; } | |||||
public ApplicationModule() | |||||
{ | |||||
} | |||||
protected override void Load(ContainerBuilder builder) | |||||
{ | |||||
builder.RegisterAssemblyTypes(typeof(OrderStatusChangedToAwaitingValidationIntegrationEvent).GetTypeInfo().Assembly) | |||||
.AsClosedTypesOf(typeof(IIntegrationEventHandler<>)); | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,20 @@ | |||||
FROM microsoft/aspnetcore:2.0 AS base | |||||
WORKDIR /app | |||||
EXPOSE 80 | |||||
FROM microsoft/aspnetcore-build:2.0 AS build | |||||
WORKDIR /src | |||||
COPY eShopOnContainers-ServicesAndWebApps.sln ./ | |||||
COPY src/Services/Ordering/Ordering.SignalrHub/Ordering.SignalrHub.csproj src/Services/Ordering/Ordering.SignalrHub/ | |||||
RUN dotnet restore -nowarn:msb3202,nu1503 | |||||
COPY . . | |||||
WORKDIR /src/src/Services/Ordering/Ordering.SignalrHub | |||||
RUN dotnet build Ordering.SignalrHub.csproj -c Release -o /app | |||||
FROM build AS publish | |||||
RUN dotnet publish Ordering.SignalrHub.csproj -c Release -o /app | |||||
FROM base AS final | |||||
WORKDIR /app | |||||
COPY --from=publish /app . | |||||
ENTRYPOINT ["dotnet", "Ordering.SignalrHub.dll"] |
@ -0,0 +1,28 @@ | |||||
using Microsoft.AspNetCore.SignalR; | |||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; | |||||
using Ordering.SignalrHub.IntegrationEvents.Events; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
namespace Ordering.SignalrHub.IntegrationEvents.EventHandling | |||||
{ | |||||
public class OrderStatusChangedToCancelledIntegrationEventHandler : IIntegrationEventHandler<OrderStatusChangedToCancelledIntegrationEvent> | |||||
{ | |||||
private readonly IHubContext<NotificationsHub> _hubContext; | |||||
public OrderStatusChangedToCancelledIntegrationEventHandler(IHubContext<NotificationsHub> hubContext) | |||||
{ | |||||
_hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext)); | |||||
} | |||||
public async Task Handle(OrderStatusChangedToCancelledIntegrationEvent @event) | |||||
{ | |||||
await _hubContext.Clients | |||||
.Group(@event.BuyerName) | |||||
.SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus }); | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,26 @@ | |||||
using Microsoft.AspNetCore.SignalR; | |||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; | |||||
using Ordering.SignalrHub.IntegrationEvents.Events; | |||||
using System; | |||||
using System.Threading.Tasks; | |||||
namespace Ordering.SignalrHub.IntegrationEvents.EventHandling | |||||
{ | |||||
public class OrderStatusChangedToPaidIntegrationEventHandler : IIntegrationEventHandler<OrderStatusChangedToPaidIntegrationEvent> | |||||
{ | |||||
private readonly IHubContext<NotificationsHub> _hubContext; | |||||
public OrderStatusChangedToPaidIntegrationEventHandler(IHubContext<NotificationsHub> hubContext) | |||||
{ | |||||
_hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext)); | |||||
} | |||||
public async Task Handle(OrderStatusChangedToPaidIntegrationEvent @event) | |||||
{ | |||||
await _hubContext.Clients | |||||
.Group(@event.BuyerName) | |||||
.SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus }); | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,28 @@ | |||||
using Microsoft.AspNetCore.SignalR; | |||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; | |||||
using Ordering.SignalrHub.IntegrationEvents.Events; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
namespace Ordering.SignalrHub.IntegrationEvents.EventHandling | |||||
{ | |||||
public class OrderStatusChangedToShippedIntegrationEventHandler : IIntegrationEventHandler<OrderStatusChangedToShippedIntegrationEvent> | |||||
{ | |||||
private readonly IHubContext<NotificationsHub> _hubContext; | |||||
public OrderStatusChangedToShippedIntegrationEventHandler(IHubContext<NotificationsHub> hubContext) | |||||
{ | |||||
_hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext)); | |||||
} | |||||
public async Task Handle(OrderStatusChangedToShippedIntegrationEvent @event) | |||||
{ | |||||
await _hubContext.Clients | |||||
.Group(@event.BuyerName) | |||||
.SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus }); | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,29 @@ | |||||
using Microsoft.AspNetCore.SignalR; | |||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; | |||||
using Ordering.SignalrHub.IntegrationEvents.Events; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
namespace Ordering.SignalrHub.IntegrationEvents.EventHandling | |||||
{ | |||||
public class OrderStatusChangedToStockConfirmedIntegrationEventHandler : | |||||
IIntegrationEventHandler<OrderStatusChangedToStockConfirmedIntegrationEvent> | |||||
{ | |||||
private readonly IHubContext<NotificationsHub> _hubContext; | |||||
public OrderStatusChangedToStockConfirmedIntegrationEventHandler(IHubContext<NotificationsHub> hubContext) | |||||
{ | |||||
_hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext)); | |||||
} | |||||
public async Task Handle(OrderStatusChangedToStockConfirmedIntegrationEvent @event) | |||||
{ | |||||
await _hubContext.Clients | |||||
.Group(@event.BuyerName) | |||||
.SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus }); | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,29 @@ | |||||
using Microsoft.AspNetCore.SignalR; | |||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; | |||||
using Ordering.SignalrHub.IntegrationEvents.Events; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
namespace Ordering.SignalrHub.IntegrationEvents.EventHandling | |||||
{ | |||||
public class OrderStatusChangedToSubmittedIntegrationEventHandler : | |||||
IIntegrationEventHandler<OrderStatusChangedToSubmittedIntegrationEvent> | |||||
{ | |||||
private readonly IHubContext<NotificationsHub> _hubContext; | |||||
public OrderStatusChangedToSubmittedIntegrationEventHandler(IHubContext<NotificationsHub> hubContext) | |||||
{ | |||||
_hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext)); | |||||
} | |||||
public async Task Handle(OrderStatusChangedToSubmittedIntegrationEvent @event) | |||||
{ | |||||
await _hubContext.Clients | |||||
.Group(@event.BuyerName) | |||||
.SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus }); | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,27 @@ | |||||
using Microsoft.AspNetCore.SignalR; | |||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
namespace Ordering.SignalrHub.IntegrationEvents | |||||
{ | |||||
public class OrderStatusChangedToAwaitingValidationIntegrationEventHandler : IIntegrationEventHandler<OrderStatusChangedToAwaitingValidationIntegrationEvent> | |||||
{ | |||||
private readonly IHubContext<NotificationsHub> _hubContext; | |||||
public OrderStatusChangedToAwaitingValidationIntegrationEventHandler(IHubContext<NotificationsHub> hubContext) | |||||
{ | |||||
_hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext)); | |||||
} | |||||
public async Task Handle(OrderStatusChangedToAwaitingValidationIntegrationEvent @event) | |||||
{ | |||||
await _hubContext.Clients | |||||
.Group(@event.BuyerName) | |||||
.SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus }); | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,19 @@ | |||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; | |||||
using System.Collections.Generic; | |||||
namespace Ordering.SignalrHub.IntegrationEvents | |||||
{ | |||||
public class OrderStatusChangedToAwaitingValidationIntegrationEvent : IntegrationEvent | |||||
{ | |||||
public int OrderId { get; } | |||||
public string OrderStatus { get; } | |||||
public string BuyerName { get; } | |||||
public OrderStatusChangedToAwaitingValidationIntegrationEvent(int orderId, string orderStatus, string buyerName) | |||||
{ | |||||
OrderId = orderId; | |||||
OrderStatus = orderStatus; | |||||
BuyerName = buyerName; | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,22 @@ | |||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
namespace Ordering.SignalrHub.IntegrationEvents.Events | |||||
{ | |||||
public class OrderStatusChangedToCancelledIntegrationEvent : IntegrationEvent | |||||
{ | |||||
public int OrderId { get; } | |||||
public string OrderStatus { get; } | |||||
public string BuyerName { get; } | |||||
public OrderStatusChangedToCancelledIntegrationEvent(int orderId, string orderStatus, string buyerName) | |||||
{ | |||||
OrderId = orderId; | |||||
OrderStatus = orderStatus; | |||||
BuyerName = buyerName; | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,23 @@ | |||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
namespace Ordering.SignalrHub.IntegrationEvents.Events | |||||
{ | |||||
public class OrderStatusChangedToPaidIntegrationEvent : IntegrationEvent | |||||
{ | |||||
public int OrderId { get; } | |||||
public string OrderStatus { get; } | |||||
public string BuyerName { get; } | |||||
public OrderStatusChangedToPaidIntegrationEvent(int orderId, | |||||
string orderStatus, string buyerName) | |||||
{ | |||||
OrderId = orderId; | |||||
OrderStatus = orderStatus; | |||||
BuyerName = buyerName; | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,22 @@ | |||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
namespace Ordering.SignalrHub.IntegrationEvents.Events | |||||
{ | |||||
public class OrderStatusChangedToShippedIntegrationEvent : IntegrationEvent | |||||
{ | |||||
public int OrderId { get; } | |||||
public string OrderStatus { get; } | |||||
public string BuyerName { get; } | |||||
public OrderStatusChangedToShippedIntegrationEvent(int orderId, string orderStatus, string buyerName) | |||||
{ | |||||
OrderId = orderId; | |||||
OrderStatus = orderStatus; | |||||
BuyerName = buyerName; | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,18 @@ | |||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; | |||||
namespace Ordering.SignalrHub.IntegrationEvents.Events | |||||
{ | |||||
public class OrderStatusChangedToStockConfirmedIntegrationEvent : IntegrationEvent | |||||
{ | |||||
public int OrderId { get; } | |||||
public string OrderStatus { get; } | |||||
public string BuyerName { get; } | |||||
public OrderStatusChangedToStockConfirmedIntegrationEvent(int orderId, string orderStatus, string buyerName) | |||||
{ | |||||
OrderId = orderId; | |||||
OrderStatus = orderStatus; | |||||
BuyerName = buyerName; | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,22 @@ | |||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
namespace Ordering.SignalrHub.IntegrationEvents.Events | |||||
{ | |||||
public class OrderStatusChangedToSubmittedIntegrationEvent : IntegrationEvent | |||||
{ | |||||
public int OrderId { get; } | |||||
public string OrderStatus { get; } | |||||
public string BuyerName { get; } | |||||
public OrderStatusChangedToSubmittedIntegrationEvent(int orderId, string orderStatus, string buyerName) | |||||
{ | |||||
OrderId = orderId; | |||||
OrderStatus = orderStatus; | |||||
BuyerName = buyerName; | |||||
} | |||||
} | |||||
} |
@ -1,9 +1,11 @@ | |||||
using Microsoft.AspNetCore.Authorization; | using Microsoft.AspNetCore.Authorization; | ||||
using Microsoft.AspNetCore.SignalR; | using Microsoft.AspNetCore.SignalR; | ||||
using System; | using System; | ||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
namespace Ordering.API.Infrastructure.Hubs | |||||
namespace Ordering.SignalrHub | |||||
{ | { | ||||
[Authorize] | [Authorize] | ||||
public class NotificationsHub : Hub | public class NotificationsHub : Hub |
@ -0,0 +1,25 @@ | |||||
<Project Sdk="Microsoft.NET.Sdk.Web"> | |||||
<PropertyGroup> | |||||
<TargetFramework>netcoreapp2.0</TargetFramework> | |||||
<DockerComposeProjectPath>..\..\..\..\docker-compose.dcproj</DockerComposeProjectPath> | |||||
</PropertyGroup> | |||||
<ItemGroup> | |||||
<Folder Include="wwwroot\" /> | |||||
</ItemGroup> | |||||
<ItemGroup> | |||||
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.2.1" /> | |||||
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.6" /> | |||||
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.0.0-preview2-final" /> | |||||
<PackageReference Include="Microsoft.AspNetCore.SignalR.Core" Version="1.0.0-preview2-final" /> | |||||
</ItemGroup> | |||||
<ItemGroup> | |||||
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusRabbitMQ\EventBusRabbitMQ.csproj" /> | |||||
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusServiceBus\EventBusServiceBus.csproj" /> | |||||
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBus\EventBus.csproj" /> | |||||
</ItemGroup> | |||||
</Project> |
@ -0,0 +1,25 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.IO; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
using Microsoft.AspNetCore; | |||||
using Microsoft.AspNetCore.Hosting; | |||||
using Microsoft.Extensions.Configuration; | |||||
using Microsoft.Extensions.Logging; | |||||
namespace Ordering.SignalrHub | |||||
{ | |||||
public class Program | |||||
{ | |||||
public static void Main(string[] args) | |||||
{ | |||||
BuildWebHost(args).Run(); | |||||
} | |||||
public static IWebHost BuildWebHost(string[] args) => | |||||
WebHost.CreateDefaultBuilder(args) | |||||
.UseStartup<Startup>() | |||||
.Build(); | |||||
} | |||||
} |
@ -0,0 +1,27 @@ | |||||
{ | |||||
"iisSettings": { | |||||
"windowsAuthentication": false, | |||||
"anonymousAuthentication": true, | |||||
"iisExpress": { | |||||
"applicationUrl": "http://localhost:51311/", | |||||
"sslPort": 0 | |||||
} | |||||
}, | |||||
"profiles": { | |||||
"IIS Express": { | |||||
"commandName": "IISExpress", | |||||
"launchBrowser": true, | |||||
"environmentVariables": { | |||||
"ASPNETCORE_ENVIRONMENT": "Development" | |||||
} | |||||
}, | |||||
"Ordering.SignalrHub": { | |||||
"commandName": "Project", | |||||
"launchBrowser": true, | |||||
"environmentVariables": { | |||||
"ASPNETCORE_ENVIRONMENT": "Development" | |||||
}, | |||||
"applicationUrl": "http://localhost:51312/" | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,204 @@ | |||||
using Autofac; | |||||
using Autofac.Extensions.DependencyInjection; | |||||
using Microsoft.AspNetCore.Authentication.JwtBearer; | |||||
using Microsoft.AspNetCore.Builder; | |||||
using Microsoft.Azure.ServiceBus; | |||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus; | |||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; | |||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ; | |||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus; | |||||
using Microsoft.Extensions.Configuration; | |||||
using Microsoft.Extensions.DependencyInjection; | |||||
using Microsoft.Extensions.Logging; | |||||
using Ordering.SignalrHub.AutofacModules; | |||||
using Ordering.SignalrHub.IntegrationEvents; | |||||
using Ordering.SignalrHub.IntegrationEvents.EventHandling; | |||||
using Ordering.SignalrHub.IntegrationEvents.Events; | |||||
using RabbitMQ.Client; | |||||
using System; | |||||
using System.IdentityModel.Tokens.Jwt; | |||||
namespace Ordering.SignalrHub | |||||
{ | |||||
public class Startup | |||||
{ | |||||
public Startup(IConfiguration configuration) | |||||
{ | |||||
Configuration = configuration; | |||||
} | |||||
// This method gets called by the runtime. Use this method to add services to the container. | |||||
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 | |||||
public IServiceProvider ConfigureServices(IServiceCollection services) | |||||
{ | |||||
services.AddCors(options => | |||||
{ | |||||
options.AddPolicy("CorsPolicy", | |||||
builder => builder.AllowAnyOrigin() | |||||
.AllowAnyMethod() | |||||
.AllowAnyHeader() | |||||
.AllowCredentials()); | |||||
}); | |||||
services.AddSignalR(); | |||||
if (Configuration.GetValue<bool>("AzureServiceBusEnabled")) | |||||
{ | |||||
services.AddSingleton<IServiceBusPersisterConnection>(sp => | |||||
{ | |||||
var logger = sp.GetRequiredService<ILogger<DefaultServiceBusPersisterConnection>>(); | |||||
var serviceBusConnectionString = Configuration["EventBusConnection"]; | |||||
var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); | |||||
return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger); | |||||
}); | |||||
} | |||||
else | |||||
{ | |||||
services.AddSingleton<IRabbitMQPersistentConnection>(sp => | |||||
{ | |||||
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>(); | |||||
var factory = new ConnectionFactory() | |||||
{ | |||||
HostName = Configuration["EventBusConnection"] | |||||
}; | |||||
if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) | |||||
{ | |||||
factory.UserName = Configuration["EventBusUserName"]; | |||||
} | |||||
if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) | |||||
{ | |||||
factory.Password = Configuration["EventBusPassword"]; | |||||
} | |||||
var retryCount = 5; | |||||
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) | |||||
{ | |||||
retryCount = int.Parse(Configuration["EventBusRetryCount"]); | |||||
} | |||||
return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); | |||||
}); | |||||
} | |||||
ConfigureAuthService(services); | |||||
RegisterEventBus(services); | |||||
services.AddOptions(); | |||||
//configure autofac | |||||
var container = new ContainerBuilder(); | |||||
container.RegisterModule(new ApplicationModule()); | |||||
container.Populate(services); | |||||
return new AutofacServiceProvider(container.Build()); | |||||
} | |||||
public IConfiguration Configuration { get; } | |||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | |||||
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) | |||||
{ | |||||
loggerFactory.AddConsole(Configuration.GetSection("Logging")); | |||||
loggerFactory.AddDebug(); | |||||
loggerFactory.AddAzureWebAppDiagnostics(); | |||||
loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Trace); | |||||
var pathBase = Configuration["PATH_BASE"]; | |||||
if (!string.IsNullOrEmpty(pathBase)) | |||||
{ | |||||
loggerFactory.CreateLogger("init").LogDebug($"Using PATH BASE '{pathBase}'"); | |||||
app.UsePathBase(pathBase); | |||||
} | |||||
app.UseCors("CorsPolicy"); | |||||
app.UseAuthentication(); | |||||
app.UseSignalR(routes => | |||||
{ | |||||
routes.MapHub<NotificationsHub>("/notificationhub", options => | |||||
options.Transports = Microsoft.AspNetCore.Http.Connections.TransportType.All); | |||||
}); | |||||
ConfigureEventBus(app); | |||||
} | |||||
private void ConfigureEventBus(IApplicationBuilder app) | |||||
{ | |||||
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>(); | |||||
eventBus.Subscribe<OrderStatusChangedToAwaitingValidationIntegrationEvent, OrderStatusChangedToAwaitingValidationIntegrationEventHandler>(); | |||||
eventBus.Subscribe<OrderStatusChangedToPaidIntegrationEvent, OrderStatusChangedToPaidIntegrationEventHandler>(); | |||||
eventBus.Subscribe<OrderStatusChangedToStockConfirmedIntegrationEvent, OrderStatusChangedToStockConfirmedIntegrationEventHandler>(); | |||||
eventBus.Subscribe<OrderStatusChangedToShippedIntegrationEvent, OrderStatusChangedToShippedIntegrationEventHandler>(); | |||||
eventBus.Subscribe<OrderStatusChangedToCancelledIntegrationEvent, OrderStatusChangedToCancelledIntegrationEventHandler>(); | |||||
eventBus.Subscribe<OrderStatusChangedToSubmittedIntegrationEvent, OrderStatusChangedToSubmittedIntegrationEventHandler>(); | |||||
} | |||||
private void ConfigureAuthService(IServiceCollection services) | |||||
{ | |||||
// prevent from mapping "sub" claim to nameidentifier. | |||||
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("sub"); | |||||
var identityUrl = Configuration.GetValue<string>("IdentityUrl"); | |||||
services.AddAuthentication(options => | |||||
{ | |||||
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; | |||||
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; | |||||
}).AddJwtBearer(options => | |||||
{ | |||||
options.Authority = identityUrl; | |||||
options.RequireHttpsMetadata = false; | |||||
options.Audience = "orders.signalrhub"; | |||||
}); | |||||
} | |||||
private void RegisterEventBus(IServiceCollection services) | |||||
{ | |||||
var subscriptionClientName = Configuration["SubscriptionClientName"]; | |||||
if (Configuration.GetValue<bool>("AzureServiceBusEnabled")) | |||||
{ | |||||
services.AddSingleton<IEventBus, EventBusServiceBus>(sp => | |||||
{ | |||||
var serviceBusPersisterConnection = sp.GetRequiredService<IServiceBusPersisterConnection>(); | |||||
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>(); | |||||
var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>(); | |||||
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); | |||||
return new EventBusServiceBus(serviceBusPersisterConnection, logger, | |||||
eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope); | |||||
}); | |||||
} | |||||
else | |||||
{ | |||||
services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp => | |||||
{ | |||||
var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>(); | |||||
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>(); | |||||
var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>(); | |||||
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); | |||||
var retryCount = 5; | |||||
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) | |||||
{ | |||||
retryCount = int.Parse(Configuration["EventBusRetryCount"]); | |||||
} | |||||
return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount); | |||||
}); | |||||
} | |||||
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>(); | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,15 @@ | |||||
{ | |||||
"IdentityUrl": "http://localhost:5105", | |||||
"Logging": { | |||||
"IncludeScopes": false, | |||||
"LogLevel": { | |||||
"Default": "Trace", | |||||
"System": "Information", | |||||
"Microsoft": "Information" | |||||
} | |||||
}, | |||||
"AzureServiceBusEnabled": false, | |||||
"SubscriptionClientName": "Ordering.signalrhub", | |||||
"EventBusRetryCount": 5, | |||||
"EventBusConnection": "localhost" | |||||
} |