Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
— |
net_core:old_program_style [2024/09/22 08:33] (aktuell) raiser angelegt |
||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | Old prpgram style | ||
+ | <code csharp Program.cs> | ||
+ | using System; | ||
+ | using System.Net.Mime; | ||
+ | using System.Threading; | ||
+ | using Microsoft.AspNetCore.Hosting; | ||
+ | using Microsoft.Extensions.Hosting; | ||
+ | |||
+ | |||
+ | namespace NameSpace | ||
+ | { | ||
+ | /// <summary> | ||
+ | /// Main Program | ||
+ | /// </summary> | ||
+ | public class Program | ||
+ | { | ||
+ | | ||
+ | /// <summary> | ||
+ | /// Main Loop Program | ||
+ | /// </summary> | ||
+ | /// <param name="args"></param> | ||
+ | public static void Main(string[] args) | ||
+ | { | ||
+ | //start an threaded app loop | ||
+ | //ThreadPool.QueueUserWorkItem(Core.CoreRun.Run, args); | ||
+ | CreateHostBuilder(args).Build().Run(); | ||
+ | } | ||
+ | |||
+ | /// <summary> | ||
+ | /// Start Kestrel Webservice | ||
+ | /// </summary> | ||
+ | /// <param name="args"></param> | ||
+ | /// <returns></returns> | ||
+ | private static IHostBuilder CreateHostBuilder(string[] args) => | ||
+ | Host.CreateDefaultBuilder(args) | ||
+ | .ConfigureWebHostDefaults( | ||
+ | webBuilder => | ||
+ | { | ||
+ | //configure server to accept all interfaces - and not only localhost ;) | ||
+ | webBuilder.UseUrls("http://*:5000", "https://*:5001", "http://0.0.0.0:5000", | ||
+ | "https://0.0.0.0:5001"); | ||
+ | webBuilder.UseStartup<Startup>(); | ||
+ | }); | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | <code csharp Startup.cs> | ||
+ | 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 | ||
+ | { | ||
+ | /// <summary> | ||
+ | /// Startup Webservice and configure the Kestrel | ||
+ | /// </summary> | ||
+ | public class Startup | ||
+ | { | ||
+ | /// <summary> | ||
+ | /// Kestrel configuration | ||
+ | /// </summary> | ||
+ | // ReSharper disable once MemberCanBePrivate.Global | ||
+ | // ReSharper disable once UnusedAutoPropertyAccessor.Global | ||
+ | public IConfiguration Configuration { get; } | ||
+ | | ||
+ | | ||
+ | // ReSharper disable once CommentTypo | ||
+ | /// <summary> | ||
+ | /// OwnConfiguration-Section im JSON-Configuration (appsettings.json) | ||
+ | /// </summary> | ||
+ | // ReSharper disable once MemberCanBePrivate.Global | ||
+ | public static OwnConfiguration OwnConfiguration { get; set; } | ||
+ | | ||
+ | | ||
+ | /// <summary> | ||
+ | /// ctor | ||
+ | /// </summary> | ||
+ | /// <param name="configuration"></param> | ||
+ | public Startup(IConfiguration configuration) | ||
+ | { | ||
+ | Configuration = configuration; | ||
+ | OwnConfiguration = Configuration.GetSection("OwnConfiguration").Get<OwnConfiguration>(); | ||
+ | } | ||
+ | | ||
+ | | ||
+ | /// <summary> | ||
+ | /// This method gets called by the runtime. Use this method to add services to the container. | ||
+ | /// </summary> | ||
+ | /// <param name="services"></param> | ||
+ | // ReSharper disable once CA1822 | ||
+ | public void ConfigureServices(IServiceCollection services) | ||
+ | { | ||
+ | | ||
+ | //provide the MVC Pattern to generate Views from Views/<Controller>/<Method>.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<SetupActionFilter> logger = loggerFactory.CreateLogger<SetupActionFilter>(); | ||
+ | //options.Filters.Add(new SetupActionFilter(OwnConfiguration, new Logger<SetupActionFilter>(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(); | ||
+ | | ||
+ | } | ||
+ | |||
+ | | ||
+ | /// <summary> | ||
+ | /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | ||
+ | /// </summary> | ||
+ | /// <param name="app"></param> | ||
+ | /// <param name="env"></param> | ||
+ | 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(); | ||
+ | }); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </code> |