171 lines
6.6 KiB
C#
Raw Normal View History

2016-11-03 17:52:15 +01:00
namespace Microsoft.eShopOnContainers.Services.Catalog.API
{
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;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
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;
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;
using Microsoft.Extensions.Options;
using System;
2017-03-24 12:37:44 +01:00
using System.Data.Common;
2017-04-17 12:28:12 +02:00
using System.Data.SqlClient;
2017-03-03 12:03:31 +01:00
using System.Reflection;
2016-11-03 17:52:15 +01:00
public class Startup
{
public IConfigurationRoot Configuration { get; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile($"settings.json", optional: false, 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 void ConfigureServices(IServiceCollection services)
{
// Add framework services.
2017-04-17 12:28:12 +02:00
2017-03-23 19:10:55 +01:00
services.AddHealthChecks(checks =>
{
2017-03-31 12:47:56 +02:00
checks.AddSqlCheck("CatalogDb", Configuration["ConnectionString"]);
2017-03-23 19:10:55 +01:00
});
2017-03-28 16:37:48 +02:00
services.AddMvc(options =>
{
options.Filters.Add(typeof(HttpGlobalExceptionFilter));
}).AddControllersAsServices();
2017-03-27 15:24:29 +02:00
services.AddDbContext<CatalogContext>(options =>
{
options.UseSqlServer(Configuration["ConnectionString"],
sqlServerOptionsAction: sqlOptions =>
2017-04-17 12:28:12 +02:00
{
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);
});
2017-04-17 12:28:12 +02: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.
options.ConfigureWarnings(warnings => warnings.Throw(RelationalEventId.QueryClientEvaluationWarning));
//Check Client vs. Server evaluation: https://docs.microsoft.com/en-us/ef/core/querying/client-eval
});
2017-04-17 12:28:12 +02:00
services.Configure<CatalogSettings>(Configuration);
// Add framework services.
2016-11-03 17:52:15 +01:00
services.AddSwaggerGen();
services.ConfigureSwaggerGen(options =>
{
options.DescribeAllEnumsAsStrings();
options.SingleApiVersion(new Swashbuckle.Swagger.Model.Info()
{
Title = "eShopOnContainers - Catalog HTTP API",
2016-11-03 17:52:15 +01:00
Version = "v1",
Description = "The Catalog Microservice HTTP API. This is a Data-Driven/CRUD microservice sample",
TermsOfService = "Terms Of Service"
2016-11-03 17:52:15 +01:00
});
});
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
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));
services.AddTransient<ICatalogIntegrationEventService, CatalogIntegrationEventService>();
2017-04-17 12:28:12 +02:00
var serviceProvider = services.BuildServiceProvider();
services.AddSingleton<IEventBus>(sp =>
{
var settings = serviceProvider.GetRequiredService<IOptionsSnapshot<CatalogSettings>>().Value;
return new EventBusRabbitMQ(settings.EventBusConnection);
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//Configure logs
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseCors("CorsPolicy");
app.UseMvcWithDefaultRoute();
2016-11-03 17:52:15 +01:00
app.UseSwagger()
.UseSwaggerUi();
var context = (CatalogContext)app
.ApplicationServices.GetService(typeof(CatalogContext));
WaitForSqlAvailability(context, loggerFactory);
2017-04-17 12:28:12 +02:00
//Seed Data
CatalogContextSeed.SeedAsync(app, loggerFactory)
2017-04-17 12:28:12 +02:00
.Wait();
2017-03-24 12:37:44 +01:00
var integrationEventLogContext = new IntegrationEventLogContext(
new DbContextOptionsBuilder<IntegrationEventLogContext>()
.UseSqlServer(Configuration["ConnectionString"], b => b.MigrationsAssembly("Catalog.API"))
2017-03-24 12:37:44 +01:00
.Options);
2017-04-17 12:28:12 +02:00
integrationEventLogContext.Database.Migrate();
}
private void WaitForSqlAvailability(CatalogContext ctx, ILoggerFactory loggerFactory, int? retry = 0)
{
2017-04-17 12:28:12 +02:00
int retryForAvailability = retry.Value;
try
{
ctx.Database.OpenConnection();
}
2017-04-17 12:28:12 +02:00
catch (SqlException ex)
{
if (retryForAvailability < 10)
{
retryForAvailability++;
var log = loggerFactory.CreateLogger(nameof(Startup));
log.LogError(ex.Message);
WaitForSqlAvailability(ctx, loggerFactory, retryForAvailability);
}
}
2017-04-17 12:28:12 +02:00
finally
{
ctx.Database.CloseConnection();
}
}
}
}