2016-11-03 17:52:15 +01:00
|
|
|
|
namespace Microsoft.eShopOnContainers.Services.Catalog.API
|
2016-09-06 17:09:19 -07:00
|
|
|
|
{
|
2017-05-11 13:44:38 +02:00
|
|
|
|
using Autofac;
|
|
|
|
|
using Autofac.Extensions.DependencyInjection;
|
2017-03-27 14:05:28 +02:00
|
|
|
|
using global::Catalog.API.Infrastructure.Filters;
|
2017-04-17 12:28:12 +02:00
|
|
|
|
using global::Catalog.API.IntegrationEvents;
|
2016-11-03 17:52:15 +01:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2017-05-24 15:33:05 +02:00
|
|
|
|
using Microsoft.Azure.ServiceBus;
|
2016-11-03 17:52:15 +01:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2017-06-29 18:36:57 +02:00
|
|
|
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
2017-05-03 10:59:36 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
|
2017-03-16 13:30:01 +01:00
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
|
2017-05-24 15:33:05 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus;
|
2017-10-11 10:10:52 -07:00
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF;
|
2017-03-24 12:37:44 +01:00
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services;
|
2016-11-03 17:52:15 +01:00
|
|
|
|
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure;
|
2017-05-18 11:43:19 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.EventHandling;
|
|
|
|
|
using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events;
|
2016-11-03 17:52:15 +01:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2017-03-23 19:10:55 +01:00
|
|
|
|
using Microsoft.Extensions.HealthChecks;
|
2016-11-03 17:52:15 +01:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2017-03-13 11:03:35 +01:00
|
|
|
|
using Microsoft.Extensions.Options;
|
2017-04-20 10:53:17 +02:00
|
|
|
|
using RabbitMQ.Client;
|
2016-12-07 13:57:31 +01:00
|
|
|
|
using System;
|
2017-03-24 12:37:44 +01:00
|
|
|
|
using System.Data.Common;
|
2017-03-03 12:03:31 +01:00
|
|
|
|
using System.Reflection;
|
2016-11-03 17:52:15 +01:00
|
|
|
|
|
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
|
|
|
|
|
2017-05-11 13:44:38 +02:00
|
|
|
|
public IServiceProvider ConfigureServices(IServiceCollection services)
|
2016-09-06 17:09:19 -07:00
|
|
|
|
{
|
2017-03-27 14:05:28 +02:00
|
|
|
|
// Add framework services.
|
2017-04-17 12:28:12 +02:00
|
|
|
|
|
2017-10-11 16:26:44 +02:00
|
|
|
|
services.AddApplicationInsightsTelemetry(Configuration);
|
|
|
|
|
|
2017-10-11 18:53:26 +02:00
|
|
|
|
if (Configuration.GetValue<string>("OrchestratorType").Equals("K8S"))
|
|
|
|
|
{
|
|
|
|
|
// Enable K8s telemetry initializer
|
|
|
|
|
services.EnableKubernetes();
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-23 19:10:55 +01:00
|
|
|
|
services.AddHealthChecks(checks =>
|
|
|
|
|
{
|
2017-05-09 13:54:45 +02:00
|
|
|
|
var minutes = 1;
|
|
|
|
|
if (int.TryParse(Configuration["HealthCheck:Timeout"], out var minutesParsed))
|
|
|
|
|
{
|
|
|
|
|
minutes = minutesParsed;
|
|
|
|
|
}
|
|
|
|
|
checks.AddSqlCheck("CatalogDb", Configuration["ConnectionString"], TimeSpan.FromMinutes(minutes));
|
2017-07-06 11:38:45 +02:00
|
|
|
|
|
|
|
|
|
var accountName = Configuration.GetValue<string>("AzureStorageAccountName");
|
|
|
|
|
var accountKey = Configuration.GetValue<string>("AzureStorageAccountKey");
|
|
|
|
|
if (!string.IsNullOrEmpty(accountName) && !string.IsNullOrEmpty(accountKey))
|
|
|
|
|
{
|
|
|
|
|
checks.AddAzureBlobStorageCheck(accountName, accountKey);
|
|
|
|
|
}
|
2017-03-23 19:10:55 +01:00
|
|
|
|
});
|
|
|
|
|
|
2017-03-28 16:37:48 +02:00
|
|
|
|
services.AddMvc(options =>
|
2016-10-31 16:54:55 +01:00
|
|
|
|
{
|
2017-03-27 14:05:28 +02:00
|
|
|
|
options.Filters.Add(typeof(HttpGlobalExceptionFilter));
|
|
|
|
|
}).AddControllersAsServices();
|
|
|
|
|
|
2017-03-27 15:24:29 +02:00
|
|
|
|
services.AddDbContext<CatalogContext>(options =>
|
2016-10-31 16:54:55 +01:00
|
|
|
|
{
|
2017-03-27 10:18:41 +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
|
2017-10-10 19:56:34 -07:00
|
|
|
|
sqlOptions.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
|
2017-03-26 18:00:04 -07:00
|
|
|
|
});
|
2017-04-17 12:28:12 +02:00
|
|
|
|
|
2017-03-04 17:29:36 -08:00
|
|
|
|
// Changing default behavior when client evaluation occurs to throw.
|
|
|
|
|
// Default in EF Core would be to log a warning when client evaluation is performed.
|
2017-03-26 18:00:04 -07:00
|
|
|
|
options.ConfigureWarnings(warnings => warnings.Throw(RelationalEventId.QueryClientEvaluationWarning));
|
2017-03-04 17:29:36 -08:00
|
|
|
|
//Check Client vs. Server evaluation: https://docs.microsoft.com/en-us/ef/core/querying/client-eval
|
2016-09-14 08:35:34 -07:00
|
|
|
|
});
|
|
|
|
|
|
2017-10-11 10:00:39 -07:00
|
|
|
|
services.AddDbContext<IntegrationEventLogContext>(options =>
|
|
|
|
|
{
|
|
|
|
|
options.UseSqlServer(Configuration["ConnectionString"],
|
|
|
|
|
sqlServerOptionsAction: sqlOptions =>
|
|
|
|
|
{
|
|
|
|
|
sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
|
|
|
|
|
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
|
|
|
|
|
sqlOptions.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2017-04-17 12:28:12 +02:00
|
|
|
|
services.Configure<CatalogSettings>(Configuration);
|
2017-01-09 10:33:43 +01:00
|
|
|
|
|
2016-09-06 17:09:19 -07:00
|
|
|
|
// Add framework services.
|
2017-05-18 21:50:51 +03:00
|
|
|
|
services.AddSwaggerGen(options =>
|
2016-11-03 17:52:15 +01:00
|
|
|
|
{
|
|
|
|
|
options.DescribeAllEnumsAsStrings();
|
2017-06-01 14:32:22 +02:00
|
|
|
|
options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info
|
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",
|
2016-11-30 15:01:14 -08:00
|
|
|
|
Description = "The Catalog Microservice HTTP API. This is a Data-Driven/CRUD microservice sample",
|
2016-11-14 15:48:30 +01:00
|
|
|
|
TermsOfService = "Terms Of Service"
|
2016-11-03 17:52:15 +01:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-11-23 09:11:09 +01:00
|
|
|
|
services.AddCors(options =>
|
|
|
|
|
{
|
|
|
|
|
options.AddPolicy("CorsPolicy",
|
|
|
|
|
builder => builder.AllowAnyOrigin()
|
|
|
|
|
.AllowAnyMethod()
|
|
|
|
|
.AllowAnyHeader()
|
|
|
|
|
.AllowCredentials());
|
|
|
|
|
});
|
2016-10-31 16:54:55 +01:00
|
|
|
|
|
2017-03-24 12:37:44 +01:00
|
|
|
|
services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>(
|
2017-04-17 12:28:12 +02:00
|
|
|
|
sp => (DbConnection c) => new IntegrationEventLogService(c));
|
|
|
|
|
|
2017-04-03 13:13:40 +02:00
|
|
|
|
services.AddTransient<ICatalogIntegrationEventService, CatalogIntegrationEventService>();
|
2017-04-17 12:28:12 +02:00
|
|
|
|
|
2017-05-24 19:23:14 +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;
|
|
|
|
|
var logger = sp.GetRequiredService<ILogger<DefaultServiceBusPersisterConnection>>();
|
2017-04-20 10:53:17 +02:00
|
|
|
|
|
2017-05-30 17:59:57 +02:00
|
|
|
|
var serviceBusConnection = new ServiceBusConnectionStringBuilder(settings.EventBusConnection);
|
2017-04-20 10:53:17 +02:00
|
|
|
|
|
2017-05-24 18:59:29 +02:00
|
|
|
|
return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger);
|
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()
|
|
|
|
|
{
|
2017-09-05 15:55:17 +02:00
|
|
|
|
HostName = Configuration["EventBusConnection"]
|
2017-05-24 15:33:05 +02:00
|
|
|
|
};
|
|
|
|
|
|
2017-09-05 15:55:17 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(Configuration["EventBusUserName"]))
|
|
|
|
|
{
|
|
|
|
|
factory.UserName = Configuration["EventBusUserName"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(Configuration["EventBusPassword"]))
|
|
|
|
|
{
|
|
|
|
|
factory.Password = Configuration["EventBusPassword"];
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-12 09:25:01 +01:00
|
|
|
|
var retryCount = 5;
|
|
|
|
|
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
|
|
|
|
|
{
|
|
|
|
|
retryCount = int.Parse(Configuration["EventBusRetryCount"]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount);
|
2017-05-24 15:33:05 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
2017-04-20 10:53:17 +02:00
|
|
|
|
|
2017-05-26 01:35:44 +02:00
|
|
|
|
RegisterEventBus(services);
|
2017-05-11 13:44:38 +02:00
|
|
|
|
|
|
|
|
|
var container = new ContainerBuilder();
|
|
|
|
|
container.Populate(services);
|
|
|
|
|
return new AutofacServiceProvider(container.Build());
|
2017-07-06 11:38:45 +02:00
|
|
|
|
|
2016-09-06 17:09:19 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
|
|
|
|
{
|
2016-10-31 16:54:55 +01:00
|
|
|
|
//Configure logs
|
|
|
|
|
|
2016-09-06 17:09:19 -07:00
|
|
|
|
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
|
|
|
|
|
loggerFactory.AddDebug();
|
2017-10-11 16:26:44 +02:00
|
|
|
|
loggerFactory.AddAzureWebAppDiagnostics();
|
|
|
|
|
loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Trace);
|
2016-09-06 17:09:19 -07:00
|
|
|
|
|
2017-09-07 19:18:53 +02:00
|
|
|
|
var pathBase = Configuration["PATH_BASE"];
|
|
|
|
|
if (!string.IsNullOrEmpty(pathBase))
|
|
|
|
|
{
|
|
|
|
|
loggerFactory.CreateLogger("init").LogDebug($"Using PATH BASE '{pathBase}'");
|
|
|
|
|
app.UsePathBase(pathBase);
|
|
|
|
|
}
|
2017-10-12 09:25:01 +01:00
|
|
|
|
|
2016-11-23 09:11:09 +01:00
|
|
|
|
app.UseCors("CorsPolicy");
|
2016-10-31 16:54:55 +01:00
|
|
|
|
|
2016-12-07 13:57:31 +01:00
|
|
|
|
app.UseMvcWithDefaultRoute();
|
2016-11-03 17:52:15 +01:00
|
|
|
|
|
|
|
|
|
app.UseSwagger()
|
2017-05-18 21:50:51 +03:00
|
|
|
|
.UseSwaggerUI(c =>
|
|
|
|
|
{
|
|
|
|
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
|
|
|
|
|
});
|
2016-12-07 13:57:31 +01:00
|
|
|
|
|
2017-05-15 19:05:47 +02:00
|
|
|
|
ConfigureEventBus(app);
|
2016-09-06 17:09:19 -07:00
|
|
|
|
}
|
2017-05-15 19:05:47 +02:00
|
|
|
|
|
2017-05-26 01:35:44 +02:00
|
|
|
|
private void RegisterEventBus(IServiceCollection services)
|
2017-05-17 00:40:40 +02:00
|
|
|
|
{
|
2017-05-24 19:23:14 +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>();
|
2017-05-24 18:59:29 +02:00
|
|
|
|
var subscriptionClientName = Configuration["SubscriptionClientName"];
|
2017-05-24 15:33:05 +02:00
|
|
|
|
|
|
|
|
|
return new EventBusServiceBus(serviceBusPersisterConnection, logger,
|
2017-06-26 18:05:02 +02:00
|
|
|
|
eventBusSubcriptionsManager, subscriptionClientName, 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 =>
|
|
|
|
|
{
|
|
|
|
|
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, retryCount);
|
|
|
|
|
});
|
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>();
|
2017-05-17 00:40:40 +02:00
|
|
|
|
}
|
2017-05-26 01:35:44 +02:00
|
|
|
|
protected virtual void ConfigureEventBus(IApplicationBuilder app)
|
2017-05-15 19:05:47 +02:00
|
|
|
|
{
|
|
|
|
|
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
|
2017-06-30 16:31:22 +02:00
|
|
|
|
eventBus.Subscribe<OrderStatusChangedToAwaitingValidationIntegrationEvent, OrderStatusChangedToAwaitingValidationIntegrationEventHandler>();
|
|
|
|
|
eventBus.Subscribe<OrderStatusChangedToPaidIntegrationEvent, OrderStatusChangedToPaidIntegrationEventHandler>();
|
2017-05-15 19:05:47 +02:00
|
|
|
|
}
|
2016-09-06 17:09:19 -07:00
|
|
|
|
}
|
|
|
|
|
}
|