-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
75 lines (57 loc) · 2.15 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using Microsoft.EntityFrameworkCore;
using NLog.Web;
//using Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore;
//using Microsoft.Extensions.DependencyInjection;
//using Microsoft.Extensions.Hosting;
using WebAPI_week1;
using WebAPI_week1.WeatherWorkers;
var builder = WebApplication.CreateBuilder(args);
//CORS
string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
builder.Services.AddCors(options =>
{
options.AddPolicy(MyAllowSpecificOrigins, builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
});
//CONFIGURE LOGGING TO WRITE TO FILE
builder.Logging.ClearProviders();
builder.Host.UseNLog();
//builder.Services.AddTransient<WeatherWorker>();
// add distributed memory cache
builder.Services.AddMemoryCache();
builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
//REGISTER DBCONTEXT
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
builder.Services.AddDbContext<PersonsDB>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
//REGISTER SERVICES
builder.Services.AddHttpClient();
builder.Services.AddSession();
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
builder.Services.AddScoped<PersonsDB>(); // Scoped because it will be injected into a service that is singleton
builder.Services.AddHostedService<WeatherWorker>();
//ADD CONTROLLERS AND SWAGGER
builder.Services.AddControllers();
builder.Services.AddControllers().AddNewtonsoftJson(options =>
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
var scope = app.Services.CreateAsyncScope();
var context = scope.ServiceProvider.GetRequiredService<PersonsDB>();
context.Database.Migrate();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseSession();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();