Exemplo n.º 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddMediatR(Assembly.GetExecutingAssembly());

            services.AddDbContext <IDSAppContext>(options =>
            {
                var connectionString = DbConnectionStringBuilder.Build(DBType, DBServer, DBPort, Database, DBUserId, DBPassword);
                options.UseNpgsql(connectionString, sqlContextOptionsBuilder =>
                {
                    sqlContextOptionsBuilder.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
                });
            });
            //services.AddSingleton<IHostedService, DBMigrationService>();
            services.Configure <AppConfig>(Configuration);
            services.AddScoped <IPersistedGrantStore, PersistedGrantStore>();
            services.AddScoped <IIdentityRepository, IdentityRepository>();
            services.AddScoped <IIdentityGrantRepository, IdentityGrantRepository>();
            services.AddScoped <IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();
            services.AddScoped <IProfileService, ProfileService>();
            services.AddPrifileContext();
            services.AddAutoMapper(typeof(Startup));
            services.AddLocalization(o => o.ResourcesPath = "Resources");

            #region Swagger
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("ids", new OpenApiInfo {
                    Title = "IDS API", Version = "v1"
                });
            });
            #endregion

            #region Identity Server ÅäÖÃ
            var builder = services.AddIdentityServer(options =>
            {
                options.IssuerUri = IdentityServerIssuerUri;
            })
                          .AddInMemoryIdentityResources(Resources.GetIdentityResources())
                          .AddInMemoryApiResources(Resources.GetApiResources())
                          .AddInMemoryClients(Clients.GetClients())
                          .AddResourceOwnerValidator <ResourceOwnerPasswordValidator>()
                          .AddPersistedGrantStore <PersistedGrantStore>()
                          .AddProfileService <ProfileService>();

            builder.AddDeveloperSigningCredential();
            #endregion

            services.AddControllers(options =>
            {
                options.Filters.Add(typeof(HttpGlobalExceptionFilter));
            })
            .AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
            }).SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
        }
        public static IServiceCollection AddCustomDbContext <Context, Startup>(this IServiceCollection services, string dbType, string database, string server, string port, string userId, string password)
            where Context : DbContext
        {
            services.AddEntityFrameworkNpgsql()
            .AddDbContext <Context>(options =>
            {
                var connectionString = DbConnectionStringBuilder.Build(dbType, server, port, database, userId, password);

                if (dbType == AppDatabaseConst.Postgres)
                {
                    options.UseNpgsql(connectionString, sqlContextOptionsBuilder =>
                    {
                        sqlContextOptionsBuilder.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
                    });
                }
                else if (dbType == AppDatabaseConst.SQLServer)
                {
                    options.UseSqlServer(connectionString, sqlContextOptionsBuilder =>
                    {
                        sqlContextOptionsBuilder.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
                    });
                }
                else if (dbType == AppDatabaseConst.MySQL)
                {
                    options.UseMySQL(connectionString, sqlContextOptionsBuilder =>
                    {
                        sqlContextOptionsBuilder.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
                    });
                }
                else
                {
                }
                options.UseNpgsql(connectionString, sqlContextOptionsBuilder =>
                {
                    sqlContextOptionsBuilder.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
                });
            }, ServiceLifetime.Scoped);
            return(services);
        }
Exemplo n.º 3
0
 public DapperConnection(string databaseType, string server, string port, string database, string userId, string password)
 {
     _databaseType    = databaseType.ToLower();
     ConnectionString = DbConnectionStringBuilder.Build(databaseType, server, port, database, userId, password);
 }