public ServerServiceContext(
     SelfServerOptions selfOptions,
     IOptionsMonitor <LoggerOptions> loggerOptions,
     IOptionsMonitor <MonitorOptions> monitorOptions,
     IServiceManagementProvider serviceManagementProvider,
     IMonitorProvider monitorProvider,
     ILogEventProvider logEventProvider)
 {
     _loggerOptions             = loggerOptions;
     _monitorOptions            = monitorOptions;
     _serviceManagementProvider = serviceManagementProvider;
     _monitorProvider           = monitorProvider;
     _logEventProvider          = logEventProvider;
     _selfOptions   = selfOptions;
     runningVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
 }
        public static IServiceCollection ActAsServer(
            this IServiceCollection services,
            IConfiguration configuration,
            Action <SelfServerOptions> action = null)
        {
            var selfServerOptions = new SelfServerOptions();

            if (action != null)
            {
                action.Invoke(selfServerOptions);
            }

            if (selfServerOptions.EnableDatabaseOptions)
            {
                services.Configure <DatabaseOptions>(configuration.GetSection("DatabaseOptions"));
            }

            if (selfServerOptions.EnableServiceMonitor)
            {
                services.Configure <MonitorOptions>(configuration.GetSection("MonitorOptions"));
                var monitorOptions = configuration.GetSection("MonitorOptions").Get <MonitorOptions>();
                services.AddSingleton <IMonitorHealthCheck, MonitorHealthCheck>();
                if (monitorOptions.Enable)
                {
                    if (monitorOptions.NotifyOptions.Enable)
                    {
                        services.Configure <Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckPublisherOptions>(opts =>
                        {
                            opts.Delay = TimeSpan.FromSeconds(monitorOptions.NotifyOptions.Delay);
                        });

                        services.AddSingleton <IHealthCheckPublisher, ServerMonitorHealthCheckPublisher>();
                    }
                    services.AddHealthChecks().AddCheck <ServerMonitorHealthCheck>(Constants.SERVER_MONITOR_HEALTHCHECK);
                }
            }

            if (selfServerOptions.EnableSerilog)
            {
                services.Configure <LoggerOptions>(configuration.GetSection("LoggerOptions"));
                Log.Logger = new LoggerConfiguration()
                             .ReadFrom.Configuration(configuration)
                             .Enrich.WithProperty("ServiceName", selfServerOptions.ServerName)
                             .CreateLogger();

                services.AddSingleton(Log.Logger);
                services.AddTransient(typeof(IServiceLogger <>), typeof(ServiceLogger <>));
            }

            if (selfServerOptions.EnableBuiltInCors)
            {
                var corsOptions = configuration.GetSection("CorsPortalOptions").Get <Core.Configurations.CorsPortalOptions>();
                services.AddCors(
                    option =>
                {
                    option.AddPolicy(SERVER_CORS, corsBuilder =>
                    {
                        if (corsOptions.AllowAny)
                        {
                            corsBuilder.AllowAnyHeader()
                            .AllowAnyMethod()
                            .AllowAnyOrigin()
                            .WithExposedHeaders(corsOptions.ExposedHeaders.ToArray());
                        }
                        else
                        {
                            if (corsOptions.AllowCredentials)
                            {
                                corsBuilder
                                .AllowCredentials();
                            }

                            if (corsOptions.AllowAnyHeader)
                            {
                                corsBuilder.AllowAnyHeader();
                            }
                            else
                            {
                                corsBuilder.WithHeaders(corsOptions.AllowedHeaders.ToArray());
                            }

                            if (corsOptions.AllowAnyMethod)
                            {
                                corsBuilder.AllowAnyMethod();
                            }
                            else
                            {
                                corsBuilder.WithMethods(corsOptions.AllowedMethods.ToArray());
                            }

                            if (corsOptions.AllowAnyHost && !corsOptions.AllowCredentials)
                            {
                                corsBuilder.AllowAnyOrigin();
                            }
                            else
                            {
                                corsBuilder.WithOrigins(corsOptions.AllowedHosts.ToArray());
                            }

                            if (corsOptions.ExposedHeaders != null)
                            {
                                corsBuilder.WithExposedHeaders(corsOptions.ExposedHeaders.ToArray());
                            }
                        }
                    });
                });
            }

            services.Configure <ServiceManagementOptions>(configuration.GetSection("ServiceManagementOptions"));
            services.Configure <CentralizedLogOptions>(configuration.GetSection("CentralizedLogOptions"));
            services.AddSingleton(a => selfServerOptions);
            services.AddSingleton <IServiceContext, ServerServiceContext>();



            services.AddSingleton <IServiceManagementProvider, ServiceManagamentProvider>();
            services.AddSingleton <IMonitorServiceReportProvider, MonitorServiceReportProvider>();
            services.AddSingleton <ILogEventProvider, LogEventProvider>();
            services.AddSingleton <IMonitorProvider, MonitorProvider>();

            services.AddHostedService <CheckingLostServicesBackgroundTask>();
            services.AddHostedService <CheckingShutdownServicesBackgroundTask>();
            services.AddHostedService <GatherAllHardwareCounterServicesBackgroundTask>();

            return(services);
        }