tuitio/IdentityServer.Api/Startup.cs

81 lines
2.5 KiB
C#
Raw Normal View History

2020-12-20 03:22:27 +02:00
using AutoMapper;
using IdentityServer.Api.Swagger;
using IdentityServer.Application;
using IdentityServer.Domain.Data;
using MediatR;
using MediatR.Pipeline;
2020-12-19 18:17:24 +02:00
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
2020-12-20 03:22:27 +02:00
using Newtonsoft.Json;
using System.Reflection;
2020-12-19 18:17:24 +02:00
namespace IdentityServer.Api
{
public class Startup
{
2020-12-20 03:22:27 +02:00
private readonly IConfiguration _configuration;
2020-12-19 18:17:24 +02:00
public Startup(IConfiguration configuration)
{
2020-12-20 03:22:27 +02:00
_configuration = configuration;
2020-12-19 18:17:24 +02:00
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
2020-12-20 03:22:27 +02:00
services.AddControllers()
.AddNewtonsoftJson(o => o.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc);
// MediatR
services.AddMediatR(GetMediatRAssemblies());
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPreProcessorBehavior<,>));
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPostProcessorBehavior<,>));
// AutoMapper
services.AddAutoMapper(
typeof(Application.Mappings.MappingProfile).Assembly);
// Swagger
services.AddSwagger();
// Data access
services.AddDataAccess();
// Application
services.AddApplicationServices();
}
private Assembly[] GetMediatRAssemblies()
{
var assembly = typeof(Application.Commands.AuthenticateUser).Assembly;
return new Assembly[] { assembly };
2020-12-19 18:17:24 +02:00
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
2020-12-20 03:22:27 +02:00
// global cors policy
app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
2020-12-19 18:17:24 +02:00
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
2020-12-20 03:22:27 +02:00
app.ConfigureSwagger();
2020-12-19 18:17:24 +02:00
}
}
}