public static void Start() { if (restServer != null && restServer.IsListening) { return; } try { restServer = RestServerBuilder.From <Startup>().Build(); //restServer = RestServerBuilder.UseDefaults().Build(); //Urls = restServer.Prefixes.ToList(); restServer.AfterStarting += (s) => { Process.Start("explorer", s.Prefixes.First()); }; restServer.AfterStopping += (s) => { Console.WriteLine("Web-Server beendet."); }; //Urls = restServer.Prefixes.ToList(); restServer.Start(); } catch (Exception ex) { throw ex; } }
public static void Main(string[] args) { Console.WriteLine(" Starting Sample Server "); // using (var server = RestServerBuilder.UseDefaults().Build()) using (var server = RestServerBuilder.From <Startup>().Build()) { server.AfterStarting += (s) => { // This will produce a weird name in the output like `<Main>b__0_2` or something unless you add a name argument to the route constructor. s.Router.Register(new Route(async(ctx) => { await ctx.Response.SendResponseAsync($"Add hoc route {ctx.Server.Router.RoutingTable.Count}"); }, "Get", "/addhoc")); }; server.AfterStarting += (s) => { var sb = new StringBuilder(Environment.NewLine); sb.Append($"********************************************************************************{Environment.NewLine}"); sb.Append($"* Server listening on {string.Join(", ", server.Prefixes)}{Environment.NewLine}"); sb.Append($"* Stop server by going to {server.Prefixes.First()}/api/stop{Environment.NewLine}"); sb.Append($"********************************************************************************{Environment.NewLine}"); s.Logger.LogDebug(sb.ToString()); OpenBrowser(s.Prefixes.First()); }; // server.Router.GetServiceProvider().GetRequiredService<LocalClient>().RunInteractiveShell(server); server.Run(); } }