Esempio 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.AddDbContext <StudyContext>(o =>
                                                 o.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddScoped(typeof(IRepository <>), typeof(Repository <>));
            services.AddRepository("Blog.Infra.Data", "Repository");
            services.AddServices("Blog.Application", "Service");
            services.AddScoped <IUnitOfWork, UnitOfWork>();
            services.AddMediatR(typeof(Startup));
            services.AddScoped <IMediatorHandler, InMemoryBus>();
            //services.AddTransient(typeof(IUserTokenAppService), typeof(UserTokenAppService));
            services.Configure <JwtSettings>(Configuration.GetSection(nameof(JwtSettings)));

            services.AddScoped <IRequestHandler <RegisterStudentCommand, Unit>, StudentCommandHandler>();

            services.AddScoped <INotificationHandler <DomainNotification>, DomainNotificationHandler>();

            //添加jwt验证:
            services.AddAuthentication(option =>
            {
                option.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                option.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(o =>
            {
                o.SecurityTokenValidators.Clear();
                o.SecurityTokenValidators.Add(new JwtValidator());
            });

            services.AddMvc()
            .AddJsonOptions(o => { o.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; })
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            services.AddAutoMapperSetup();
            services.AddSwagger();


            ///仓储引擎注入
            EngineerContext.Initialize(new GeneralEngineer(services.BuildServiceProvider()));
        }