// Entry point for the application. public static void Main(string[] args) { var config = WebApplicationConfiguration.GetDefault(args); var application = new WebApplicationBuilder() .UseConfiguration(config) .UseStartup<StartupBlockingOnStart>() .Build(); using (application) { application.Start(); Console.ReadLine(); } }
public static IDisposable CreateServer(Func<HttpContext, Task> app) { Action<IApplicationBuilder> startup = builder => { builder.Use(async (ct, next) => { try { // Kestrel does not return proper error responses: // https://github.com/aspnet/KestrelHttpServer/issues/43 await next(); } catch (Exception ex) { if (ct.Response.HasStarted) { throw; } ct.Response.StatusCode = 500; ct.Response.Headers.Clear(); await ct.Response.WriteAsync(ex.ToString()); } }); builder.UseWebSockets(); builder.Run(c => app(c)); }; var configBuilder = new ConfigurationBuilder(); configBuilder.AddInMemoryCollection(); var config = configBuilder.Build(); config["server.urls"] = "http://localhost:54321"; var application = new WebApplicationBuilder() .UseConfiguration(config) .UseServer("Microsoft.AspNet.Server.Kestrel") .Configure(startup) .Build(); application.Start(); return application; }