首页 > Web开发 > 详细

.Net Core 使用依赖注入

时间:2016-10-21 19:08:24      阅读:630      评论:0      收藏:0      [点我收藏+]

ASP.NET Core 源码阅读笔记(1) ---Microsoft.Extensions.DependencyInjection

在asp .net中使用依赖注入很简单,只需要在Startup类的ConfigureServices(IServiceCollection services)方法中,通过IServiceCollection接口注入就行。

通过接口的Add*方法进行注册,代码如下:

ASP.NET Core runtime帮忙注册如下方法

       // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) { services.AddEntityFrameworkSqlServer().AddDbContext<**Context>(options => options.UseSqlServer(Configuration.GetConnectionString("***"))); //services.AddAuthorization(options => { options.DefaultPolicy }); // Add framework services. services.AddOptions(); services.AddDistributedMemoryCache(); services.AddMvc(); services.AddScoped<MenuService>(); services.AddTransient<ICommon, Common>(); services.AddSingleton<IConfiguration>(Configuration); }

 

    

.Net Core 使用依赖注入

原文:http://www.cnblogs.com/chrisghb8812/p/5985432.html

(0)
(1)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!