-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathProgram.cs
101 lines (80 loc) · 2.85 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using Microsoft.AspNetCore.Mvc.Formatters; // To use IOutputFormatter.
using Northwind.EntityModels; // To use AddNorthwindContext method.
using Microsoft.Extensions.Caching.Memory; // To use IMemoryCache and so on.
using Northwind.WebApi.Repositories; // To use ICustomerRepository.
using Swashbuckle.AspNetCore.SwaggerUI; // To use SubmitMethod.
using Microsoft.AspNetCore.HttpLogging; // To use HttpLoggingFields.
const string corsPolicyName = "allowWasmClient";
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpLogging(options =>
{
options.LoggingFields = HttpLoggingFields.All;
options.RequestBodyLogLimit = 4096; // Default is 32k.
options.ResponseBodyLogLimit = 4096; // Default is 32k.
});
builder.Services.AddSingleton<IMemoryCache>(
new MemoryCache(new MemoryCacheOptions()));
// Add services to the container.
builder.Services.AddNorthwindContext();
builder.Services.AddControllers(options =>
{
WriteLine("Default output formatters:");
foreach (IOutputFormatter formatter in options.OutputFormatters)
{
OutputFormatter? mediaFormatter = formatter as OutputFormatter;
if (mediaFormatter is null)
{
WriteLine($" {formatter.GetType().Name}");
}
else // OutputFormatter class has SupportedMediaTypes.
{
WriteLine(" {0}, Media types: {1}",
arg0: mediaFormatter.GetType().Name,
arg1: string.Join(", ",
mediaFormatter.SupportedMediaTypes));
}
}
})
.AddXmlDataContractSerializerFormatters()
.AddXmlSerializerFormatters();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddScoped<ICustomerRepository, CustomerRepository>();
builder.Services.AddHealthChecks()
.AddDbContextCheck<NorthwindContext>()
// Execute SELECT 1 using the specified connection string.
.AddSqlServer("Data Source=.;Initial Catalog=Northwind;Integrated Security=true;TrustServerCertificate=true;");
builder.Services.AddCors(options =>
{
// Allow HTTP calls from the Blazor Web App project.
options.AddPolicy(name: corsPolicyName,
policy =>
{
policy.AllowAnyHeader();
policy.WithOrigins("https://localhost:5161",
"http://localhost:5160");
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json",
"Northwind Service API Version 1");
c.SupportedSubmitMethods(new[] {
SubmitMethod.Get, SubmitMethod.Post,
SubmitMethod.Put, SubmitMethod.Delete });
});
}
app.UseHttpLogging();
app.UseHttpsRedirection();
app.UseAuthorization();
app.UseHealthChecks(path: "/howdoyoufeel");
app.UseMiddleware<SecurityHeaders>();
app.UseCors(corsPolicyName);
app.MapControllers();
app.Run();