Example #1
0
 /// <summary>
 /// Add a health check for Oracle databases.
 /// </summary>
 /// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
 /// <param name="connectionString">The Oracle connection string to be used.</param>
 /// <param name="healthQuery">The query to be used in check. Optional. If <c>null</c> select * from v$version is used.</param>
 /// <param name="name">The health check name. Optional. If <c>null</c> the type name 'oracle' will be used for the name.</param>
 /// <param name="failureStatus">
 /// The <see cref="HealthStatus"/> that should be reported when the health check fails. Optional. If <c>null</c> then
 /// the default status of <see cref="HealthStatus.Unhealthy"/> will be reported.
 /// </param>
 /// <param name="tags">A list of tags that can be used to filter sets of health checks. Optional.</param>
 /// <param name="timeout">An optional <see cref="TimeSpan"/> representing the timeout of the check.</param>
 /// <returns>The specified <paramref name="builder"/>.</returns>
 public static IHealthChecksBuilder AddOracle(
     this IHealthChecksBuilder builder,
     string connectionString,
     string healthQuery         = HEALTH_QUERY,
     string?name                = default,
     HealthStatus?failureStatus = default,
     IEnumerable <string>?tags  = default,
     TimeSpan?timeout           = default) => builder.AddOracle(_ => connectionString, healthQuery, name, failureStatus, tags, timeout);
 public static void ConfigureOracle(this IServiceCollection services, string connectionString, int poolSize, IHealthChecksBuilder checksBuilder, HealthChecksUIBuilder healthChecksUI)
 {
     services.AddEntityFrameworkOracle();
     services.AddSingleton <IDataBaseModelBuilderOptions>(c => new OracleModelBuilderOptions());
     services.AddDbContextPool <ApplicationDbContext>(builder =>
     {
         builder.UseOracle(connectionString, s => s.MigrationsAssembly("IoTSharp.Data.Oracle"));
         builder.UseInternalServiceProvider(services.BuildServiceProvider());
     }
                                                      , poolSize);
     checksBuilder.AddOracle(connectionString, name: "IoTSharp.Data.Oracle");
     healthChecksUI.AddInMemoryStorage();
 }
        private static void DoAdd(IServiceCollection services, OracleServiceInfo info, IConfiguration config, ServiceLifetime contextLifetime, IHealthChecksBuilder builder)
        {
            Type OracleConnection = ConnectorHelpers.FindType(OracleTypeLocator.Assemblies, OracleTypeLocator.ConnectionTypeNames);
            var  OracleConfig     = new OracleProviderConnectorOptions(config);
            var  factory          = new OracleProviderConnectorFactory(info, OracleConfig, OracleConnection);

            services.Add(new ServiceDescriptor(typeof(IDbConnection), factory.Create, contextLifetime));
            services.Add(new ServiceDescriptor(OracleConnection, factory.Create, contextLifetime));
            if (builder == null)
            {
                services.Add(new ServiceDescriptor(typeof(IHealthContributor), ctx => new SkynetCloudRelationalHealthContributor((IDbConnection)factory.Create(ctx), ctx.GetService <ILogger <SkynetCloudRelationalHealthContributor> >()), ServiceLifetime.Singleton));
            }
            else
            {
                builder.AddOracle(factory.CreateConnectionString());
            }
        }
        /// <summary>
        /// 建立HealthChecks服务
        /// </summary>
        /// <param name="builder">HealthChecks服务创建者</param>
        /// <param name="configuration">应用程序配置</param>
        /// <returns></returns>
        protected virtual IHealthChecksBuilder BuildHealthChecks(IHealthChecksBuilder builder, IConfiguration configuration)
        {
            //system
            long providerMemory    = configuration["OSharp:HealthChecks:PrivateMemory"].CastTo(1000_000_000L);
            long virtualMemorySize = configuration["OSharp:HealthChecks:VirtualMemorySize"].CastTo(1000_000_000L);
            long workingSet        = configuration["OSharp:HealthChecks:WorkingSet"].CastTo(1000_000_000L);

            builder.AddPrivateMemoryHealthCheck(providerMemory);        //最大私有内存
            builder.AddVirtualMemorySizeHealthCheck(virtualMemorySize); //最大虚拟内存
            builder.AddWorkingSetHealthCheck(workingSet);               //最大工作内存

            OsharpOptions options = configuration.GetOsharpOptions();

            //数据库
            foreach (var pair in options.DbContexts.OrderBy(m => m.Value.DatabaseType))
            {
                string connectionString = pair.Value.ConnectionString;
                switch (pair.Value.DatabaseType)
                {
                case DatabaseType.SqlServer:
                    builder.AddSqlServer(connectionString, null, pair.Key);
                    break;

                case DatabaseType.Sqlite:
                    builder.AddSqlite(connectionString, name: pair.Key);
                    break;

                case DatabaseType.MySql:
                    builder.AddMySql(connectionString, pair.Key);
                    break;

                case DatabaseType.PostgreSql:
                    builder.AddNpgSql(connectionString, name: pair.Key);
                    break;

                case DatabaseType.Oracle:
                    builder.AddOracle(connectionString, name: pair.Key);
                    break;

                default:
                    throw new ArgumentOutOfRangeException($"OSharpOptions中 {pair.Value.DatabaseType} 不受支持");
                }
            }

            //SMTP
            if (options.MailSender != null)
            {
                var smtp = options.MailSender;
                builder.AddSmtpHealthCheck(smtpOptions =>
                {
                    smtpOptions.Host = smtp.Host;
                    smtpOptions.LoginWith(smtp.UserName, smtp.Password);
                });
            }

            //Redis
            if (options.Redis != null && options.Redis.Enabled)
            {
                var redis = options.Redis;
                builder.AddRedis(redis.Configuration);
            }

            //Hangfire
            if (configuration["OSharp:Hangfire:Enabled"].CastTo(false))
            {
                builder.AddHangfire(hangfireOptions =>
                {
                    hangfireOptions.MinimumAvailableServers = 1;
                });
            }

            return(builder);
        }