internal static string GetDefaultHostId(IEnvironment environment, ScriptApplicationHostOptions scriptOptions)
        {
            // We're setting the default here on the newly created configuration
            // If the user has explicitly set the HostID via host.json, it will overwrite
            // what we set here
            string hostId = null;

            if (environment.IsAppService() || environment.IsKubernetesManagedHosting())
            {
                string uniqueSlotName = environment?.GetAzureWebsiteUniqueSlotName();
                if (!string.IsNullOrEmpty(uniqueSlotName))
                {
                    // If running on Azure Web App, derive the host ID from unique site slot name
                    hostId = uniqueSlotName;
                }
            }
            else if (environment.IsLinuxConsumption())
            {
                // The hostid is derived from the hostname for Linux consumption.
                string hostName = environment.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteHostName);
                hostId = hostName?.Replace(".azurewebsites.net", string.Empty);
            }
            else
            {
                // When running locally, derive a stable host ID from machine name
                // and root path. We use a hash rather than the path itself to ensure
                // IDs differ (due to truncation) between folders that may share the same
                // root path prefix.
                // Note that such an ID won't work in distributed scenarios, so should
                // only be used for local/CLI scenarios.
                string sanitizedMachineName = Environment.MachineName
                                              .Where(char.IsLetterOrDigit)
                                              .Aggregate(new StringBuilder(), (b, c) => b.Append(c)).ToString();
                hostId = $"{sanitizedMachineName}-{Math.Abs(Utility.GetStableHash(scriptOptions.ScriptPath))}";
            }

            if (!string.IsNullOrEmpty(hostId))
            {
                if (hostId.Length > ScriptConstants.MaximumHostIdLength)
                {
                    // Truncate to the max host name length if needed
                    hostId = hostId.Substring(0, ScriptConstants.MaximumHostIdLength);
                }
            }

            // Lowercase and trim any trailing '-' as they can cause problems with queue names
            return(hostId?.ToLowerInvariant().TrimEnd('-'));
        }
示例#2
0
        public static IHostBuilder AddScriptHostCore(this IHostBuilder builder, ScriptApplicationHostOptions applicationHostOptions, Action <IWebJobsBuilder> configureWebJobs = null)
        {
            var skipHostInitialization = builder.Properties.ContainsKey(ScriptConstants.SkipHostInitializationKey);

            builder.ConfigureWebJobs(webJobsBuilder =>
            {
                // Built in binding registrations
                webJobsBuilder.AddExecutionContextBinding(o =>
                {
                    o.AppDirectory = applicationHostOptions.ScriptPath;
                })
                .AddHttp(o =>
                {
                    o.SetResponse = HttpBinding.SetResponse;
                })
                .AddTimers()
                .AddManualTrigger();

                if (!skipHostInitialization)
                {
                    // Only set our external startup if we're not suppressing host initialization
                    // as we don't want to load user assemblies otherwise.
                    webJobsBuilder.UseScriptExternalStartup(applicationHostOptions.ScriptPath);
                }

                configureWebJobs?.Invoke(webJobsBuilder);
            }, o => o.AllowPartialHostStartup = true);

            // Script host services - these services are scoped to a host instance, and when a new host
            // is created, these services are recreated
            builder.ConfigureServices(services =>
            {
                // Core WebJobs/Script Host services
                services.AddSingleton <ScriptHost>();
                services.AddSingleton <IScriptJobHost>(p => p.GetRequiredService <ScriptHost>());
                services.AddSingleton <IJobHost>(p => p.GetRequiredService <ScriptHost>());
                services.AddSingleton <IFunctionMetadataManager, FunctionMetadataManager>();
                services.AddSingleton <IProxyMetadataManager, ProxyMetadataManager>();
                services.AddSingleton <ITypeLocator, ScriptTypeLocator>();
                services.AddSingleton <ScriptSettingsManager>();
                services.AddTransient <IExtensionsManager, ExtensionsManager>();
                services.TryAddSingleton <IMetricsLogger, MetricsLogger>();
                services.TryAddSingleton <IScriptJobHostEnvironment, ConsoleScriptJobHostEnvironment>();

                // Script binding providers
                services.TryAddEnumerable(ServiceDescriptor.Singleton <IScriptBindingProvider, WebJobsCoreScriptBindingProvider>());
                services.TryAddEnumerable(ServiceDescriptor.Singleton <IScriptBindingProvider, CoreExtensionsScriptBindingProvider>());
                services.TryAddEnumerable(ServiceDescriptor.Singleton <IScriptBindingProvider, GeneralScriptBindingProvider>());

                // Configuration
                services.AddSingleton <IOptions <ScriptApplicationHostOptions> >(new OptionsWrapper <ScriptApplicationHostOptions>(applicationHostOptions));
                services.AddSingleton <IOptionsMonitor <ScriptApplicationHostOptions> >(new ScriptApplicationHostOptionsMonitor(applicationHostOptions));
                services.ConfigureOptions <ScriptHostOptionsSetup>();
                services.ConfigureOptions <JobHostFunctionTimeoutOptionsSetup>();
                // TODO: pgopa only add this to WebHostServiceCollection
                services.ConfigureOptions <LanguageWorkerOptionsSetup>();
                services.AddOptions <FunctionResultAggregatorOptions>()
                .Configure <IConfiguration>((o, c) =>
                {
                    c.GetSection(ConfigurationSectionNames.JobHost)
                    .GetSection(ConfigurationSectionNames.Aggregator)
                    .Bind(o);
                });

                services.AddSingleton <IFileLoggingStatusManager, FileLoggingStatusManager>();
                services.AddSingleton <IPrimaryHostStateProvider, PrimaryHostStateProvider>();

                if (!applicationHostOptions.HasParentScope)
                {
                    AddCommonServices(services);
                }

                // Hosted services
                services.TryAddEnumerable(ServiceDescriptor.Singleton <IHostedService, PrimaryHostCoordinator>());
            });

            return(builder);
        }
示例#3
0
 public static IHostBuilder AddScriptHost(this IHostBuilder builder, ScriptApplicationHostOptions applicationOptions, Action <IWebJobsBuilder> configureWebJobs = null)
 => builder.AddScriptHost(applicationOptions, null, configureWebJobs);
示例#4
0
        public static IHostBuilder AddScriptHost(this IHostBuilder builder,
                                                 ScriptApplicationHostOptions applicationOptions,
                                                 ILoggerFactory loggerFactory,
                                                 IMetricsLogger metricsLogger,
                                                 Action <IWebJobsBuilder> configureWebJobs = null)
        {
            loggerFactory = loggerFactory ?? NullLoggerFactory.Instance;

            builder.SetAzureFunctionsConfigurationRoot();
            // Host configuration
            builder.ConfigureLogging((context, loggingBuilder) =>
            {
                loggingBuilder.AddDefaultWebJobsFilters();

                string loggingPath = ConfigurationPath.Combine(ConfigurationSectionNames.JobHost, "Logging");
                loggingBuilder.AddConfiguration(context.Configuration.GetSection(loggingPath));

                loggingBuilder.Services.AddSingleton <IFileWriterFactory, DefaultFileWriterFactory>();
                loggingBuilder.Services.AddSingleton <ILoggerProvider, HostFileLoggerProvider>();
                loggingBuilder.Services.AddSingleton <ILoggerProvider, FunctionFileLoggerProvider>();

                loggingBuilder.AddConsoleIfEnabled(context);

                ConfigureApplicationInsights(context, loggingBuilder);
            })
            .ConfigureAppConfiguration((context, configBuilder) =>
            {
                if (!context.Properties.ContainsKey(ScriptConstants.SkipHostJsonConfigurationKey))
                {
                    configBuilder.Add(new HostJsonFileConfigurationSource(applicationOptions, SystemEnvironment.Instance, loggerFactory, metricsLogger));
                }
            });

            // WebJobs configuration
            builder.AddScriptHostCore(applicationOptions, configureWebJobs, loggerFactory);

            // Allow FunctionsStartup to add configuration after all other configuration is registered.
            builder.ConfigureAppConfiguration((context, configBuilder) =>
            {
                // Pre-build configuration here to load bundles and to store for later validation.
                var config = configBuilder.Build();
                var extensionBundleOptions = GetExtensionBundleOptions(config);
                var bundleManager          = new ExtensionBundleManager(extensionBundleOptions, SystemEnvironment.Instance, loggerFactory);
                var metadataServiceManager = applicationOptions.RootServiceProvider.GetService <IFunctionMetadataManager>();
                var languageWorkerOptions  = applicationOptions.RootServiceProvider.GetService <IOptions <LanguageWorkerOptions> >();

                var locator = new ScriptStartupTypeLocator(applicationOptions.ScriptPath, loggerFactory.CreateLogger <ScriptStartupTypeLocator>(), bundleManager, metadataServiceManager, metricsLogger, languageWorkerOptions);

                // The locator (and thus the bundle manager) need to be created now in order to configure app configuration.
                // Store them so they do not need to be re-created later when configuring services.
                context.Properties[BundleManagerKey]      = bundleManager;
                context.Properties[StartupTypeLocatorKey] = locator;

                // If we're skipping host initialization, this key will not exist and this will also be skipped.
                if (context.Properties.TryGetValue(DelayedConfigurationActionKey, out object actionObject) &&
                    actionObject is Action <IWebJobsStartupTypeLocator> delayedConfigAction)
                {
                    context.Properties.Remove(DelayedConfigurationActionKey);

                    delayedConfigAction(locator);

                    // store the snapshot for validation later, but only if there
                    // are any registered external configuration startups.
                    if (locator.HasExternalConfigurationStartups())
                    {
                        context.Properties[ConfigurationSnapshotKey] = config;
                    }
                }
            });

            return(builder);
        }
示例#5
0
        public static IHostBuilder AddScriptHostCore(this IHostBuilder builder, ScriptApplicationHostOptions applicationHostOptions, Action <IWebJobsBuilder> configureWebJobs = null, ILoggerFactory loggerFactory = null)
        {
            var skipHostInitialization = builder.Properties.ContainsKey(ScriptConstants.SkipHostInitializationKey);

            builder.ConfigureServices((context, services) =>
            {
                services.AddSingleton <ExternalConfigurationStartupValidator>();
                services.AddSingleton <IHostedService>(s =>
                {
                    if (!skipHostInitialization)
                    {
                        var environment = s.GetService <IEnvironment>();

                        // This key will not be here if we don't have any external configuration startups registered
                        if (context.Properties.TryGetValue(ConfigurationSnapshotKey, out object originalConfigObject) &&
                            originalConfigObject is IConfigurationRoot originalConfig)
                        {
                            context.Properties.Remove(ConfigurationSnapshotKey);

                            // Validate the config for anything that needs the Scale Controller.
                            // Including Core Tools as a warning during development time.
                            if (environment.IsWindowsConsumption() ||
                                environment.IsLinuxConsumption() ||
                                (environment.IsWindowsElasticPremium() && !environment.IsRuntimeScaleMonitoringEnabled()) ||
                                environment.IsCoreTools())
                            {
                                var validator = s.GetService <ExternalConfigurationStartupValidator>();
                                var logger    = s.GetService <ILoggerFactory>().CreateLogger <ExternalConfigurationStartupValidator>();

                                return(new ExternalConfigurationStartupValidatorService(validator, originalConfig, environment, logger));
                            }
                        }
                    }

                    return(NullHostedService.Instance);
                });

                // Wire this up early so that any early worker logs are guaranteed to be flushed if any other
                // IHostedService has a slow startup.
                services.AddSingleton <IHostedService, WorkerConsoleLogService>();
            });

            builder.ConfigureWebJobs((context, webJobsBuilder) =>
            {
                // Built in binding registrations
                webJobsBuilder.AddExecutionContextBinding(o =>
                {
                    o.AppDirectory = applicationHostOptions.ScriptPath;
                })
                .AddHttp()
                .AddTimersWithStorage()
                .AddManualTrigger()
                .AddWarmup();

                var bundleManager = context.Properties.GetAndRemove <IExtensionBundleManager>(BundleManagerKey);
                webJobsBuilder.Services.AddSingleton <IExtensionBundleManager>(_ => bundleManager);

                if (!skipHostInitialization)
                {
                    var webJobsBuilderContext = new WebJobsBuilderContext
                    {
                        Configuration       = context.Configuration,
                        EnvironmentName     = context.HostingEnvironment.EnvironmentName,
                        ApplicationRootPath = applicationHostOptions.ScriptPath
                    };

                    // Only set our external startup if we're not suppressing host initialization
                    // as we don't want to load user assemblies otherwise.
                    var locator = context.Properties.GetAndRemove <ScriptStartupTypeLocator>(StartupTypeLocatorKey);
                    webJobsBuilder.UseExternalStartup(locator, webJobsBuilderContext, loggerFactory);
                }

                configureWebJobs?.Invoke(webJobsBuilder);
            }, o => o.AllowPartialHostStartup = true,
                                     (context, webJobsConfigBuilder) =>
            {
                if (!skipHostInitialization)
                {
                    var webJobsBuilderContext = new WebJobsBuilderContext
                    {
                        Configuration       = context.Configuration,
                        EnvironmentName     = context.HostingEnvironment.EnvironmentName,
                        ApplicationRootPath = applicationHostOptions.ScriptPath
                    };

                    // Delay this call so we can call the customer's setup last.
                    context.Properties[DelayedConfigurationActionKey] = new Action <IWebJobsStartupTypeLocator>(locator => webJobsConfigBuilder.UseExternalConfigurationStartup(locator, webJobsBuilderContext, loggerFactory));
                }
            });

            // Script host services - these services are scoped to a host instance, and when a new host
            // is created, these services are recreated
            builder.ConfigureServices(services =>
            {
                // Core WebJobs/Script Host services
                services.AddSingleton <ScriptHost>();

                // HTTP Worker
                services.AddSingleton <IHttpWorkerProcessFactory, HttpWorkerProcessFactory>();
                services.AddSingleton <IHttpWorkerChannelFactory, HttpWorkerChannelFactory>();
                services.AddSingleton <IHttpWorkerService, DefaultHttpWorkerService>();
                // Rpc Worker
                services.AddSingleton <IJobHostRpcWorkerChannelManager, JobHostRpcWorkerChannelManager>();
                services.AddSingleton <IRpcFunctionInvocationDispatcherLoadBalancer, RpcFunctionInvocationDispatcherLoadBalancer>();

                //Worker Function Invocation dispatcher
                services.AddSingleton <IFunctionInvocationDispatcherFactory, FunctionInvocationDispatcherFactory>();
                services.AddSingleton <IScriptJobHost>(p => p.GetRequiredService <ScriptHost>());
                services.AddSingleton <IJobHost>(p => p.GetRequiredService <ScriptHost>());
                services.AddSingleton <IHostedService, WorkerConcurrencyManager>();

                services.AddSingleton <ITypeLocator, ScriptTypeLocator>();
                services.AddSingleton <ScriptSettingsManager>();
                services.AddTransient <IExtensionsManager, ExtensionsManager>();
                services.TryAddSingleton <IHttpRoutesManager, DefaultHttpRouteManager>();
                services.TryAddSingleton <IMetricsLogger, MetricsLogger>();
                services.AddTransient <IExtensionBundleContentProvider, ExtensionBundleContentProvider>();

                // Script binding providers
                services.TryAddEnumerable(ServiceDescriptor.Singleton <IScriptBindingProvider, WebJobsCoreScriptBindingProvider>());
                services.TryAddEnumerable(ServiceDescriptor.Singleton <IScriptBindingProvider, CoreExtensionsScriptBindingProvider>());
                services.TryAddEnumerable(ServiceDescriptor.Singleton <IScriptBindingProvider, GeneralScriptBindingProvider>());

                // Configuration
                services.AddSingleton <IOptions <ScriptApplicationHostOptions> >(new OptionsWrapper <ScriptApplicationHostOptions>(applicationHostOptions));
                services.AddSingleton <IOptionsMonitor <ScriptApplicationHostOptions> >(new ScriptApplicationHostOptionsMonitor(applicationHostOptions));
                services.ConfigureOptions <ScriptJobHostOptionsSetup>();
                services.ConfigureOptions <JobHostFunctionTimeoutOptionsSetup>();
                // LanguageWorkerOptionsSetup should be registered in WebHostServiceCollection as well to enable starting worker processing in placeholder mode.
                services.ConfigureOptions <LanguageWorkerOptionsSetup>();
                services.AddOptions <WorkerConcurrencyOptions>();
                services.ConfigureOptions <HttpWorkerOptionsSetup>();
                services.ConfigureOptions <ManagedDependencyOptionsSetup>();
                services.AddOptions <FunctionResultAggregatorOptions>()
                .Configure <IConfiguration>((o, c) =>
                {
                    c.GetSection(ConfigurationSectionNames.JobHost)
                    .GetSection(ConfigurationSectionNames.Aggregator)
                    .Bind(o);
                });
                services.AddOptions <ScaleOptions>()
                .Configure <IConfiguration>((o, c) =>
                {
                    c.GetSection(ConfigurationSectionNames.JobHost)
                    .GetSection(ConfigurationSectionNames.Scale)
                    .Bind(o);
                });
                services.AddSingleton <IFunctionsHostingConfiguration, FunctionsHostingConfiguration>();

                services.AddSingleton <IFileLoggingStatusManager, FileLoggingStatusManager>();

                if (!applicationHostOptions.HasParentScope)
                {
                    AddCommonServices(services);
                }

                if (SystemEnvironment.Instance.IsKubernetesManagedHosting())
                {
                    services.AddSingleton <IDistributedLockManager, KubernetesDistributedLockManager>();
                }

                services.TryAddEnumerable(ServiceDescriptor.Singleton <IHostedService, FunctionInvocationDispatcherShutdownManager>());

                if (SystemEnvironment.Instance.IsRuntimeScaleMonitoringEnabled())
                {
                    services.TryAddEnumerable(ServiceDescriptor.Singleton <IHostedService, FunctionsScaleMonitorService>());
                }
                services.TryAddSingleton <FunctionsScaleManager>();

                services.AddSingleton <IHostOptionsProvider, HostOptionsProvider>();
            });

            RegisterFileProvisioningService(builder);
            return(builder);
        }
示例#6
0
        public static IHostBuilder AddScriptHostCore(this IHostBuilder builder, ScriptApplicationHostOptions applicationHostOptions, Action <IWebJobsBuilder> configureWebJobs = null)
        {
            builder.ConfigureWebJobs(webJobsBuilder =>
            {
                // Built in binding registrations
                webJobsBuilder.AddExecutionContextBinding(o =>
                {
                    o.AppDirectory = applicationHostOptions.ScriptPath;
                })
                .AddHttp(o =>
                {
                    o.SetResponse = HttpBinding.SetResponse;
                })
                .AddTimers()
                .AddManualTrigger();

                webJobsBuilder.UseScriptExternalStartup(applicationHostOptions.ScriptPath);

                configureWebJobs?.Invoke(webJobsBuilder);
            }, o => o.AllowPartialHostStartup = true);

            // Script host services
            builder.ConfigureServices(services =>
            {
                // Core WebJobs/Script Host services
                services.AddSingleton <ScriptHost>();
                services.AddSingleton <IScriptJobHost>(p => p.GetRequiredService <ScriptHost>());
                services.AddSingleton <IJobHost>(p => p.GetRequiredService <ScriptHost>());
                services.AddSingleton <IFunctionMetadataManager, FunctionMetadataManager>();
                services.AddSingleton <IProxyMetadataManager, ProxyMetadataManager>();
                services.AddSingleton <ITypeLocator, ScriptTypeLocator>();
                services.AddSingleton <ScriptSettingsManager>();
                services.AddTransient <IExtensionsManager, ExtensionsManager>();
                services.TryAddSingleton <IMetricsLogger, MetricsLogger>();
                services.TryAddSingleton <IScriptJobHostEnvironment, ConsoleScriptJobHostEnvironment>();
                services.TryAddSingleton <HostPerformanceManager>();

                // Script binding providers
                services.TryAddEnumerable(ServiceDescriptor.Singleton <IScriptBindingProvider, WebJobsCoreScriptBindingProvider>());
                services.TryAddEnumerable(ServiceDescriptor.Singleton <IScriptBindingProvider, CoreExtensionsScriptBindingProvider>());
                services.TryAddEnumerable(ServiceDescriptor.Singleton <IScriptBindingProvider, GeneralScriptBindingProvider>());

                // Configuration
                services.AddSingleton <IOptions <ScriptApplicationHostOptions> >(new OptionsWrapper <ScriptApplicationHostOptions>(applicationHostOptions));
                services.AddSingleton <IOptionsMonitor <ScriptApplicationHostOptions> >(new ScriptApplicationHostOptionsMonitor(applicationHostOptions));
                services.ConfigureOptions <ScriptHostOptionsSetup>();
                services.ConfigureOptions <HostHealthMonitorOptionsSetup>();
                services.ConfigureOptions <JobHostFunctionTimeoutOptionsSetup>();
                // TODO: pgopa only add this to WebHostServiceCollection
                services.ConfigureOptions <LanguageWorkerOptionsSetup>();
                services.AddOptions <FunctionResultAggregatorOptions>()
                .Configure <IConfiguration>((o, c) =>
                {
                    c.GetSection(ConfigurationSectionNames.JobHost)
                    .GetSection(ConfigurationSectionNames.Aggregator)
                    .Bind(o);
                });

                services.AddSingleton <IFileLoggingStatusManager, FileLoggingStatusManager>();
                services.AddSingleton <IPrimaryHostStateProvider, PrimaryHostStateProvider>();

                if (!applicationHostOptions.HasParentScope)
                {
                    AddCommonServices(services);
                }

                // Hosted services
                services.TryAddEnumerable(ServiceDescriptor.Singleton <IHostedService, PrimaryHostCoordinator>());
            });

            return(builder);
        }
示例#7
0
        public static IWebJobsBuilder UseScriptExternalStartup(this IWebJobsBuilder builder, ScriptApplicationHostOptions applicationHostOptions, ILoggerFactory loggerFactory, IExtensionBundleManager extensionBundleManager)
        {
            var logger = loggerFactory?.CreateLogger <ScriptStartupTypeLocator>() ?? throw new ArgumentNullException(nameof(loggerFactory));
            var metadataServiceProvider = applicationHostOptions.RootServiceProvider.GetService <IFunctionMetadataProvider>();

            return(builder.UseExternalStartup(new ScriptStartupTypeLocator(applicationHostOptions.ScriptPath, logger, extensionBundleManager, metadataServiceProvider)));
        }
        public static IHostBuilder AddScriptHostCore(this IHostBuilder builder, ScriptApplicationHostOptions applicationHostOptions, IMetricsLogger metricsLogger, Action <IWebJobsBuilder> configureWebJobs = null, ILoggerFactory loggerFactory = null)
        {
            var skipHostInitialization = builder.Properties.ContainsKey(ScriptConstants.SkipHostInitializationKey);

            builder.ConfigureWebJobs((context, webJobsBuilder) =>
            {
                // Built in binding registrations
                webJobsBuilder.AddExecutionContextBinding(o =>
                {
                    o.AppDirectory = applicationHostOptions.ScriptPath;
                })
                .AddHttp(o =>
                {
                    o.SetResponse = HttpBinding.SetResponse;
                })
                .AddTimers()
                .AddManualTrigger()
                .AddWarmup();

                var extensionBundleOptions = GetExtensionBundleOptions(context);
                var bundleManager          = new ExtensionBundleManager(extensionBundleOptions, SystemEnvironment.Instance, loggerFactory);
                webJobsBuilder.Services.AddSingleton <IExtensionBundleManager>(_ => bundleManager);
                if (!skipHostInitialization)
                {
                    // Only set our external startup if we're not suppressing host initialization
                    // as we don't want to load user assemblies otherwise.
                    webJobsBuilder.UseScriptExternalStartup(applicationHostOptions, loggerFactory, bundleManager, metricsLogger);
                }

                configureWebJobs?.Invoke(webJobsBuilder);
            }, o => o.AllowPartialHostStartup = true);

            // Script host services - these services are scoped to a host instance, and when a new host
            // is created, these services are recreated
            builder.ConfigureServices(services =>
            {
                // Core WebJobs/Script Host services
                services.AddSingleton <ScriptHost>();

                // HTTP Worker
                services.AddSingleton <IHttpWorkerProcessFactory, HttpWorkerProcessFactory>();
                services.AddSingleton <IHttpWorkerChannelFactory, HttpWorkerChannelFactory>();
                services.AddSingleton <IHttpWorkerService, DefaultHttpWorkerService>();
                // Rpc Worker
                services.AddSingleton <IJobHostRpcWorkerChannelManager, JobHostRpcWorkerChannelManager>();
                services.AddSingleton <IRpcFunctionInvocationDispatcherLoadBalancer, RpcFunctionInvocationDispatcherLoadBalancer>();

                //Worker Function Invocation dispatcher
                services.AddSingleton <IFunctionInvocationDispatcherFactory, FunctionInvocationDispatcherFactory>();

                services.AddSingleton <IScriptJobHost>(p => p.GetRequiredService <ScriptHost>());
                services.AddSingleton <IJobHost>(p => p.GetRequiredService <ScriptHost>());
                services.AddSingleton <IFunctionMetadataManager, FunctionMetadataManager>();
                services.AddSingleton <IProxyMetadataManager, ProxyMetadataManager>();
                services.AddSingleton <ITypeLocator, ScriptTypeLocator>();
                services.AddSingleton <ScriptSettingsManager>();
                services.AddTransient <IExtensionsManager, ExtensionsManager>();
                services.TryAddSingleton <IHttpRoutesManager, DefaultHttpRouteManager>();
                services.TryAddSingleton <IMetricsLogger, MetricsLogger>();
                services.TryAddSingleton <IScriptJobHostEnvironment, ConsoleScriptJobHostEnvironment>();
                services.AddTransient <IExtensionBundleContentProvider, ExtensionBundleContentProvider>();

                // Script binding providers
                services.TryAddEnumerable(ServiceDescriptor.Singleton <IScriptBindingProvider, WebJobsCoreScriptBindingProvider>());
                services.TryAddEnumerable(ServiceDescriptor.Singleton <IScriptBindingProvider, CoreExtensionsScriptBindingProvider>());
                services.TryAddEnumerable(ServiceDescriptor.Singleton <IScriptBindingProvider, GeneralScriptBindingProvider>());

                // Configuration
                services.AddSingleton <IOptions <ScriptApplicationHostOptions> >(new OptionsWrapper <ScriptApplicationHostOptions>(applicationHostOptions));
                services.AddSingleton <IOptionsMonitor <ScriptApplicationHostOptions> >(new ScriptApplicationHostOptionsMonitor(applicationHostOptions));
                services.ConfigureOptions <ScriptHostOptionsSetup>();
                services.ConfigureOptions <JobHostFunctionTimeoutOptionsSetup>();
                // TODO: pgopa only add this to WebHostServiceCollection
                services.ConfigureOptions <LanguageWorkerOptionsSetup>();
                services.ConfigureOptions <HttpWorkerOptionsSetup>();
                services.ConfigureOptions <ManagedDependencyOptionsSetup>();
                services.AddOptions <FunctionResultAggregatorOptions>()
                .Configure <IConfiguration>((o, c) =>
                {
                    c.GetSection(ConfigurationSectionNames.JobHost)
                    .GetSection(ConfigurationSectionNames.Aggregator)
                    .Bind(o);
                });
                services.AddOptions <ScaleOptions>()
                .Configure <IConfiguration>((o, c) =>
                {
                    c.GetSection(ConfigurationSectionNames.JobHost)
                    .GetSection(ConfigurationSectionNames.Scale)
                    .Bind(o);
                });

                services.AddSingleton <IFileLoggingStatusManager, FileLoggingStatusManager>();
                services.AddSingleton <IPrimaryHostStateProvider, PrimaryHostStateProvider>();

                if (!applicationHostOptions.HasParentScope)
                {
                    AddCommonServices(services);
                }

                services.AddSingleton <IHostedService, WorkerConsoleLogService>();
                services.TryAddEnumerable(ServiceDescriptor.Singleton <IHostedService, PrimaryHostCoordinator>());
                services.TryAddEnumerable(ServiceDescriptor.Singleton <IHostedService, FunctionInvocationDispatcherShutdownManager>());

                if (SystemEnvironment.Instance.IsRuntimeScaleMonitoringEnabled())
                {
                    services.TryAddEnumerable(ServiceDescriptor.Singleton <IHostedService, FunctionsScaleMonitorService>());
                }
                services.TryAddSingleton <FunctionsScaleManager>();
            });

            RegisterFileProvisioningService(builder);
            return(builder);
        }