示例#1
0
        private IServer buildServer()
        {
            var server = Container.TryGetInstance <IServer>();

            if (server == null)
            {
                return(new NulloServer());
            }

            var serverAddressesFeature = server.Features?.Get <IServerAddressesFeature>();
            var addresses = serverAddressesFeature?.Addresses;

            if (addresses != null && !addresses.IsReadOnly && addresses.Count == 0)
            {
                var urls = Registry.EnvironmentConfiguration[WebHostDefaults.ServerUrlsKey];
                if (!string.IsNullOrEmpty(urls))
                {
                    serverAddressesFeature.PreferHostingUrls = WebHostUtilities.ParseBool(Registry.EnvironmentConfiguration, WebHostDefaults.PreferHostingUrlsKey);

                    foreach (var value in urls.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        addresses.Add(value);
                    }
                }
            }

            return(server);
        }
示例#2
0
    public WebHostOptions(IConfiguration primaryConfiguration, IConfiguration?fallbackConfiguration = null)
    {
        if (primaryConfiguration is null)
        {
            throw new ArgumentNullException(nameof(primaryConfiguration));
        }

        string?GetConfig(string key) => primaryConfiguration[key] ?? fallbackConfiguration?[key];

        ApplicationName      = GetConfig(WebHostDefaults.ApplicationKey) ?? Assembly.GetEntryAssembly()?.GetName().Name ?? string.Empty;
        StartupAssembly      = GetConfig(WebHostDefaults.StartupAssemblyKey);
        DetailedErrors       = WebHostUtilities.ParseBool(GetConfig(WebHostDefaults.DetailedErrorsKey));
        CaptureStartupErrors = WebHostUtilities.ParseBool(GetConfig(WebHostDefaults.CaptureStartupErrorsKey));
        Environment          = GetConfig(WebHostDefaults.EnvironmentKey);
        WebRoot                = GetConfig(WebHostDefaults.WebRootKey);
        ContentRootPath        = GetConfig(WebHostDefaults.ContentRootKey);
        PreventHostingStartup  = WebHostUtilities.ParseBool(GetConfig(WebHostDefaults.PreventHostingStartupKey));
        SuppressStatusMessages = WebHostUtilities.ParseBool(GetConfig(WebHostDefaults.SuppressStatusMessagesKey));
        ServerUrls             = GetConfig(WebHostDefaults.ServerUrlsKey);
        PreferHostingUrls      = WebHostUtilities.ParseBool(GetConfig(WebHostDefaults.PreferHostingUrlsKey));

        // Search the primary assembly and configured assemblies.
        HostingStartupAssemblies        = Split(ApplicationName, GetConfig(WebHostDefaults.HostingStartupAssembliesKey));
        HostingStartupExcludeAssemblies = Split(GetConfig(WebHostDefaults.HostingStartupExcludeAssembliesKey));

        var timeout = GetConfig(WebHostDefaults.ShutdownTimeoutKey);

        if (!string.IsNullOrEmpty(timeout) &&
            int.TryParse(timeout, NumberStyles.None, CultureInfo.InvariantCulture, out var seconds))
        {
            ShutdownTimeout = TimeSpan.FromSeconds(seconds);
        }
    }
示例#3
0
    public WebHostOptions(IConfiguration configuration, string applicationNameFallback)
    {
        if (configuration == null)
        {
            throw new ArgumentNullException(nameof(configuration));
        }

        ApplicationName      = configuration[WebHostDefaults.ApplicationKey] ?? applicationNameFallback;
        StartupAssembly      = configuration[WebHostDefaults.StartupAssemblyKey];
        DetailedErrors       = WebHostUtilities.ParseBool(configuration, WebHostDefaults.DetailedErrorsKey);
        CaptureStartupErrors = WebHostUtilities.ParseBool(configuration, WebHostDefaults.CaptureStartupErrorsKey);
        Environment          = configuration[WebHostDefaults.EnvironmentKey];
        WebRoot                = configuration[WebHostDefaults.WebRootKey];
        ContentRootPath        = configuration[WebHostDefaults.ContentRootKey];
        PreventHostingStartup  = WebHostUtilities.ParseBool(configuration, WebHostDefaults.PreventHostingStartupKey);
        SuppressStatusMessages = WebHostUtilities.ParseBool(configuration, WebHostDefaults.SuppressStatusMessagesKey);

        // Search the primary assembly and configured assemblies.
        HostingStartupAssemblies        = Split(ApplicationName, configuration[WebHostDefaults.HostingStartupAssembliesKey]);
        HostingStartupExcludeAssemblies = Split(configuration[WebHostDefaults.HostingStartupExcludeAssembliesKey]);

        var timeout = configuration[WebHostDefaults.ShutdownTimeoutKey];

        if (!string.IsNullOrEmpty(timeout) &&
            int.TryParse(timeout, NumberStyles.None, CultureInfo.InvariantCulture, out var seconds))
        {
            ShutdownTimeout = TimeSpan.FromSeconds(seconds);
        }
    }
示例#4
0
    private void EnsureServer()
    {
        Debug.Assert(_applicationServices != null, "Initialize must be called first.");

        if (Server == null)
        {
            Server = _applicationServices.GetRequiredService <IServer>();

            var serverAddressesFeature = Server.Features?.Get <IServerAddressesFeature>();
            var addresses = serverAddressesFeature?.Addresses;
            if (addresses != null && !addresses.IsReadOnly && addresses.Count == 0)
            {
                var urls = _config[WebHostDefaults.ServerUrlsKey] ?? _config[DeprecatedServerUrlsKey];
                if (!string.IsNullOrEmpty(urls))
                {
                    serverAddressesFeature !.PreferHostingUrls = WebHostUtilities.ParseBool(_config[WebHostDefaults.PreferHostingUrlsKey]);

                    foreach (var value in urls.Split(';', StringSplitOptions.RemoveEmptyEntries))
                    {
                        addresses.Add(value);
                    }
                }
            }
        }
    }
示例#5
0
        // lifted shamelessly from https://github.com/aspnet/AspNetCore/blob/master/src/Hosting/Hosting/src/GenericHost/GenericWebHostedService.cs#L66
        public ICollection <string> GentlyGetEndpointAddresses()
        {
            IServerAddressesFeature serverAddressesFeature = server.Features?.Get <IServerAddressesFeature>();
            ICollection <string>    addresses = serverAddressesFeature?.Addresses;

            if (addresses != null && !addresses.IsReadOnly && addresses.Count == 0)
            {
                string urls = configuration[WebHostDefaults.ServerUrlsKey];
                if (!string.IsNullOrEmpty(urls))
                {
                    serverAddressesFeature.PreferHostingUrls = WebHostUtilities.ParseBool(configuration, WebHostDefaults.PreferHostingUrlsKey);

                    foreach (string value in urls.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        addresses.Add(value);
                    }
                }
            }

            return(addresses);
        }
示例#6
0
        private void EnsureServer()
        {
            if (Server == null)
            {
                Server = Services.GetRequiredService <IServer>();

                var serverAddressesFeature = Server.Features?.Get <IServerAddressesFeature>();
                var addresses = serverAddressesFeature?.Addresses;
                if (addresses != null && !addresses.IsReadOnly && addresses.Count == 0)
                {
                    var urls = _config[WebHostDefaults.ServerUrlsKey];
                    if (!string.IsNullOrEmpty(urls))
                    {
                        serverAddressesFeature.PreferHostingUrls = WebHostUtilities.ParseBool(_config, WebHostDefaults.PreferHostingUrlsKey);

                        foreach (var value in urls.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
                        {
                            addresses.Add(value);
                        }
                    }
                }
            }
        }
示例#7
0
    public async Task StartAsync(CancellationToken cancellationToken)
    {
        HostingEventSource.Log.HostStart();

        var serverAddressesFeature = Server.Features.Get <IServerAddressesFeature>();
        var addresses = serverAddressesFeature?.Addresses;

        if (addresses != null && !addresses.IsReadOnly && addresses.Count == 0)
        {
            var urls = Configuration[WebHostDefaults.ServerUrlsKey];
            if (!string.IsNullOrEmpty(urls))
            {
                serverAddressesFeature !.PreferHostingUrls = WebHostUtilities.ParseBool(Configuration, WebHostDefaults.PreferHostingUrlsKey);

                foreach (var value in urls.Split(';', StringSplitOptions.RemoveEmptyEntries))
                {
                    addresses.Add(value);
                }
            }
        }

        RequestDelegate?application = null;

        try
        {
            var configure = Options.ConfigureApplication;

            if (configure == null)
            {
                throw new InvalidOperationException($"No application configured. Please specify an application via IWebHostBuilder.UseStartup, IWebHostBuilder.Configure, or specifying the startup assembly via {nameof(WebHostDefaults.StartupAssemblyKey)} in the web host configuration.");
            }

            var builder = ApplicationBuilderFactory.CreateBuilder(Server.Features);

            foreach (var filter in StartupFilters.Reverse())
            {
                configure = filter.Configure(configure);
            }

            configure(builder);

            // Build the request pipeline
            application = builder.Build();
        }
        catch (Exception ex)
        {
            Logger.ApplicationError(ex);

            if (!Options.WebHostOptions.CaptureStartupErrors)
            {
                throw;
            }

            var showDetailedErrors = HostingEnvironment.IsDevelopment() || Options.WebHostOptions.DetailedErrors;

            application = ErrorPageBuilder.BuildErrorPageApplication(HostingEnvironment.ContentRootFileProvider, Logger, showDetailedErrors, ex);
        }

        var httpApplication = new HostingApplication(application, Logger, DiagnosticListener, ActivitySource, Propagator, HttpContextFactory);

        await Server.StartAsync(httpApplication, cancellationToken);

        if (addresses != null)
        {
            foreach (var address in addresses)
            {
                Log.ListeningOnAddress(LifetimeLogger, address);
            }
        }

        if (Logger.IsEnabled(LogLevel.Debug))
        {
            foreach (var assembly in Options.WebHostOptions.GetFinalHostingStartupAssemblies())
            {
                Log.StartupAssemblyLoaded(Logger, assembly);
            }
        }

        if (Options.HostingStartupExceptions != null)
        {
            foreach (var exception in Options.HostingStartupExceptions.InnerExceptions)
            {
                Logger.HostingStartupAssemblyError(exception);
            }
        }
    }