示例#1
0
            public async Task BindAsync(AddressBindContext context)
            {
                context.Logger.LogDebug(CoreStrings.BindingToDefaultAddress, Constants.DefaultServerAddress);

                await BindLocalhostAsync(ServerAddress.FromUrl(Constants.DefaultServerAddress), context).ConfigureAwait(false);
            }
示例#2
0
        private static async Task BindAddressAsync(string address, AddressBindContext context)
        {
            var parsedAddress = ServerAddress.FromUrl(address);
            var https         = false;

            if (parsedAddress.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase))
            {
                https = true;
            }
            else if (!parsedAddress.Scheme.Equals("http", StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException(CoreStrings.FormatUnsupportedAddressScheme(address));
            }

            if (!string.IsNullOrEmpty(parsedAddress.PathBase))
            {
                throw new InvalidOperationException(CoreStrings.FormatConfigurePathBaseFromMethodCall($"{nameof(IApplicationBuilder)}.UsePathBase()"));
            }

            ListenOptions options = null;

            if (parsedAddress.IsUnixPipe)
            {
                options = new ListenOptions(parsedAddress.UnixPipePath);
                await BindEndpointAsync(options, context).ConfigureAwait(false);

                context.Addresses.Add(options.GetDisplayName());
            }
            else if (string.Equals(parsedAddress.Host, "localhost", StringComparison.OrdinalIgnoreCase))
            {
                // "localhost" for both IPv4 and IPv6 can't be represented as an IPEndPoint.
                await BindLocalhostAsync(parsedAddress, context, https).ConfigureAwait(false);
            }
            else
            {
                if (TryCreateIPEndPoint(parsedAddress, out var endpoint))
                {
                    options = new ListenOptions(endpoint);
                    await BindEndpointAsync(options, context).ConfigureAwait(false);
                }
                else
                {
                    // when address is 'http://hostname:port', 'http://*:port', or 'http://+:port'
                    try
                    {
                        options = new ListenOptions(new IPEndPoint(IPAddress.IPv6Any, parsedAddress.Port));
                        await BindEndpointAsync(options, context).ConfigureAwait(false);
                    }
                    catch (Exception ex) when(!(ex is IOException))
                    {
                        context.Logger.LogDebug(CoreStrings.FormatFallbackToIPv4Any(parsedAddress.Port));

                        // for machines that do not support IPv6
                        options = new ListenOptions(new IPEndPoint(IPAddress.Any, parsedAddress.Port));
                        await BindEndpointAsync(options, context).ConfigureAwait(false);
                    }
                }

                context.Addresses.Add(options.GetDisplayName());
            }

            if (https && options != null)
            {
                options.KestrelServerOptions = context.ServerOptions;
                context.DefaultHttpsProvider.ConfigureHttps(options);
            }
        }
示例#3
0
 private static Task BindEndpointAsync(IPEndPoint endpoint, AddressBindContext context)
 => BindEndpointAsync(new ListenOptions(endpoint), context);
示例#4
0
        public static async Task BindAsync(IEnumerable <ListenOptions> listenOptions, AddressBindContext context, CancellationToken cancellationToken)
        {
            var strategy = CreateStrategy(
                listenOptions.ToArray(),
                context.Addresses.ToArray(),
                context.ServerAddressesFeature.PreferHostingUrls);

            // reset options. The actual used options and addresses will be populated
            // by the address binding feature
            context.ServerOptions.OptionsInUse.Clear();
            context.Addresses.Clear();

            await strategy.BindAsync(context, cancellationToken).ConfigureAwait(false);
        }