public static int Start(Action <string> onUriReceive) { var port = FreePortHelper.GetFreePort(); server = new IpcServer(); server.Start(port); // Passing no port selects a free port automatically. Console.WriteLine("Started server on port {0}.", server.Port); server.ReceivedRequest += (sender, args) => { args.Response = "I've got: " + args.Request; args.Handled = true; onUriReceive(args.Request); }; return(port); }
/// <summary> /// Start listening at 127.0.0.1:port. /// </summary> public void Start(int port = 0) { if (_server != null) { throw new Exception("Server already initialized."); } Address = getLocalHost(); Port = port <= 0 ? FreePortHelper.GetFreePort() : port; _server = new HttpServer(new MyLogWriter()); _server.ExceptionThrown += (source, exception) => throw new Exception("Error during server processing.", exception); _server.FormDecoderProviders.Add(new MyFormDecoder()); _server.Add(new MyModule(this)); _server.Start(IPAddress.Loopback, Port); Trace.WriteLine( $@"[Web server] Started local web server for URL '{baseUrl}'."); }