Beispiel #1
0
 public VeraDbRepository(IConfiguration config, VeraDbContext db, IMapper mapper, ILogger <VeraDbRepository> logger)
 {
     _config = config;
     _db     = db;
     _mapper = mapper;
     _logger = logger;
 }
Beispiel #2
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddOptions()
            .AddMvc()
            .AddJsonOptions(options => { options.SerializerSettings.Formatting = Formatting.Indented; })
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.AddCors();

            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            services.ConfigureAuthentication(Configuration);

            services.AddOptions()
            .Configure <AuthOptions>(Configuration.GetSection("Auth"))
            .Configure <SmscOptions>(Configuration.GetSection("Smsc"));

            services.AddAutoMapper(mc =>
            {
                mc.AddProfile(new MappingProfile());
            }, Assembly.GetExecutingAssembly());

            services.AddEntityFrameworkSqlServer()
            .AddDbContext <VeraDbContext>(options =>
            {
                options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
                options.UseSqlServer(Configuration.GetConnectionString("Vera"));
            });

            var optionsBuilder = new DbContextOptionsBuilder <VeraDbContext>();

            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("Vera"));
            using (var _db = new VeraDbContext(optionsBuilder.Options))
            {
                if (_db.Database.GetPendingMigrations().Any())
                {
                    _db.Database.Migrate();
                }
            }

            services.AddSingleton <ISmsSender, SmsDeliveryService>();
            services.AddSingleton <JwtFactory>();
            services.AddScoped <IVeraDbRepository, VeraDbRepository>();
            services.AddScoped <IClaimsAccessor, ClaimsAccessor>();

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info {
                    Title = "Vera REST API", Version = "v1"
                });
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });
        }