using System;
using System.IO;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
namespace NameSpace
{
///
/// Startup Webservice and configure the Kestrel
///
public class Startup
{
///
/// Kestrel configuration
///
// ReSharper disable once MemberCanBePrivate.Global
// ReSharper disable once UnusedAutoPropertyAccessor.Global
public IConfiguration Configuration { get; }
// ReSharper disable once CommentTypo
///
/// OwnConfiguration-Section im JSON-Configuration (appsettings.json)
///
// ReSharper disable once MemberCanBePrivate.Global
public static OwnConfiguration OwnConfiguration { get; set; }
///
/// ctor
///
///
public Startup(IConfiguration configuration)
{
Configuration = configuration;
OwnConfiguration = Configuration.GetSection("OwnConfiguration").Get();
}
///
/// This method gets called by the runtime. Use this method to add services to the container.
///
///
// ReSharper disable once CA1822
public void ConfigureServices(IServiceCollection services)
{
//provide the MVC Pattern to generate Views from Views//.cshtml
//see: Views/Setup/Index.cshtml
services.AddControllersWithViews(options =>
{
//add singleton SetupActionFilter
//init logger for SetupActionFilter and register to global logging system (console)
var loggerFactory = LoggerFactory.Create(builder =>
{
builder
.AddConsole();
//.AddEventLog();
});
ILogger logger = loggerFactory.CreateLogger();
//options.Filters.Add(new SetupActionFilter(OwnConfiguration, new Logger(new LoggerFactory())));
options.Filters.Add(new SetupActionFilter(OwnConfiguration, logger));
//Every request has own instance from SetupActionFilter
//options.Filters.Add(typeof(SetupActionFilter));
});
//todo: AddHealthChecks
//services.AddHealthChecks()
//add swagger service
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo {Title = "APP", Version = "v1"});
// Set the comments path for the Swagger JSON and UI.
//generate documentation for swagger-UI from the Code-XML-Documentation /// <...>
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
services.AddMvc();
}
///
/// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
///
///
///
public void Configure(IApplicationBuilder app, IWebHostEnvironment env )//, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "APP v1"));
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}