示例#1
0
        //public void ConfigureContainer(ContainerBuilder builder)
        //{
        //    // Add any Autofac modules or registrations.
        //    // This is called AFTER ConfigureServices so things you
        //    // register here OVERRIDE things registered in ConfigureServices.
        //    //
        //    // You must have the call to AddAutofac in the Program.Main
        //    // method or this won't be called.
        //    builder.RegisterModule(new AutofacModule());
        //}


        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseAuthentication();
            app.UseMiniProfiler();



            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "areas",
                    template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");

                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

            DocConfigurer.Configure(app, env);
        }
示例#2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            Server.ContentRootPath = this._env.ContentRootPath;
            services.Configure <CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded    = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            //jwt授权
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(Encoding.ASCII.GetBytes("sdfsdfsrty45634kkhllghtdgdfss345t678fs")),
                    ValidateIssuer           = true,
                    ValidIssuer      = "issuer",
                    ValidateAudience = true,
                    ValidAudience    = "audience",
                };
                options.Events = new JwtBearerEvents
                {
                    OnAuthenticationFailed = context =>
                    {
                        // 如果过期,则把<是否过期>添加到,返回头信息中
                        if (context.Exception.GetType() == typeof(SecurityTokenExpiredException))
                        {
                            context.Response.Headers.Add("Token-Expired", "true");
                        }
                        return(Task.CompletedTask);
                    }
                };
            });


            #region AutoMapper
            services.AddAutoMapper(typeof(Startup));
            #endregion

            #region 连接数据库
            //连接数据
            string aa   = Configuration["ConnectionStrings:SqlServerConnection"];
            string path = Configuration.GetConnectionString("SqlServerConnection");
            services.AddDbContext <MyDbContext>
                (options => options.UseSqlServer(path));
            #endregion

            #region SwaggerUI
            DocConfigurer.ConfigureServices(services, this.Configuration, this._env);
            #endregion

            #region Profiler 性能监控
            //监控sql,性能
            services.AddMiniProfiler(options =>
            {
                //  /profiler/results-index   /profiler/results     /profiler/results-list
                options.RouteBasePath       = "/profiler";
                options.PopupRenderPosition = RenderPosition.BottomLeft;
                //options.
            }).AddEntityFramework();
            #endregion

            #region 依赖注入
            //微软自带
            //services.AddScoped<IDbContextProvider<MyDbContext>, SimpleDbContextProvider<MyDbContext>>();
            //services.AddScoped<IDemoStudentAppService, DemoStudentAppService>();
            //services.AddScoped<IDemoStudentRepository, DemoStudentRepository>();
            //services.AddScoped<IMyDemoStudentAppService, MyDemoStudentAppService>();
            //services.AddScoped<IMyDemoStudentRepository, MyDemoStudentRepository>();

            //实例化 AutoFac  容器
            var builder           = new ContainerBuilder();
            var assemblysServices = Assembly.Load("My.D3.Application");
            builder.RegisterAssemblyTypes(assemblysServices).AsImplementedInterfaces();
            //注册上下文
            builder.RegisterType <SimpleDbContextProvider <MyDbContext> >().As <IDbContextProvider <MyDbContext> >().InstancePerLifetimeScope();

            //将services填充到Autofac容器生成器中
            builder.Populate(services);
            //使用已进行的组件登记创建新容器
            var ApplicationContainer = builder.Build();
            return(new AutofacServiceProvider(ApplicationContainer));//第三方IOC接管 core内置DI容器

            //IServiceProvider  返回这个
            #endregion
        }