The startup.cs class was purified by writing the extensions method in the field where services are registered in the startup.cs class.
This commit is contained in:
parent
79d81f531a
commit
4814e1bce2
@ -0,0 +1,85 @@
|
||||
using IdentityServer4.Services;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.eShopOnContainers.Services.Identity.API;
|
||||
using Microsoft.eShopOnContainers.Services.Identity.API.Certificates;
|
||||
using Microsoft.eShopOnContainers.Services.Identity.API.Data;
|
||||
using Microsoft.eShopOnContainers.Services.Identity.API.Devspaces;
|
||||
using Microsoft.eShopOnContainers.Services.Identity.API.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using Microsoft.eShopOnContainers.Services.Identity.API.Services;
|
||||
|
||||
namespace Identity.API.Extensions
|
||||
{
|
||||
public static class StartupConfigurationExtensions
|
||||
{
|
||||
public static void RegisterAppInsights(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddApplicationInsightsTelemetry(configuration);
|
||||
services.AddApplicationInsightsKubernetesEnricher();
|
||||
}
|
||||
|
||||
// Add framework services.
|
||||
public static void ConfigureDatabase(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddDbContext<ApplicationDbContext>(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: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
|
||||
}));
|
||||
|
||||
services.AddIdentity<ApplicationUser, IdentityRole>()
|
||||
.AddEntityFrameworkStores<ApplicationDbContext>()
|
||||
.AddDefaultTokenProviders();
|
||||
}
|
||||
|
||||
// Adds IdentityServer
|
||||
public static void ConfigureIdentityServer(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
var connectionString = configuration["ConnectionString"];
|
||||
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
|
||||
|
||||
services.AddIdentityServer(x =>
|
||||
{
|
||||
x.IssuerUri = "null";
|
||||
x.Authentication.CookieLifetime = TimeSpan.FromHours(2);
|
||||
})
|
||||
.AddDevspacesIfNeeded(configuration.GetValue("EnableDevspaces", false))
|
||||
.AddSigningCredential(Certificate.Get())
|
||||
.AddAspNetIdentity<ApplicationUser>()
|
||||
.AddConfigurationStore(options =>
|
||||
{
|
||||
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString,
|
||||
sqlServerOptionsAction: sqlOptions =>
|
||||
{
|
||||
sqlOptions.MigrationsAssembly(migrationsAssembly);
|
||||
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
|
||||
sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
|
||||
});
|
||||
})
|
||||
.AddOperationalStore(options =>
|
||||
{
|
||||
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString,
|
||||
sqlServerOptionsAction: sqlOptions =>
|
||||
{
|
||||
sqlOptions.MigrationsAssembly(migrationsAssembly);
|
||||
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
|
||||
sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
|
||||
});
|
||||
})
|
||||
.Services.AddTransient<IProfileService, ProfileService>();
|
||||
}
|
||||
|
||||
public static void ConfigureDependecyInjections(this IServiceCollection services)
|
||||
{
|
||||
services.AddTransient<ILoginService<ApplicationUser>, EFLoginService>();
|
||||
services.AddTransient<IRedirectService, RedirectService>();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,18 +1,11 @@
|
||||
using Autofac;
|
||||
using Autofac.Extensions.DependencyInjection;
|
||||
using HealthChecks.UI.Client;
|
||||
using IdentityServer4.Services;
|
||||
using Identity.API.Extensions;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.eShopOnContainers.Services.Identity.API.Certificates;
|
||||
using Microsoft.eShopOnContainers.Services.Identity.API.Data;
|
||||
using Microsoft.eShopOnContainers.Services.Identity.API.Devspaces;
|
||||
using Microsoft.eShopOnContainers.Services.Identity.API.Models;
|
||||
using Microsoft.eShopOnContainers.Services.Identity.API.Services;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
@ -20,37 +13,22 @@ using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using StackExchange.Redis;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Microsoft.eShopOnContainers.Services.Identity.API
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public Startup(IConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
}
|
||||
public Startup(IConfiguration configuration) => (Configuration) = (configuration);
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
public IServiceProvider ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
RegisterAppInsights(services);
|
||||
services.RegisterAppInsights(Configuration);
|
||||
|
||||
// Add framework services.
|
||||
services.AddDbContext<ApplicationDbContext>(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: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
|
||||
}));
|
||||
|
||||
services.AddIdentity<ApplicationUser, IdentityRole>()
|
||||
.AddEntityFrameworkStores<ApplicationDbContext>()
|
||||
.AddDefaultTokenProviders();
|
||||
services.ConfigureDatabase(Configuration);
|
||||
|
||||
services.Configure<AppSettings>(Configuration);
|
||||
|
||||
@ -69,42 +47,10 @@ namespace Microsoft.eShopOnContainers.Services.Identity.API
|
||||
name: "IdentityDB-check",
|
||||
tags: new string[] { "IdentityDB" });
|
||||
|
||||
services.AddTransient<ILoginService<ApplicationUser>, EFLoginService>();
|
||||
services.AddTransient<IRedirectService, RedirectService>();
|
||||
|
||||
var connectionString = Configuration["ConnectionString"];
|
||||
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
|
||||
services.ConfigureDependecyInjections();
|
||||
|
||||
// Adds IdentityServer
|
||||
services.AddIdentityServer(x =>
|
||||
{
|
||||
x.IssuerUri = "null";
|
||||
x.Authentication.CookieLifetime = TimeSpan.FromHours(2);
|
||||
})
|
||||
.AddDevspacesIfNeeded(Configuration.GetValue("EnableDevspaces", false))
|
||||
.AddSigningCredential(Certificate.Get())
|
||||
.AddAspNetIdentity<ApplicationUser>()
|
||||
.AddConfigurationStore(options =>
|
||||
{
|
||||
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString,
|
||||
sqlServerOptionsAction: sqlOptions =>
|
||||
{
|
||||
sqlOptions.MigrationsAssembly(migrationsAssembly);
|
||||
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
|
||||
sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
|
||||
});
|
||||
})
|
||||
.AddOperationalStore(options =>
|
||||
{
|
||||
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString,
|
||||
sqlServerOptionsAction: sqlOptions =>
|
||||
{
|
||||
sqlOptions.MigrationsAssembly(migrationsAssembly);
|
||||
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
|
||||
sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
|
||||
});
|
||||
})
|
||||
.Services.AddTransient<IProfileService, ProfileService>();
|
||||
services.ConfigureIdentityServer(Configuration);
|
||||
|
||||
//services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
|
||||
services.AddControllers();
|
||||
@ -174,11 +120,5 @@ namespace Microsoft.eShopOnContainers.Services.Identity.API
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private void RegisterAppInsights(IServiceCollection services)
|
||||
{
|
||||
services.AddApplicationInsightsTelemetry(Configuration);
|
||||
services.AddApplicationInsightsKubernetesEnricher();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user