public static OpenHttpServer CreateAndRun() { var currentPort = DefaultPort; while (true) { OpenHttpServer server = null; try { server = new OpenHttpServer(); var baseAddress = new Uri($"http://localhost:{currentPort}"); // ReSharper disable once AccessToDisposedClosure var webHost = WebHost.Start(baseAddress.ToString(), ctx => server.Handler(ctx)); server.Host = webHost; server.BaseAddress = baseAddress; return(server); } catch (IOException ex) { server?.Dispose(); if (!(ex.InnerException is AddressInUseException)) { throw; } } currentPort++; } }
public void Dispose() { _httpServer?.Dispose(); _client?.Dispose(); }