Skip to content

Instantly share code, notes, and snippets.

@SuperJC710e
Created December 1, 2022 18:52
Show Gist options
  • Save SuperJC710e/cdf5fde65e3eb229d1b5ee07efec3dcc to your computer and use it in GitHub Desktop.
Save SuperJC710e/cdf5fde65e3eb229d1b5ee07efec3dcc to your computer and use it in GitHub Desktop.
background-code.cs
using FGSyspro.Api;
using FGSyspro.Api.Startup;
using FGSyspro.Core;
using FGSyspro.Infrastructure;
using FGSyspro.Infrastructure.ENET;
using Hellang.Middleware.ProblemDetails;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Identity.Web;
using System.Reflection;
using System.Text.Json.Serialization;
WebApplicationBuilder? builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));
builder.Services.AddProblemDetails(options =>
{
options.MapToStatusCode<BusinessObjectException>(StatusCodes.Status400BadRequest);
});
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
builder.Services.AddCors(opt =>
opt.AddDefaultPolicy(cfg =>
{
if (builder.Environment.IsDevelopment())
{
cfg
.AllowAnyOrigin()
.AllowAnyHeader();
return;
}
cfg
.WithOrigins(builder.Configuration.GetSection("Cors:Origins").Get<string[]>());
}));
builder.Services.AddDefaultCore();//can register any
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
InfrastructureOptions? infraOptions = builder.Configuration.GetRequiredSection("Infrastructure").Get<InfrastructureOptions>();
builder.Services.AddSingleton(infraOptions);
builder.Services.AddScoped<IContextAcessor, ContextAcessor>();
builder.Services.AddDefaultInfrastructure(infraOptions);
AzureAdOptions? azureAdOptions = builder.Configuration.GetRequiredSection("AzureAd").Get<AzureAdOptions>();
SwaggerOptions? swaggerOptions = builder.Configuration.GetRequiredSection("SwaggerClient").Get<SwaggerOptions>();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(opt =>
{
string? xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
opt.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
opt.AddMicrosoftIdentity(azureAdOptions);
});
builder.Services.AddApplicationInsightsTelemetry(options =>
{
options.ConnectionString = builder.Configuration["APPINSIGHTS_CONNECTIONSTRING"];
});
EnetOptions? enetOptions = builder.Configuration.GetRequiredSection("ENet").Get<EnetOptions>();
builder.Services.AddSingleton<EnetOptions>(enetOptions);
WebApplication? app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsProduction())
{
app.UseSwagger();
app.UseSwaggerUI(opt =>
opt.ConfigureMicrosoftIdentity(swaggerOptions));
}
app.UseHttpsRedirection();
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();
app.UseProblemDetails();
app.MapControllers();
app.Run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment