ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

asp.net core 扩展方法

2021-10-01 16:33:00  阅读:209  来源: 互联网

标签:core asp AddSingleton registerService IServiceCollection services net 方法 public


   下面我们给 IServiceCollection 写扩展方法

首先新建一个静态类 名称未 services,代码如下:

 1 public static class service
 2         {
 3         public  static IServiceCollection registerService(this IServiceCollection services)
 4         {
 5 
 6             services.AddSession();
 7            
 8             services.AddControllersWithViews();
 9             services.AddSingleton<hy_contentService>();
10             services.AddSingleton<UserService>();
11             services.AddSingleton<hy_roomService>();
12             services.AddSingleton<hy_classService>();
13             services.AddSingleton<hy_groupService>();
14             //sqlserver_personService
15             services.AddSingleton<sqlserver_personService>();
16             services.AddSingleton<Town_countryService>();
17             services.AddSingleton<sqlserver_departmentService>();
18             //TypeService
19             services.AddHttpContextAccessor();
20             services.AddRazorPages().AddRazorRuntimeCompilation();
21 
22 
23 
24 
25 
26             return services;
27         }
28     }
View Code

 在扩展类中新建一个   registerService方法 用于 扩展  ,方法签名如下:
            public  static IServiceCollection registerService(this IServiceCollection services)       

完整代码如下:

 1  public  static IServiceCollection registerService(this IServiceCollection services)
 2         {
 3         //将服务注册都可以写到该扩展方法中
 4             services.AddSession();
 5            
 6             services.AddControllersWithViews();
 7             services.AddSingleton<hy_contentService>();
 8             services.AddSingleton<UserService>();
 9             services.AddSingleton<hy_roomService>();
10             services.AddSingleton<hy_classService>();
11             services.AddSingleton<hy_groupService>();
12             //sqlserver_personService
13             services.AddSingleton<sqlserver_personService>();
14             services.AddSingleton<Town_countryService>();
15             services.AddSingleton<sqlserver_departmentService>();
16             //TypeService
17             services.AddHttpContextAccessor();
18             services.AddRazorPages().AddRazorRuntimeCompilation();
19 
20 
21 
22 
23 
24             return services;
25         }

下面 在 startUp ConfigureServices方法中使用  扩展方法

 1  public void ConfigureServices(IServiceCollection services)
 2         {
 3             DB.DB_ConnectionString = Configuration.GetConnectionString("mySqlStr");
 4             DB.DB_SqlserverConnectionString = Configuration.GetConnectionString("SqlServerStr"); //SqlServerStr
 5 
 6             services.registerService();  

//该处调用 扩展方法 registerService ,把注册各种服务都可以写入该方法内 ,增强程序可读性 ,同时能保持 ConfigureServices 方法可读性。层次、逻辑更清晰 7 8 }

扩展方法 定义及应用就写到这里!

 

标签:core,asp,AddSingleton,registerService,IServiceCollection,services,net,方法,public
来源: https://www.cnblogs.com/sunzzhankui/p/15359589.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有