171 lines
6.8 KiB
C#
Raw Normal View History

2017-06-01 10:10:00 +02:00
namespace Microsoft.eShopOnContainers.Services.Marketing.API
{
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Reflection;
using System;
2017-06-02 16:31:03 +02:00
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Filters;
2017-06-13 17:31:37 +02:00
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
using RabbitMQ.Client;
using BuildingBlocks.EventBus.Abstractions;
using BuildingBlocks.EventBus;
using IntegrationEvents.Events;
using IntegrationEvents.Handlers;
using Infrastructure.Repositories;
using Autofac;
using Autofac.Extensions.DependencyInjection;
2017-06-01 10:10:00 +02:00
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
if (env.IsDevelopment())
{
builder.AddUserSecrets(typeof(Startup).GetTypeInfo().Assembly);
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
2017-06-13 17:31:37 +02:00
public IServiceProvider ConfigureServices(IServiceCollection services)
2017-06-01 10:10:00 +02:00
{
// Add framework services.
2017-06-02 16:31:03 +02:00
services.AddMvc(options =>
{
options.Filters.Add(typeof(HttpGlobalExceptionFilter));
}).AddControllersAsServices(); //Injecting Controllers themselves thru DIFor further info see: http://docs.autofac.org/en/latest/integration/aspnetcore.html#controllers-as-services
2017-06-01 10:10:00 +02:00
2017-06-13 17:31:37 +02:00
services.Configure<MarketingSettings>(Configuration);
2017-06-01 10:10:00 +02:00
services.AddDbContext<MarketingContext>(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: 5, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
});
// Changing default behavior when client evaluation occurs to throw.
// Default in EF Core would be to log a warning when client evaluation is performed.
options.ConfigureWarnings(warnings => warnings.Throw(RelationalEventId.QueryClientEvaluationWarning));
//Check Client vs. Server evaluation: https://docs.microsoft.com/en-us/ef/core/querying/client-eval
});
2017-06-13 17:31:37 +02:00
services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
{
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
var factory = new ConnectionFactory()
{
HostName = Configuration["EventBusConnection"]
};
return new DefaultRabbitMQPersistentConnection(factory, logger);
});
RegisterServiceBus(services);
2017-06-01 10:10:00 +02:00
// Add framework services.
services.AddSwaggerGen(options =>
{
options.DescribeAllEnumsAsStrings();
options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info
{
Title = "Marketing HTTP API",
Version = "v1",
Description = "The Marketing Service HTTP API",
TermsOfService = "Terms Of Service"
});
});
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
2017-06-13 17:31:37 +02:00
services.AddTransient<IMarketingDataRepository, MarketingDataRepository>();
//configure autofac
var container = new ContainerBuilder();
container.Populate(services);
return new AutofacServiceProvider(container.Build());
2017-06-01 10:10:00 +02:00
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseCors("CorsPolicy");
ConfigureAuth(app);
app.UseMvcWithDefaultRoute();
app.UseSwagger()
.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
2017-06-13 17:31:37 +02:00
ConfigureEventBus(app);
2017-06-01 20:15:21 +02:00
MarketingContextSeed.SeedAsync(app, loggerFactory)
.Wait();
2017-06-01 10:10:00 +02:00
}
protected virtual void ConfigureAuth(IApplicationBuilder app)
{
var identityUrl = Configuration.GetValue<string>("IdentityUrl");
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
Authority = identityUrl.ToString(),
ApiName = "marketing",
RequireHttpsMetadata = false
});
}
2017-06-13 17:31:37 +02:00
private void RegisterServiceBus(IServiceCollection services)
{
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
services.AddSingleton<IEventBusSubscriptionsManager,
InMemoryEventBusSubscriptionsManager>();
services.AddTransient<IIntegrationEventHandler<UserLocationUpdatedIntegrationEvent>,
UserLocationUpdatedIntegrationEventHandler>();
}
private void ConfigureEventBus(IApplicationBuilder app)
{
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
eventBus.Subscribe<UserLocationUpdatedIntegrationEvent,
IIntegrationEventHandler<UserLocationUpdatedIntegrationEvent>>();
}
2017-06-01 10:10:00 +02:00
}
}