/// <summary> /// Initializes a new instance of the <see cref="WebServer" /> class. /// </summary> /// <param name="mode">The type of HTTP listener to configure.</param> /// <param name="certificate">The X.509 certificate to use for SSL connections.</param> /// <param name="urlPrefixes">The URL prefixes to configure.</param> /// <exception cref="ArgumentNullException"><paramref name="urlPrefixes"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentException"> /// <para>One or more of the elements of <paramref name="urlPrefixes"/> is the empty string.</para> /// <para>- or -</para> /// <para>One or more of the elements of <paramref name="urlPrefixes"/> is already registered.</para> /// </exception> public WebServer(HttpListenerMode mode, X509Certificate2 certificate, params string[] urlPrefixes) : this(new WebServerOptions() .WithMode(mode) .WithCertificate(certificate) .WithUrlPrefixes(urlPrefixes)) { }
public void WebServer_Start_OnListenerStartFailure_Returns(HttpListenerMode listenerMode) { void ConfigureServerOptions(WebServerOptions options) => options .WithMode(listenerMode) .WithUrlPrefix("http://*:12345"); using var server1 = new WebServer(ConfigureServerOptions); using var server2 = new WebServer(ConfigureServerOptions); server1.Start(); server2.Start(); Assert.AreEqual(WebServerState.Stopped, server2.State); }
/// <summary> /// Creates the specified mode. /// </summary> /// <param name="mode">The mode.</param> /// <param name="certificate">The certificate.</param> /// <returns> /// A HTTP Listener. /// </returns> /// <exception cref="ArgumentOutOfRangeException">mode - null.</exception> public static IHttpListener Create(HttpListenerMode mode, X509Certificate certificate = null) { switch (mode) { case HttpListenerMode.EmbedIO: return(new Net.HttpListener(certificate)); case HttpListenerMode.Microsoft: if (System.Net.HttpListener.IsSupported) { return(new HttpListener(new System.Net.HttpListener())); } return(new Net.HttpListener(certificate)); default: throw new ArgumentOutOfRangeException(nameof(mode), mode, "Invalid HTTP Listener mode."); } }
/// <summary> /// Creates the specified mode. /// </summary> /// <param name="mode">The mode.</param> /// <returns>A HTTP Listener.</returns> /// <exception cref="ArgumentOutOfRangeException">mode - null.</exception> public static IHttpListener Create(HttpListenerMode mode) { switch (mode) { case HttpListenerMode.EmbedIO: return(new Net.HttpListener()); #if !NETSTANDARD1_3 && !UWP case HttpListenerMode.Microsoft: if (System.Net.HttpListener.IsSupported) { return(new HttpListener(new System.Net.HttpListener())); } return(new Net.HttpListener()); #endif default: throw new ArgumentOutOfRangeException(nameof(mode), mode, "Invalid HTTP Listener mode."); } }
/// <summary> /// Initializes a new instance of the <see cref="WebServer" /> class. /// </summary> /// <param name="mode">The type of HTTP listener to configure.</param> /// <param name="urlPrefixes">The URL prefixes to configure.</param> /// <exception cref="ArgumentNullException"><paramref name="urlPrefixes"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentException"> /// <para>One or more of the elements of <paramref name="urlPrefixes"/> is the empty string.</para> /// <para>- or -</para> /// <para>One or more of the elements of <paramref name="urlPrefixes"/> is already registered.</para> /// </exception> public WebServer(HttpListenerMode mode, params string[] urlPrefixes) : this(new WebServerOptions().WithMode(mode).WithUrlPrefixes(urlPrefixes)) { }
/// <summary> /// Sets the type of HTTP listener. /// </summary> /// <param name="this">The <see cref="WebServerOptions"/> on which this method is called.</param> /// <param name="value">The type of HTTP listener.</param> /// <returns><paramref name="this"/> with its <see cref="WebServerOptions.Mode">Mode</see> property /// set to <paramref name="value"/>.</returns> /// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception> /// <exception cref="InvalidOperationException">The configuration of <paramref name="this"/> is locked.</exception> public static WebServerOptions WithMode(this WebServerOptions @this, HttpListenerMode value) { @this.Mode = value; return(@this); }
/// <summary> /// Initializes a new instance of the <see cref="WebServer" /> class. /// </summary> /// <param name="urlPrefixes">The URL prefix.</param> /// <param name="routingStrategy">The routing strategy.</param> /// <param name="mode">The mode.</param> /// <param name="certificate">The certificate.</param> /// <exception cref="ArgumentException">Argument urlPrefix must be specified.</exception> /// <remarks> /// <c>urlPrefixes</c> must be specified as something similar to: http://localhost:9696/ /// Please notice the ending slash. -- It is important. /// </remarks> public WebServer(string[] urlPrefixes, RoutingStrategy routingStrategy, HttpListenerMode mode, X509Certificate certificate) : this(urlPrefixes, routingStrategy, HttpListenerFactory.Create(mode, certificate)) { // placeholder }
/// <summary> /// Initializes a new instance of the <see cref="WebServer" /> class. /// /// Default setting is EmbedIO HttpListenerMode. /// </summary> /// <remarks> /// <c>urlPrefixes</c> must be specified as something similar to: http://localhost:9696/ /// Please notice the ending slash. -- It is important. /// </remarks> /// <param name="urlPrefixes">The URL prefix.</param> /// <param name="routingStrategy">The routing strategy.</param> /// <param name="mode">The mode.</param> /// <exception cref="ArgumentException">Argument urlPrefix must be specified.</exception> public WebServer(string[] urlPrefixes, RoutingStrategy routingStrategy, HttpListenerMode mode = HttpListenerMode.EmbedIO) : this(urlPrefixes, routingStrategy, HttpListenerFactory.Create(mode)) { // placeholder }