205 lines
9.2 KiB
C#
Raw Normal View History

namespace Microsoft.eShopOnContainers.Services.Ordering.API
{
2016-12-22 13:20:12 +01:00
using AspNetCore.Http;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using global::Ordering.API.Application.IntegrationCommands.Commands;
using global::Ordering.API.Application.IntegrationEvents;
using global::Ordering.API.Application.IntegrationEvents.Events;
using global::Ordering.API.Application.Sagas;
using global::Ordering.API.Infrastructure.Middlewares;
using global::Ordering.API.Application.IntegrationCommands.Commands;
using global::Ordering.API.Application.Sagas;
using Infrastructure;
using Infrastructure.Auth;
using Infrastructure.AutofacModules;
using Infrastructure.Filters;
2016-12-22 13:20:12 +01:00
using Infrastructure.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF;
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
2017-03-23 19:10:55 +01:00
using Microsoft.Extensions.HealthChecks;
using Microsoft.Extensions.Logging;
2016-11-22 18:40:47 +01:00
using Ordering.Infrastructure;
using RabbitMQ.Client;
using System;
using System.Data.Common;
2016-11-22 18:40:47 +01:00
using System.Reflection;
using global::Ordering.API.Application.IntegrationEvents.EventHandling;
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
2016-11-22 18:40:47 +01:00
.AddJsonFile("settings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"settings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
2017-03-03 12:03:31 +01:00
builder.AddUserSecrets(typeof(Startup).GetTypeInfo().Assembly);
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
public IServiceProvider ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc(options =>
{
options.Filters.Add(typeof(HttpGlobalExceptionFilter));
}).AddControllersAsServices(); //Injecting Controllers themselves thru DI
//For further info see: http://docs.autofac.org/en/latest/integration/aspnetcore.html#controllers-as-services
2017-03-23 19:10:55 +01:00
services.AddHealthChecks(checks =>
{
2017-03-31 12:47:56 +02:00
checks.AddSqlCheck("OrderingDb", Configuration["ConnectionString"]);
2017-03-23 19:10:55 +01:00
});
2016-11-22 18:40:47 +01:00
services.AddEntityFrameworkSqlServer()
2017-01-26 15:08:51 -08:00
.AddDbContext<OrderingContext>(options =>
{
2017-04-17 12:28:12 +02:00
options.UseSqlServer(Configuration["ConnectionString"],
sqlServerOptionsAction: sqlOptions =>
{
sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
sqlOptions.EnableRetryOnFailure(maxRetryCount: 5, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
});
},
ServiceLifetime.Scoped //Showing explicitly that the DbContext is shared across the HTTP request scope (graph of objects started in the HTTP request)
2017-01-26 15:08:51 -08:00
);
2016-11-22 18:40:47 +01:00
services.AddSwaggerGen();
services.ConfigureSwaggerGen(options =>
{
options.OperationFilter<AuthorizationHeaderParameterOperationFilter>();
options.DescribeAllEnumsAsStrings();
options.SingleApiVersion(new Swashbuckle.Swagger.Model.Info()
{
Title = "Ordering HTTP API",
Version = "v1",
Description = "The Ordering Service HTTP API",
2017-03-03 12:03:31 +01:00
TermsOfService = "Terms Of Service"
});
});
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
2016-12-22 13:20:12 +01:00
// Add application services.
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
2017-03-03 12:03:31 +01:00
services.AddTransient<IIdentityService, IdentityService>();
services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>(
sp => (DbConnection c) => new IntegrationEventLogService(c));
services.AddTransient<IOrderingIntegrationEventService, OrderingIntegrationEventService>();
2017-04-29 21:58:11 -07:00
services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
{
2017-04-29 21:58:11 -07:00
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
var factory = new ConnectionFactory()
{
HostName = Configuration["EventBusConnection"]
};
2017-04-29 21:58:11 -07:00
return new DefaultRabbitMQPersistentConnection(factory, logger);
});
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
services.AddTransient<IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>>();
services.AddTransient<IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>, OrderProcessSaga>();
services.AddTransient<IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>,
OrderStockConfirmedIntegrationEventHandler>();
services.AddTransient<IIntegrationEventHandler<OrderStockNotConfirmedIntegrationEvent>,
OrderStockNotConfirmedIntegrationEventHandler>();
services.AddTransient<IIntegrationEventHandler<OrderPaymentFailedIntegrationEvent>,
OrderPaymentFailedIntegrationEventHandler>();
services.AddTransient<IIntegrationEventHandler<OrderPaymentSuccededIntegrationEvent>,
OrderPaymentSuccededIntegrationEventHandler>();
services.AddOptions();
//configure autofac
var container = new ContainerBuilder();
container.Populate(services);
container.RegisterModule(new MediatorModule());
2017-03-03 12:03:31 +01:00
container.RegisterModule(new ApplicationModule(Configuration["ConnectionString"]));
return new AutofacServiceProvider(container.Build());
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseCors("CorsPolicy");
2017-03-17 16:28:05 +01:00
app.UseFailingMiddleware();
ConfigureAuth(app);
app.UseMvcWithDefaultRoute();
app.UseSwagger()
.UseSwaggerUi();
OrderingContextSeed.SeedAsync(app).Wait();
ConfigureEventBus(app);
var integrationEventLogContext = new IntegrationEventLogContext(
new DbContextOptionsBuilder<IntegrationEventLogContext>()
.UseSqlServer(Configuration["ConnectionString"], b => b.MigrationsAssembly("Ordering.API"))
.Options);
integrationEventLogContext.Database.Migrate();
2017-05-08 10:48:06 +02:00
}
private void ConfigureEventBus(IApplicationBuilder app)
{
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
eventBus.Subscribe<UserCheckoutAcceptedIntegrationEvent, IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>>();
2017-05-11 16:33:11 +02:00
eventBus.Subscribe<ConfirmGracePeriodCommandMsg, IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>>();
eventBus.Subscribe<OrderStockConfirmedIntegrationEvent, IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>>();
eventBus.Subscribe<OrderStockNotConfirmedIntegrationEvent, IIntegrationEventHandler<OrderStockNotConfirmedIntegrationEvent>>();
eventBus.Subscribe<OrderPaymentFailedIntegrationEvent, IIntegrationEventHandler<OrderPaymentFailedIntegrationEvent>>();
eventBus.Subscribe<OrderPaymentSuccededIntegrationEvent, IIntegrationEventHandler<OrderPaymentSuccededIntegrationEvent>>();
}
protected virtual void ConfigureAuth(IApplicationBuilder app)
{
var identityUrl = Configuration.GetValue<string>("IdentityUrl");
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
Authority = identityUrl.ToString(),
ScopeName = "orders",
RequireHttpsMetadata = false
});
}
}
}