ASP.NET Core 下的依赖注入(一)
发布时间:2020-12-16 07:32:34 所属栏目:asp.Net 来源:网络整理
导读:本文介绍利用 Microsoft.Extensions.Configuration.Binder.dll 来实现超级简单的注入。 ? 1.?appsettings.json 中定义配置 假设我们有如下配置: { " Logging " : { " LogLevel " : { " Default " : " Information " , " Microsoft " : " Warning " , " Micr
本文介绍利用 Microsoft.Extensions.Configuration.Binder.dll 来实现超级简单的注入。 ? 1.?appsettings.json 中定义配置假设我们有如下配置: { "Logging": { "LogLevel": { "Default": "Information","Microsoft": "Warning","Microsoft.Hosting.Lifetime": "Information" } },"AllowedHosts": "*","Tecent": { "Qzone": { "Url": "http://i.qq.com","CName": "QQ空间","Age": 15 },"Weixin": { } } } ? 2. 定义配置模型类public interface ITecentConfig { QzoneConfig Qzone { get; set; } } ? public class TecentConfig : ITecentConfig { public QzoneConfig Qzone { get; set; } } ? public class QzoneConfig { public string Url { get; set; } public string CName { get; set; } public int Age { get; set; } } ? ? ? 3. Nuget 中引用?Microsoft.Extensions.Configuration.Binder.dll?? 4.? 编写 ServiceCollection 的扩展方法public static class ServiceCollectionExtensions { public static void ConfigureApplicationServices(this IServiceCollection services,IConfiguration configuration) { ITecentConfig tencentConfig = services.ConfigureStartupConfig<ITecentConfig,TecentConfig>(configuration.GetSection("Tecent")); //由于已经通过依赖注入了,并且单例模式,下面通过依赖注入读取配置。 } public static TConfig ConfigureStartupConfig<TConfig>(this IServiceCollection services,IConfiguration configuration) where TConfig : class,new() { if (services == null) throw new ArgumentNullException(nameof(services)); if (configuration == null) throw new ArgumentNullException(nameof(configuration)); var config = new TConfig(); configuration.Bind(config); services.AddSingleton(config); //.NET Core DI 为我们提供的实例生命周其包括三种: //Transient: 每一次GetService都会创建一个新的实例 //Scoped: 在同一个Scope内只初始化一个实例 ,可以 // 理解为( 每一个request级别只创建一个实例,同一个http request会在一个 scope内) //Singleton :整个应用程序生命周期以内只创建一个实例 return config; } public static IConfig ConfigureStartupConfig<IConfig,ConfigImpl>(this IServiceCollection services,IConfiguration configuration) where ConfigImpl : class,IConfig,new() { if (services == null) throw new ArgumentNullException(nameof(services)); if (configuration == null) throw new ArgumentNullException(nameof(configuration)); var config = new ConfigImpl(); configuration.Bind(config); services.AddSingleton(typeof(IConfig),config); return config; } } ? 这里需要解释一下,这里有 2 个重载方法??ConfigureStartupConfig, 第一个表示不用接口(或者抽象父类),直接读取配置成我们自己的配置模型,然后依赖注入单例。第二个方法表示只依赖接口或者抽象父类(指:我们自己定义的 ITencentConfig),不依赖具体的子类(指:我们自己定义的 TecentConfig)。? 5. 在?Startup.cs 中注册服务public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.Configure<CookiePolicyOptions>(options => { options.CheckConsentNeeded = context => true; }); services.ConfigureApplicationServices(Configuration);//注册我们自定义的服务 //注册自定义的模型绑定 services.AddMvc() .AddNewtonsoftJson(); services.AddRazorPages(); } } ? 6.? MVC 控制器中构造函数注入public class HomeController : Controller { private ITecentConfig _tecentConfig; public HomeController(ITecentConfig tecentConfig) { _tecentConfig = tecentConfig; } public IActionResult About() { return Content(string.Format("这个是 About 介绍。{0}: {1}。HashCode:{2}",_tecentConfig.Qzone.CName,_tecentConfig.Qzone.Url,_tecentConfig.GetHashCode() ) ); } } ? 7. 效果图? ? ? 谢谢浏览! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automap
- asp.net-mvc – 使用jquery Ajax加载PartialView?
- asp.net – 编码HTML页面标题的正确方法
- ASP.NET -- WebForm -- .aspx与.aspx.cs文件
- 框架升级后某个类型所在程序集发生转移,应用还能正常运行吗
- asp.net-core – 如何在VS2017 final中定位多个框架?
- ASP.NET——总结
- asp-classic – 从JScript代码调用VB编写的函数(经典ASP)
- asp.net – 如何在OnActionExecuting中获取当前路由信息
- asp.net – 无法将第二个自引用FK添加到模型,导致无法确定主
推荐文章
站长推荐
- asp.net-mvc – 在MVC 3中跨请求保持查询字符串
- 如何防止ASP.NET站点的图像热链接?
- asp.net-mvc – ASP.NET MVC,MVCContrib,Structu
- Windbg程序调试系列1-Mex扩展使用总结
- asp.net-mvc – ASP.NET MVC 3 – 区域无法正常工
- asp.net – 在.net 3.5 SP1中设置表单的操作会在
- 我在哪里可以获得asp.net身份的rtm控制器模板?
- 无限分级和tree结构数据增删改【提供Demo下载】
- asp.net – 使用JavaScript重新排列的ListBox元素
- asp.net-mvc – Asp.net MVC:上传多个图像文件?
热点阅读