mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2025-12-06 05:32:03 +01:00
83 lines
2.3 KiB
C#
83 lines
2.3 KiB
C#
using Microsoft.AspNetCore.Mvc.Formatters; // IOutputFormatter, OutputFormatter
|
|
using Packt.Shared; // AddNorthwindContext extension method
|
|
using Northwind.WebApi.Repositories; // ICustomerRepository, CustomerRepository
|
|
using Swashbuckle.AspNetCore.SwaggerUI;
|
|
using Microsoft.AspNetCore.Server.Kestrel.Core; // HttpProtocols
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// 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;");
|
|
|
|
/*
|
|
builder.WebHost.ConfigureKestrel((context, options) =>
|
|
{
|
|
options.ListenAnyIP(5002, listenOptions =>
|
|
{
|
|
listenOptions.Protocols = HttpProtocols.Http1AndHttp2AndHttp3;
|
|
listenOptions.UseHttps(); // HTTP/3 requires secure connections
|
|
});
|
|
});
|
|
*/
|
|
|
|
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.UseHttpsRedirection();
|
|
|
|
app.UseAuthorization();
|
|
|
|
app.UseHealthChecks(path: "/howdoyoufeel");
|
|
|
|
app.UseMiddleware<SecurityHeaders>();
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|