/// <summary> /// /// </summary> /// <param name="services"></param> public IServiceProvider ConfigureServices(IServiceCollection services) { //配置跨域处理 services.AddCors(options => { options.AddPolicy("AllowAnyOrigin", builder => { builder.AllowAnyOrigin() //允许任何来源的主机访问 .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials();//指定处理cookie }); }); services.AddSession(options => { options.Cookie.HttpOnly = true; options.IdleTimeout = TimeSpan.FromSeconds(7200); }); services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new Info { Description = "Tingli-作者", Version = "Version 1.0", Title = "微信API文档" }); var basePath = AppContext.BaseDirectory; var commentsFileName = Assembly.GetExecutingAssembly().GetName().Name + ".XML"; var xmlPath = Path.Combine(basePath, commentsFileName); options.IncludeXmlComments(xmlPath); options.OperationFilter <AssignOperationVendorExtensions>(); }); services.AddDistributedMemoryCache(); services.AddMvc(options => options.Filters.Add <GlobalExceptionAttribute>()) .AddJsonOptions(options => { options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss.fff"; options.SerializerSettings.ContractResolver = new DefaultContractResolver(); }) .SetCompatibilityVersion(CompatibilityVersion.Version_2_1); return(AutofacService.RegisterAutofac(services, "Stack.WeChat.Contracts", "Stack.WeChat.Service")); }
// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { 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); services.AddDbContext <NsDb>(options => options.UseSqlServer(Configuration.GetConnectionString("SQLServer"))); services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, x => { x.LoginPath = new PathString("/login"); x.AccessDeniedPath = new PathString("/error"); }); AutofacService autofac = new AutofacService(); return(autofac.RegisterAutofac(services)); }
/// <summary> /// IServiceProvider /// </summary> /// <param name="services"></param> /// <returns></returns> public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc().AddJsonOptions(options => { options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; options.SerializerSettings.ContractResolver = new DefaultContractResolver(); options.SerializerSettings.DateFormatString = "yyyy-MM-dd"; }); #region Swagger services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new Info { Version = "Version 1.0", Title = "TrunkCoreDemo的API文档", Description = "Dylan作者" }); var commentsFileName = Assembly.GetExecutingAssembly().GetName().Name + ".XML"; var basePath = AppContext.BaseDirectory; var xmlPath = Path.Combine(basePath, commentsFileName); options.IncludeXmlComments(xmlPath); options.OperationFilter <AssignOperationVendorExtensions>(); }); #endregion //向容器内注入数据库 //var connectionStrings = Configuration.GetConnectionString("MYSQLWALIUJR_SYS"); //services.AddDbContext<WALIUJR_SYSContext>(options => options.UseSqlServer(connectionStrings)); #region Autofac DI注入 第一种 //说明:ConfigureServices返回void,通过构造函数来获取 //services.AddTransient<IDemoService, DemoService>(); #endregion #region Autofac DI注入 第二种 //说明:ConfigureServices返回IServiceProvider,通过接口属性来获取 //services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>()); //var builder = new ContainerBuilder(); //builder.RegisterType<DemoService>().As<IDemoService>().InstancePerLifetimeScope().PropertiesAutowired(); //builder.RegisterType(typeof(ValuesController)).InstancePerLifetimeScope().PropertiesAutowired(); //builder.Populate(services); //return new AutofacServiceProvider(builder.Build()); #endregion #region Autofac DI注入 第三种 //说明:ConfigureServices返回IServiceProvider,通过接口属性来获取,自动获取Controller的属性 //services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>()); //var assembly = this.GetType().GetTypeInfo().Assembly; //var manager = new ApplicationPartManager(); //manager.ApplicationParts.Add(new AssemblyPart(assembly)); //manager.FeatureProviders.Add(new ControllerFeatureProvider()); //var feature = new ControllerFeature(); //manager.PopulateFeature(feature); //var builder = new ContainerBuilder(); //builder.RegisterType<ApplicationPartManager>().AsSelf().SingleInstance(); //builder.RegisterTypes(feature.Controllers.Select(ti => ti.AsType()).ToArray()).PropertiesAutowired(); //builder.Populate(services); //Assembly iface = Assembly.Load("TrunkCoreDemo.Service"); //Assembly service = Assembly.Load("TrunkCoreDemo.Contracts"); //builder.RegisterAssemblyTypes(iface, service) // .Where(t => t.Name.EndsWith("Service")) // .AsImplementedInterfaces() // .InstancePerLifetimeScope(); //return new AutofacServiceProvider(builder.Build()); #endregion #region Autofac DI注入 第四种 //说明:ConfigureServices返回IServiceProvider,通过接口属性来获取, //Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(q => q.FullName.Contains("Trunk")).ToArray(); //var types = Assembly.GetEntryAssembly().GetReferencedAssemblies(); //String baseDir = AppContext.BaseDirectory; //String basePath2 = Path.GetDirectoryName(typeof(Program).Assembly.Location); //DirectoryInfo dirInfo = new DirectoryInfo(baseDir); //var fileInfo = dirInfo.GetFiles().Where(q => q.FullName.Contains(".Service")).FirstOrDefault(); //services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>()); //var assembly = this.GetType().GetTypeInfo().Assembly; //var manager = new ApplicationPartManager(); //manager.ApplicationParts.Add(new AssemblyPart(assembly)); //manager.FeatureProviders.Add(new ControllerFeatureProvider()); //var feature = new ControllerFeature(); //manager.PopulateFeature(feature); //var builder = new ContainerBuilder(); //builder.RegisterType<ApplicationPartManager>().AsSelf().SingleInstance(); //builder.RegisterTypes(feature.Controllers.Select(ti => ti.AsType()).ToArray()).PropertiesAutowired(); //builder.Populate(services); //var assemblys = Assembly.LoadFrom(fileInfo.FullName); //var baseType = typeof(IDependency); //builder.RegisterAssemblyTypes(assemblys) // .Where(m => baseType.IsAssignableFrom(m) && m != baseType) // .AsImplementedInterfaces().InstancePerLifetimeScope(); //return new AutofacServiceProvider(builder.Build()); #endregion return(AutofacService.RegisterAutofac(services, "DMS.EntityFrameworkCore.Contracts", "DMS.EntityFrameworkCore.Service")); }