2018-05-15 16:27:10 +02:00
|
|
|
|
using Autofac;
|
|
|
|
|
using Autofac.Extensions.DependencyInjection;
|
2019-07-23 12:14:09 +02:00
|
|
|
|
using Catalog.API.Grpc;
|
2018-05-15 16:27:10 +02:00
|
|
|
|
using global::Catalog.API.Infrastructure.Filters;
|
|
|
|
|
using global::Catalog.API.IntegrationEvents;
|
2019-07-23 12:14:09 +02:00
|
|
|
|
using HealthChecks.UI.Client;
|
2018-05-15 16:27:10 +02:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
2019-07-23 12:14:09 +02:00
|
|
|
|
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
2018-05-15 16:27:10 +02:00
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Azure.ServiceBus;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services;
|
|
|
|
|
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure;
|
|
|
|
|
using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.EventHandling;
|
|
|
|
|
using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2019-07-23 12:14:09 +02:00
|
|
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
2018-05-15 16:27:10 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
2019-07-29 13:21:00 +02:00
|
|
|
|
using Microsoft.OpenApi.Models;
|
2021-04-09 16:01:38 +05:30
|
|
|
|
using OpenTelemetry.Customization;
|
|
|
|
|
using OpenTelemetry.Customization.Extensions;
|
2018-05-15 16:27:10 +02:00
|
|
|
|
using RabbitMQ.Client;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Data.Common;
|
2019-06-26 12:58:07 +02:00
|
|
|
|
using System.IO;
|
2019-07-23 12:14:09 +02:00
|
|
|
|
using System.Reflection;
|
2018-05-15 16:27:10 +02:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.eShopOnContainers.Services.Catalog.API
|
2016-09-06 17:09:19 -07:00
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
2017-08-29 18:11:30 +02:00
|
|
|
|
public Startup(IConfiguration configuration)
|
2016-09-06 17:09:19 -07:00
|
|
|
|
{
|
2017-08-29 18:11:30 +02:00
|
|
|
|
Configuration = configuration;
|
2016-09-06 17:09:19 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-12 09:25:01 +01:00
|
|
|
|
public IConfiguration Configuration { get; }
|
2017-08-29 18:11:30 +02:00
|
|
|
|
|
2019-08-01 11:04:54 +02:00
|
|
|
|
public IServiceProvider ConfigureServices(IServiceCollection services)
|
2016-09-06 17:09:19 -07:00
|
|
|
|
{
|
2018-05-15 16:27:10 +02:00
|
|
|
|
services.AddAppInsight(Configuration)
|
2019-06-26 12:58:07 +02:00
|
|
|
|
.AddGrpc().Services
|
2018-05-15 16:27:10 +02:00
|
|
|
|
.AddCustomMVC(Configuration)
|
|
|
|
|
.AddCustomDbContext(Configuration)
|
|
|
|
|
.AddCustomOptions(Configuration)
|
2018-05-16 14:45:15 +02:00
|
|
|
|
.AddIntegrationServices(Configuration)
|
2018-05-15 16:27:10 +02:00
|
|
|
|
.AddEventBus(Configuration)
|
2019-07-26 11:11:31 +02:00
|
|
|
|
.AddSwagger(Configuration)
|
2021-04-07 18:05:46 +05:30
|
|
|
|
.AddCustomHealthCheck(Configuration)
|
2021-04-09 16:01:38 +05:30
|
|
|
|
.AddOpenTelemetry(new OpenTelemetryConfig() { ServiceName= "Catalog.API",
|
|
|
|
|
ExportType= Configuration.GetValue<string>("OTEL_USE_EXPORTER"),
|
|
|
|
|
ExportToolEndpoint = Configuration.GetValue<string>("OTEL_EXPORTER_TOOL_ENDPOINT")
|
|
|
|
|
});
|
2018-05-15 16:27:10 +02:00
|
|
|
|
|
|
|
|
|
var container = new ContainerBuilder();
|
|
|
|
|
container.Populate(services);
|
|
|
|
|
|
2019-08-01 11:04:54 +02:00
|
|
|
|
return new AutofacServiceProvider(container.Build());
|
2018-05-15 16:27:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-26 12:58:07 +02:00
|
|
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
|
2018-05-15 16:27:10 +02:00
|
|
|
|
{
|
|
|
|
|
//Configure logs
|
|
|
|
|
|
2019-02-06 13:03:54 +00:00
|
|
|
|
//loggerFactory.AddAzureWebAppDiagnostics();
|
|
|
|
|
//loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Trace);
|
2018-05-15 16:27:10 +02:00
|
|
|
|
|
|
|
|
|
var pathBase = Configuration["PATH_BASE"];
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(pathBase))
|
|
|
|
|
{
|
2019-02-22 15:05:28 +00:00
|
|
|
|
loggerFactory.CreateLogger<Startup>().LogDebug("Using PATH BASE '{pathBase}'", pathBase);
|
2018-05-15 16:27:10 +02:00
|
|
|
|
app.UsePathBase(pathBase);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-26 15:39:50 +02:00
|
|
|
|
app.UseSwagger()
|
|
|
|
|
.UseSwaggerUI(c =>
|
|
|
|
|
{
|
|
|
|
|
c.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "Catalog.API V1");
|
|
|
|
|
});
|
2020-12-30 11:29:47 +05:30
|
|
|
|
|
2019-06-26 12:58:07 +02:00
|
|
|
|
app.UseRouting();
|
2020-06-08 21:58:48 +02:00
|
|
|
|
app.UseCors("CorsPolicy");
|
2019-07-23 12:14:09 +02:00
|
|
|
|
app.UseEndpoints(endpoints =>
|
2019-06-26 12:58:07 +02:00
|
|
|
|
{
|
2019-07-23 12:14:09 +02:00
|
|
|
|
endpoints.MapDefaultControllerRoute();
|
2019-07-26 11:11:31 +02:00
|
|
|
|
endpoints.MapControllers();
|
2019-07-23 12:14:09 +02:00
|
|
|
|
endpoints.MapGet("/_proto/", async ctx =>
|
2019-06-26 12:58:07 +02:00
|
|
|
|
{
|
|
|
|
|
ctx.Response.ContentType = "text/plain";
|
2019-07-17 18:26:20 +02:00
|
|
|
|
using var fs = new FileStream(Path.Combine(env.ContentRootPath, "Proto", "catalog.proto"), FileMode.Open, FileAccess.Read);
|
|
|
|
|
using var sr = new StreamReader(fs);
|
|
|
|
|
while (!sr.EndOfStream)
|
|
|
|
|
{
|
|
|
|
|
var line = await sr.ReadLineAsync();
|
|
|
|
|
if (line != "/* >>" || line != "<< */")
|
|
|
|
|
{
|
|
|
|
|
await ctx.Response.WriteAsync(line);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-26 12:58:07 +02:00
|
|
|
|
});
|
2019-07-23 12:14:09 +02:00
|
|
|
|
endpoints.MapGrpcService<CatalogService>();
|
|
|
|
|
endpoints.MapHealthChecks("/hc", new HealthCheckOptions()
|
|
|
|
|
{
|
|
|
|
|
Predicate = _ => true,
|
|
|
|
|
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
|
|
|
|
|
});
|
|
|
|
|
endpoints.MapHealthChecks("/liveness", new HealthCheckOptions
|
|
|
|
|
{
|
|
|
|
|
Predicate = r => r.Name.Contains("self")
|
|
|
|
|
});
|
2019-06-26 12:58:07 +02:00
|
|
|
|
});
|
2017-04-17 12:28:12 +02:00
|
|
|
|
|
2018-05-15 16:27:10 +02:00
|
|
|
|
ConfigureEventBus(app);
|
|
|
|
|
}
|
2017-10-11 18:53:26 +02:00
|
|
|
|
|
2018-05-15 16:27:10 +02:00
|
|
|
|
protected virtual void ConfigureEventBus(IApplicationBuilder app)
|
|
|
|
|
{
|
|
|
|
|
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
|
|
|
|
|
eventBus.Subscribe<OrderStatusChangedToAwaitingValidationIntegrationEvent, OrderStatusChangedToAwaitingValidationIntegrationEventHandler>();
|
|
|
|
|
eventBus.Subscribe<OrderStatusChangedToPaidIntegrationEvent, OrderStatusChangedToPaidIntegrationEventHandler>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class CustomExtensionMethods
|
|
|
|
|
{
|
|
|
|
|
public static IServiceCollection AddAppInsight(this IServiceCollection services, IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
services.AddApplicationInsightsTelemetry(configuration);
|
2019-07-23 12:14:09 +02:00
|
|
|
|
services.AddApplicationInsightsKubernetesEnricher();
|
2018-05-15 16:27:10 +02:00
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IServiceCollection AddCustomMVC(this IServiceCollection services, IConfiguration configuration)
|
2019-06-26 12:58:07 +02:00
|
|
|
|
{
|
2019-07-26 11:11:31 +02:00
|
|
|
|
services.AddControllers(options =>
|
2019-07-26 09:42:43 +02:00
|
|
|
|
{
|
|
|
|
|
options.Filters.Add(typeof(HttpGlobalExceptionFilter));
|
2021-05-03 16:36:31 +05:30
|
|
|
|
})
|
|
|
|
|
.AddJsonOptions(options => options.JsonSerializerOptions.WriteIndented = true);
|
2017-03-27 14:05:28 +02:00
|
|
|
|
|
2018-05-15 16:27:10 +02:00
|
|
|
|
services.AddCors(options =>
|
|
|
|
|
{
|
|
|
|
|
options.AddPolicy("CorsPolicy",
|
2019-01-04 13:39:25 +01:00
|
|
|
|
builder => builder
|
|
|
|
|
.SetIsOriginAllowed((host) => true)
|
2018-05-15 16:27:10 +02:00
|
|
|
|
.AllowAnyMethod()
|
|
|
|
|
.AllowAnyHeader()
|
|
|
|
|
.AllowCredentials());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-30 17:43:22 +01:00
|
|
|
|
public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services, IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
var accountName = configuration.GetValue<string>("AzureStorageAccountName");
|
|
|
|
|
var accountKey = configuration.GetValue<string>("AzureStorageAccountKey");
|
|
|
|
|
|
|
|
|
|
var hcBuilder = services.AddHealthChecks();
|
|
|
|
|
|
|
|
|
|
hcBuilder
|
2019-01-03 17:11:56 +01:00
|
|
|
|
.AddCheck("self", () => HealthCheckResult.Healthy())
|
2018-11-30 17:43:22 +01:00
|
|
|
|
.AddSqlServer(
|
|
|
|
|
configuration["ConnectionString"],
|
|
|
|
|
name: "CatalogDB-check",
|
|
|
|
|
tags: new string[] { "catalogdb" });
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(accountName) && !string.IsNullOrEmpty(accountKey))
|
2019-06-26 12:58:07 +02:00
|
|
|
|
{
|
2018-11-30 17:43:22 +01:00
|
|
|
|
hcBuilder
|
|
|
|
|
.AddAzureBlobStorage(
|
|
|
|
|
$"DefaultEndpointsProtocol=https;AccountName={accountName};AccountKey={accountKey};EndpointSuffix=core.windows.net",
|
|
|
|
|
name: "catalog-storage-check",
|
|
|
|
|
tags: new string[] { "catalogstorage" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (configuration.GetValue<bool>("AzureServiceBusEnabled"))
|
|
|
|
|
{
|
|
|
|
|
hcBuilder
|
|
|
|
|
.AddAzureServiceBusTopic(
|
|
|
|
|
configuration["EventBusConnection"],
|
|
|
|
|
topicName: "eshop_event_bus",
|
|
|
|
|
name: "catalog-servicebus-check",
|
|
|
|
|
tags: new string[] { "servicebus" });
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
hcBuilder
|
|
|
|
|
.AddRabbitMQ(
|
|
|
|
|
$"amqp://{configuration["EventBusConnection"]}",
|
|
|
|
|
name: "catalog-rabbitmqbus-check",
|
|
|
|
|
tags: new string[] { "rabbitmqbus" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-15 16:27:10 +02:00
|
|
|
|
public static IServiceCollection AddCustomDbContext(this IServiceCollection services, IConfiguration configuration)
|
|
|
|
|
{
|
2019-07-24 12:15:38 +02:00
|
|
|
|
services.AddEntityFrameworkSqlServer()
|
|
|
|
|
.AddDbContext<CatalogContext>(options =>
|
2016-10-31 16:54:55 +01:00
|
|
|
|
{
|
2018-05-15 16:27:10 +02:00
|
|
|
|
options.UseSqlServer(configuration["ConnectionString"],
|
2017-03-26 18:00:04 -07:00
|
|
|
|
sqlServerOptionsAction: sqlOptions =>
|
2017-04-17 12:28:12 +02:00
|
|
|
|
{
|
2017-03-26 18:00:04 -07:00
|
|
|
|
sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
|
|
|
|
|
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
|
2019-07-24 12:15:38 +02:00
|
|
|
|
sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
|
2017-03-26 18:00:04 -07:00
|
|
|
|
});
|
2016-09-14 08:35:34 -07:00
|
|
|
|
});
|
|
|
|
|
|
2017-10-11 10:00:39 -07:00
|
|
|
|
services.AddDbContext<IntegrationEventLogContext>(options =>
|
|
|
|
|
{
|
2018-05-15 16:27:10 +02:00
|
|
|
|
options.UseSqlServer(configuration["ConnectionString"],
|
2017-10-11 10:00:39 -07:00
|
|
|
|
sqlServerOptionsAction: sqlOptions =>
|
|
|
|
|
{
|
|
|
|
|
sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
|
|
|
|
|
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
|
2019-07-24 12:15:38 +02:00
|
|
|
|
sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
|
2017-10-11 10:00:39 -07:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2018-05-15 16:27:10 +02:00
|
|
|
|
return services;
|
|
|
|
|
}
|
2017-01-09 10:33:43 +01:00
|
|
|
|
|
2018-05-15 16:27:10 +02:00
|
|
|
|
public static IServiceCollection AddCustomOptions(this IServiceCollection services, IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
services.Configure<CatalogSettings>(configuration);
|
|
|
|
|
services.Configure<ApiBehaviorOptions>(options =>
|
|
|
|
|
{
|
|
|
|
|
options.InvalidModelStateResponseFactory = context =>
|
|
|
|
|
{
|
|
|
|
|
var problemDetails = new ValidationProblemDetails(context.ModelState)
|
|
|
|
|
{
|
|
|
|
|
Instance = context.HttpContext.Request.Path,
|
|
|
|
|
Status = StatusCodes.Status400BadRequest,
|
|
|
|
|
Detail = "Please refer to the errors property for additional details."
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return new BadRequestObjectResult(problemDetails)
|
|
|
|
|
{
|
|
|
|
|
ContentTypes = { "application/problem+json", "application/problem+xml" }
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-26 11:11:31 +02:00
|
|
|
|
public static IServiceCollection AddSwagger(this IServiceCollection services, IConfiguration configuration)
|
2018-05-15 16:27:10 +02:00
|
|
|
|
{
|
2017-05-18 21:50:51 +03:00
|
|
|
|
services.AddSwaggerGen(options =>
|
2016-11-03 17:52:15 +01:00
|
|
|
|
{
|
|
|
|
|
options.DescribeAllEnumsAsStrings();
|
2019-07-29 13:21:00 +02:00
|
|
|
|
options.SwaggerDoc("v1", new OpenApiInfo
|
2016-11-03 17:52:15 +01:00
|
|
|
|
{
|
2016-11-30 15:01:14 -08:00
|
|
|
|
Title = "eShopOnContainers - Catalog HTTP API",
|
2016-11-03 17:52:15 +01:00
|
|
|
|
Version = "v1",
|
2019-06-26 12:58:07 +02:00
|
|
|
|
Description = "The Catalog Microservice HTTP API. This is a Data-Driven/CRUD microservice sample"
|
2016-11-03 17:52:15 +01:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2018-05-15 16:27:10 +02:00
|
|
|
|
return services;
|
|
|
|
|
|
|
|
|
|
}
|
2016-10-31 16:54:55 +01:00
|
|
|
|
|
2018-05-16 14:45:15 +02:00
|
|
|
|
public static IServiceCollection AddIntegrationServices(this IServiceCollection services, IConfiguration configuration)
|
2018-05-15 16:27:10 +02:00
|
|
|
|
{
|
2017-03-24 12:37:44 +01:00
|
|
|
|
services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>(
|
2019-08-07 17:06:20 +02:00
|
|
|
|
sp => (DbConnection c) => new IntegrationEventLogService(c));
|
2017-04-17 12:28:12 +02:00
|
|
|
|
|
2017-04-03 13:13:40 +02:00
|
|
|
|
services.AddTransient<ICatalogIntegrationEventService, CatalogIntegrationEventService>();
|
2017-04-17 12:28:12 +02:00
|
|
|
|
|
2018-05-15 16:27:10 +02:00
|
|
|
|
if (configuration.GetValue<bool>("AzureServiceBusEnabled"))
|
2017-04-17 12:28:12 +02:00
|
|
|
|
{
|
2017-05-24 15:33:05 +02:00
|
|
|
|
services.AddSingleton<IServiceBusPersisterConnection>(sp =>
|
2017-04-20 10:53:17 +02:00
|
|
|
|
{
|
2017-05-24 15:33:05 +02:00
|
|
|
|
var settings = sp.GetRequiredService<IOptions<CatalogSettings>>().Value;
|
2017-05-30 17:59:57 +02:00
|
|
|
|
var serviceBusConnection = new ServiceBusConnectionStringBuilder(settings.EventBusConnection);
|
2021-03-09 12:02:28 +02:00
|
|
|
|
var subscriptionClientName = configuration["SubscriptionClientName"];
|
2017-04-20 10:53:17 +02:00
|
|
|
|
|
2021-03-09 12:02:28 +02:00
|
|
|
|
return new DefaultServiceBusPersisterConnection(serviceBusConnection, subscriptionClientName);
|
2017-05-24 15:33:05 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
|
|
|
|
|
{
|
|
|
|
|
var settings = sp.GetRequiredService<IOptions<CatalogSettings>>().Value;
|
|
|
|
|
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
|
2017-09-05 15:55:17 +02:00
|
|
|
|
|
2017-05-24 15:33:05 +02:00
|
|
|
|
var factory = new ConnectionFactory()
|
|
|
|
|
{
|
2019-04-02 15:36:20 +01:00
|
|
|
|
HostName = configuration["EventBusConnection"],
|
|
|
|
|
DispatchConsumersAsync = true
|
2017-05-24 15:33:05 +02:00
|
|
|
|
};
|
|
|
|
|
|
2018-05-15 16:27:10 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(configuration["EventBusUserName"]))
|
2017-09-05 15:55:17 +02:00
|
|
|
|
{
|
2018-05-15 16:27:10 +02:00
|
|
|
|
factory.UserName = configuration["EventBusUserName"];
|
2017-09-05 15:55:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-15 16:27:10 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(configuration["EventBusPassword"]))
|
2017-09-05 15:55:17 +02:00
|
|
|
|
{
|
2018-05-15 16:27:10 +02:00
|
|
|
|
factory.Password = configuration["EventBusPassword"];
|
2017-09-05 15:55:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-12 09:25:01 +01:00
|
|
|
|
var retryCount = 5;
|
2018-05-15 16:27:10 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"]))
|
2017-10-12 09:25:01 +01:00
|
|
|
|
{
|
2018-05-15 16:27:10 +02:00
|
|
|
|
retryCount = int.Parse(configuration["EventBusRetryCount"]);
|
2017-10-12 09:25:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount);
|
2017-05-24 15:33:05 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
2017-04-20 10:53:17 +02:00
|
|
|
|
|
2018-05-15 16:27:10 +02:00
|
|
|
|
return services;
|
2016-09-06 17:09:19 -07:00
|
|
|
|
}
|
2017-05-15 19:05:47 +02:00
|
|
|
|
|
2018-05-15 16:27:10 +02:00
|
|
|
|
public static IServiceCollection AddEventBus(this IServiceCollection services, IConfiguration configuration)
|
2017-10-13 11:35:26 +02:00
|
|
|
|
{
|
2018-05-15 16:27:10 +02:00
|
|
|
|
if (configuration.GetValue<bool>("AzureServiceBusEnabled"))
|
2017-05-24 15:33:05 +02:00
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<IEventBus, EventBusServiceBus>(sp =>
|
|
|
|
|
{
|
|
|
|
|
var serviceBusPersisterConnection = sp.GetRequiredService<IServiceBusPersisterConnection>();
|
2017-06-26 18:05:02 +02:00
|
|
|
|
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
|
2017-05-24 15:33:05 +02:00
|
|
|
|
var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>();
|
|
|
|
|
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
|
|
|
|
|
|
|
|
|
|
return new EventBusServiceBus(serviceBusPersisterConnection, logger,
|
2021-03-09 12:02:28 +02:00
|
|
|
|
eventBusSubcriptionsManager, iLifetimeScope);
|
2017-05-24 15:33:05 +02:00
|
|
|
|
});
|
2017-05-17 00:40:40 +02:00
|
|
|
|
|
2017-05-24 15:33:05 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-10-12 09:25:01 +01:00
|
|
|
|
services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp =>
|
|
|
|
|
{
|
2021-03-09 12:02:28 +02:00
|
|
|
|
var subscriptionClientName = configuration["SubscriptionClientName"];
|
2017-10-12 09:25:01 +01:00
|
|
|
|
var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>();
|
|
|
|
|
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
|
|
|
|
|
var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>();
|
|
|
|
|
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
|
|
|
|
|
|
|
|
|
|
var retryCount = 5;
|
2018-05-15 16:27:10 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"]))
|
2017-10-12 09:25:01 +01:00
|
|
|
|
{
|
2018-05-15 16:27:10 +02:00
|
|
|
|
retryCount = int.Parse(configuration["EventBusRetryCount"]);
|
2017-10-12 09:25:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-10 17:37:18 +01:00
|
|
|
|
return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount);
|
2017-10-12 09:25:01 +01:00
|
|
|
|
});
|
2017-05-24 15:33:05 +02:00
|
|
|
|
}
|
2017-05-17 00:40:40 +02:00
|
|
|
|
|
2017-05-24 15:33:05 +02:00
|
|
|
|
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
2017-06-30 16:31:22 +02:00
|
|
|
|
services.AddTransient<OrderStatusChangedToAwaitingValidationIntegrationEventHandler>();
|
|
|
|
|
services.AddTransient<OrderStatusChangedToPaidIntegrationEventHandler>();
|
2018-05-15 16:27:10 +02:00
|
|
|
|
|
|
|
|
|
return services;
|
2017-05-15 19:05:47 +02:00
|
|
|
|
}
|
2016-09-06 17:09:19 -07:00
|
|
|
|
}
|
|
|
|
|
}
|