# Conflicts: # eShopOnContainers-ServicesAndWebApps.sln # src/Services/Basket/Basket.API/Controllers/BasketController.cs # src/Services/Basket/Basket.API/Startup.cs
201 lines
8.0 KiB
C#
201 lines
8.0 KiB
C#
|
|
|
|
using Ordering.API.Application.IntegrationCommands.Commands;
|
|
using Ordering.API.Application.IntegrationEvents.EventHandling;
|
|
using Ordering.API.Application.Sagas;
|
|
|
|
namespace Microsoft.eShopOnContainers.Services.Ordering.API
|
|
{
|
|
using AspNetCore.Http;
|
|
using Autofac;
|
|
using Autofac.Extensions.DependencyInjection;
|
|
using global::Ordering.API.Application.IntegrationEvents;
|
|
using global::Ordering.API.Application.IntegrationEvents.EventHandling;
|
|
using global::Ordering.API.Infrastructure.Middlewares;
|
|
using Infrastructure;
|
|
using Infrastructure.Auth;
|
|
using Infrastructure.AutofacModules;
|
|
using Infrastructure.Filters;
|
|
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;
|
|
using Microsoft.Extensions.HealthChecks;
|
|
using Microsoft.Extensions.Logging;
|
|
using Ordering.Infrastructure;
|
|
using RabbitMQ.Client;
|
|
using System;
|
|
using System.Data.Common;
|
|
using System.Reflection;
|
|
|
|
public class Startup
|
|
{
|
|
public Startup(IHostingEnvironment env)
|
|
{
|
|
var builder = new ConfigurationBuilder()
|
|
.SetBasePath(env.ContentRootPath)
|
|
.AddJsonFile("settings.json", optional: true, reloadOnChange: true)
|
|
.AddJsonFile($"settings.{env.EnvironmentName}.json", optional: true);
|
|
|
|
if (env.IsDevelopment())
|
|
{
|
|
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
|
|
|
|
|
|
services.AddHealthChecks(checks =>
|
|
{
|
|
checks.AddSqlCheck("OrderingDb", Configuration["ConnectionString"]);
|
|
});
|
|
|
|
services.AddEntityFrameworkSqlServer()
|
|
.AddDbContext<OrderingContext>(options =>
|
|
{
|
|
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)
|
|
);
|
|
|
|
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",
|
|
TermsOfService = "Terms Of Service"
|
|
});
|
|
});
|
|
|
|
services.AddCors(options =>
|
|
{
|
|
options.AddPolicy("CorsPolicy",
|
|
builder => builder.AllowAnyOrigin()
|
|
.AllowAnyMethod()
|
|
.AllowAnyHeader()
|
|
.AllowCredentials());
|
|
});
|
|
|
|
// Add application services.
|
|
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
|
services.AddTransient<IIdentityService, IdentityService>();
|
|
services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>(
|
|
sp => (DbConnection c) => new IntegrationEventLogService(c));
|
|
var serviceProvider = services.BuildServiceProvider();
|
|
services.AddTransient<IOrderingIntegrationEventService, OrderingIntegrationEventService>();
|
|
|
|
services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
|
|
{
|
|
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
|
|
|
|
var factory = new ConnectionFactory()
|
|
{
|
|
HostName = Configuration["EventBusConnection"]
|
|
};
|
|
|
|
return new DefaultRabbitMQPersistentConnection(factory, logger);
|
|
});
|
|
|
|
|
|
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
|
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
|
|
services.AddTransient<UserCheckoutAcceptedIntegrationEventHandler>();
|
|
services.AddOptions();
|
|
|
|
//configure autofac
|
|
|
|
var container = new ContainerBuilder();
|
|
container.Populate(services);
|
|
|
|
container.RegisterModule(new MediatorModule());
|
|
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");
|
|
|
|
app.UseFailingMiddleware();
|
|
|
|
ConfigureAuth(app);
|
|
ConfigureEventBus(app);
|
|
|
|
app.UseMvcWithDefaultRoute();
|
|
|
|
app.UseSwagger()
|
|
.UseSwaggerUi();
|
|
|
|
OrderingContextSeed.SeedAsync(app).Wait();
|
|
|
|
var integrationEventLogContext = new IntegrationEventLogContext(
|
|
new DbContextOptionsBuilder<IntegrationEventLogContext>()
|
|
.UseSqlServer(Configuration["ConnectionString"], b => b.MigrationsAssembly("Ordering.API"))
|
|
.Options);
|
|
integrationEventLogContext.Database.Migrate();
|
|
|
|
}
|
|
|
|
private void ConfigureEventBus(IApplicationBuilder app)
|
|
{
|
|
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
|
|
eventBus.SubscribeDynamic(
|
|
"UserCheckoutAccepted",
|
|
() => app.ApplicationServices.GetRequiredService<UserCheckoutAcceptedIntegrationEventHandler>());
|
|
|
|
eventBus.Subscribe<ConfirmGracePeriodCommandMsg, IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>>(
|
|
() => app.ApplicationServices
|
|
.GetService<IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>>()
|
|
);
|
|
|
|
}
|
|
|
|
protected virtual void ConfigureAuth(IApplicationBuilder app)
|
|
{
|
|
var identityUrl = Configuration.GetValue<string>("IdentityUrl");
|
|
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
|
|
{
|
|
Authority = identityUrl.ToString(),
|
|
ScopeName = "orders",
|
|
RequireHttpsMetadata = false
|
|
});
|
|
}
|
|
}
|
|
}
|