/
Program.cs
30 lines (26 loc) · 1.07 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var builder = WebApplication.CreateBuilder(args);
#region Add services to the DI container
// AddDataBase
builder.Services.AddDataBase(builder.Configuration.GetConnectionString("CS"));
// AddCors
builder.Services.AddCors(options => options.AddPolicy(name: "AppOrigins",
policy => { policy.WithOrigins("http://localhost:4200").AllowAnyMethod().AllowAnyHeader(); }));
// Add Scoped AppServices
var appServices = typeof(Program).Assembly.GetTypes()
.Where(s => s.Name.EndsWith("Service") && s.IsInterface == false).ToList();
foreach (var appService in appServices)
builder.Services.Add(new ServiceDescriptor(appService, appService, ServiceLifetime.Scoped));
builder.Services.AddControllers().AddBadRequestServices();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
#endregion
var app = builder.Build();
#region HTTP request pipeline
if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); }
app.ExceptionHandler();
app.UseCors("AppOrigins");
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
#endregion