asp.net core 配置依赖注入

在startup.cs中的ConfigureServices方法有几种方式来配置依赖注入.

 1 public IServiceProvider ConfigureServices(IServiceCollection services)
 2         {
 3            // 1.字符串key + 字符串值
 4             services.Configure<DbOption>("CzarCms", Configuration.GetSection("DbOpion"));
 5 
 6             //2. 直接配置默认实现
 7             services.Configure<CodeGenerateOption>(options =>
 8             {
 9                 
10                 options.Author = "yilezhu";//作者名称
11                 options.OutputPath = "d:\\CzarCmsCodeGenerator";//模板代码生成的路径
12                 options.ModelsNamespace = "Czar.Cms.Models";//实体命名空间
13                 options.IRepositoryNamespace = "Czar.Cms.IRepository";//仓储接口命名空间
14                 options.RepositoryNamespace = "Czar.Cms.Repository.SqlServer";//仓储命名空间
15                 options.IServicesNamespace = "Czar.Cms.IServices";//服务接口命名空间
16                 options.ServicesNamespace = "Czar.Cms.Services";//服务命名空间
17 
18 
19             });
20 
21             //3. 直接配置接口和对应的实现,还显示指定了对象的生命周期
22             services.AddScoped<IArticleRepository, ArticleRepository>();//在同一次请求中,获取多次对象得到的是同一个对象
23             //services.AddTransient<IArticleRepository, ArticleRepository>(); //在每次请求中,获取到的都是一个新对象
24             //services.AddSingleton<IArticleRepository, ArticleRepository>(); //在整个应用程序中,获取到的都是一个新对象
25 
26             //4.配置asp.net core 内置的接口(服务)
27 
28             //4.1 配置基于cookie的授权认证
29             services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
30            .AddCookie(options =>
31            {
32                options.LoginPath = "/Account/Index";
33                options.LogoutPath = "/Account/Logout";
34                options.ExpireTimeSpan = TimeSpan.FromMinutes(15);
35            });
36 
37             //4.2 配置基于cookie的session
38             services.AddSession(options =>
39             {
40                 options.IdleTimeout = TimeSpan.FromMinutes(15);
41                 options.Cookie.HttpOnly = true;
42             });
43 
44             //4.3 配置跨域伪造请求(CSRF)
45             services.AddAntiforgery(options =>
46             {
47                 // Set Cookie properties using CookieBuilder properties†.
48                 options.FormFieldName = "AntiforgeryKey_yilezhu";
49                 options.HeaderName = "X-CSRF-TOKEN-yilezhu";
50                 options.SuppressXFrameOptionsHeader = false;
51             });
52             //4.4 配置mvc
53             services.AddMvc(option =>
54             {
55                 option.Filters.Add(new GlobalExceptionFilter());
56             })
57                 .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
58                 .AddControllersAsServices()
59                 .AddFluentValidation(fv =>
60                 {
61                     //程序集方式引入
62                     fv.RegisterValidatorsFromAssemblyContaining<ManagerRoleValidation>();
63                     //去掉其他的验证,只使用FluentValidation的验证规则
64                     fv.RunDefaultMvcValidationAfterFluentValidationExecutes = false;
65                 });
66             //DI了AutoMapper中需要用到的服务,其中包括AutoMapper的配置类 Profile
67             services.AddAutoMapper();
68             var builder = new ContainerBuilder();
69             builder.Populate(services);
70 
71             //4.5 通过程序集,批量注入对象,
72             //不过这种方式不知道是通过名称约束,还是通过每个类显示实现的接口,来建立接口(服务)和对象的对象关系的
73             builder.RegisterAssemblyTypes(typeof(ManagerRoleRepository).Assembly)
74                    .Where(t => t.Name.EndsWith("Repository"))
75                    .AsImplementedInterfaces();
76             builder.RegisterAssemblyTypes(typeof(ManagerRoleService).Assembly)
77                  .Where(t => t.Name.EndsWith("Service"))
78                  .AsImplementedInterfaces();
79             return new AutofacServiceProvider(builder.Build());
80         }

文章没写完,现在仅仅是总结了一下,如何把具体的依赖(对象)给注入到asp.net core的容器中,后面会找时间写一下,如何取出这些对象,因为注入只是在做准备工作,拿出对象才是真正的开始工作...

 

转载于:https://www.cnblogs.com/LeeSolo/p/10562755.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值