public static void Main(string[] args) { // TODO AppDomains are no longer a thing - what to do about Razor? // Initialize in separate AppDomain for Razor //if (AppDomain.CurrentDomain.IsDefaultAppDomain()) //{ // var setup = new AppDomainSetup // { // ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase // }; // var strongNames = new StrongName[0]; // var domain = AppDomain.CreateDomain( // "MyMainDomain", // null, // setup, // new PermissionSet(PermissionState.Unrestricted), // strongNames); // domain.ExecuteAssembly(Assembly.GetEntryAssembly().Location); // AppDomain.Unload(domain); // return; //} using (var host = new WebHostBuilder() .UseContentRoot(Directory.GetCurrentDirectory()) .UseKestrel() .UseStartup<Startup>() .UseUrls("http://localhost:1234") .Build()) { Log.Info("Starting server..."); host.Start(); Log.Info("Running on port 1234."); if (IsRunningOnLinux()) { // On linux (i.e. Travis CI), we only terminate when killed with -SIGUSR1 // TODO Listening to arbitrary unix signals is unsupported on .NET Core so far. Reimplement when available. //UnixSignal.WaitAny(new[] { new UnixSignal(Signum.SIGUSR1) }); } else { // During local debugging, we quit on any input. Console.ReadLine(); } Log.Info("Shutting down server..."); } }
// Entry point for the application. public static void Main(string[] args) { var config = new ConfigurationBuilder().AddCommandLine(args).Build(); var host = new WebHostBuilder() .UseConfiguration(config) .UseStartup<StartupBlockingOnStart>() .Build(); using (host) { host.Start(); Console.ReadLine(); } }
private IWebHost StartWebSocketServer(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 host = new WebHostBuilder() .UseConfiguration(config) .UseKestrel() .Configure(startup) .Build(); host.Start(); return host; }
/// <summary> /// Starts listening at the specified port. /// </summary> public void Start() { Startup.Listener = this; m_host = new WebHostBuilder(); HttpsConnectionFilterOptions httpsOptions = new HttpsConnectionFilterOptions(); httpsOptions.CheckCertificateRevocation = false; httpsOptions.ClientCertificateMode = ClientCertificateMode.NoCertificate; httpsOptions.ServerCertificate = m_serverCert; httpsOptions.SslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12; m_host.UseKestrel(options => { options.NoDelay = true; options.UseHttps(httpsOptions); }); m_host.UseContentRoot(Directory.GetCurrentDirectory()); m_host.UseStartup<Startup>(); m_host.Build(); m_host.Start(Utils.ReplaceLocalhost(m_uri.ToString())); }
private void startWebServer(int port, WebSocketsHandler webSockets) { var baseDirectory = AppContext.BaseDirectory; var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(baseDirectory) .UseUrls($"http://localhost:{port}") .Configure(app => { app.UseWebSockets(); app.Use(async (http, next) => { if (http.WebSockets.IsWebSocketRequest) await webSockets.HandleSocket(http).ConfigureAwait(false); else await next().ConfigureAwait(false); }); #if DEBUG configureStaticFiles(app); #endif app.Run(async http => { if (http.Request.Path == "/favicon.ico") { var stream = GetType() .GetTypeInfo() .Assembly.GetManifestResourceStream("StorytellerRunner.favicon.ico"); http.Response.ContentType = "image/x-icon"; await stream.CopyToAsync(http.Response.Body).ConfigureAwait(false); return; } try { string html; if (http.Request.Path.HasValue && http.Request.Path.Value == "/preview") { html = ExportWriter.BuildPage(_application); } else { html = HomeEndpoint.BuildPage(_application, _input).ToString(); } http.Response.ContentType = "text/html"; await http.Response.WriteAsync(html).ConfigureAwait(false); } catch (Exception e) { Console.WriteLine(e.ToString()); throw; } }); }); _server = host.Start(); }
private IWebHost startHost(int port, WebSocketsHandler webSockets, TopicMiddleware middleware) { var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseUrls($"http://localhost:{port}") .Configure(app => { app.UseWebSockets(); app.Use(async (http, next) => { if (http.WebSockets.IsWebSocketRequest) await webSockets.HandleSocket(http).ConfigureAwait(false); else await next().ConfigureAwait(false); }); app.UseStaticFiles(new StaticFileOptions { ServeUnknownFileTypes = true, FileProvider = new PhysicalFileProvider(_settings.Root) }); app.Use(async (http, next) => { if (http.Request.Method.EqualsIgnoreCase("POST")) switch (http.Request.Path) { case "/refresh": Console.WriteLine("Hey, I got the order to do the hard refresh!"); await HardRefresh().ConfigureAwait(false); _refresher.RefreshPage(); break; case "/open": var url = new Uri(http.Request.Headers["referer"]); Console.WriteLine("Open requested to " + url); var topic = FindTopicByUrl(url.AbsolutePath.TrimStart('/')); if (topic != null) { try { var start = new ProcessStartInfo(); start.UseShellExecute = true; start.FileName = "open"; start.Arguments = topic.File; Process.Start(start); } catch (Exception ex){ Process.Start(topic.File); } } break; } else await next().ConfigureAwait(false); }); app.Run(middleware.Invoke); }) .Build(); host.Start(); return host; }