Exemplo n.º 1
0
        public void AddServices(IServiceCollection services)
        {
            services.AddSingleton <IStorage, MySqlStorage>();
            services.AddScoped <IStorageConnection, MySqlStorageConnection>();
            services.AddScoped <ICapPublisher, CapPublisher>();
            services.AddTransient <ICallbackPublisher, CapPublisher>();
            services.AddTransient <IAdditionalProcessor, DefaultAdditionalProcessor>();

            var mysqlOptions = new MySqlOptions();

            _configure(mysqlOptions);

            if (mysqlOptions.DbContextType != null)
            {
                services.AddSingleton(x =>
                {
                    using (var scope = x.CreateScope())
                    {
                        var provider  = scope.ServiceProvider;
                        var dbContext = (DbContext)provider.GetService(mysqlOptions.DbContextType);
                        mysqlOptions.ConnectionString = dbContext.Database.GetDbConnection().ConnectionString;
                        return(mysqlOptions);
                    }
                });
            }
            else
            {
                services.AddSingleton(mysqlOptions);
            }
        }
        public void AddServices(IServiceCollection services)
        {
            services.AddSingleton <IStorage, MySqlStorage>();
            services.AddScoped <IStorageConnection, MySqlStorageConnection>();
            services.AddScoped <ICapPublisher, CapPublisher>();
            services.AddTransient <IAdditionalProcessor, DefaultAdditionalProcessor>();

            var mysqlOptions = new MySqlOptions();

            _configure(mysqlOptions);

            if (mysqlOptions.DbContextType != null)
            {
                var provider     = TempBuildService(services);
                var dbContextObj = provider.GetService(mysqlOptions.DbContextType);
                var dbContext    = (DbContext)dbContextObj;
                mysqlOptions.ConnectionString = dbContext.Database.GetDbConnection().ConnectionString;
            }
            services.AddSingleton(mysqlOptions);
        }
        private void AddSingletionMySqlOptions(IServiceCollection services)
        {
            var mysqlOptions = new MySqlOptions();

            _configure(mysqlOptions);

            if (mysqlOptions.DbContextType != null)
            {
                services.AddSingleton(x =>
                {
                    using (var scope = x.CreateScope())
                    {
                        var provider  = scope.ServiceProvider;
                        var dbContext = (DbContext)provider.GetService(mysqlOptions.DbContextType);
                        mysqlOptions.ConnectionString = dbContext.Database.GetDbConnection().ConnectionString;
                        return(mysqlOptions);
                    }
                });
            }
            else
            {
                services.AddSingleton(mysqlOptions);
            }
        }