.net core 使用Autofac依赖注入
Startup中:
public IContainer ApplicationContainer { get; private set; }
// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.Configure<Data>(Configuration.GetSection("Data"));
services.AddDbContext<ShujuContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
// Add framework services.
services.AddMvc();
var builder = new ContainerBuilder();
builder.RegisterModule(new AutofacModule());
builder.Populate(services);
this.ApplicationContainer = builder.Build();
return new AutofacServiceProvider(this.ApplicationContainer);
}
AutoFacModule类
public class AutofacModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<DiTest>().As<IDiTest>();
}
}
使用:
private IDiTest diTest { get; }
public HomeController(IDiTest _diTest)
{
diTest = _diTest;
}
.net core 使用Autofac依赖注入的更多相关文章
- 拥抱.NET Core系列:依赖注入(2)
上一篇"拥抱.NET Core系列:依赖注入(1)"大体介绍了服务注册.获取和生命周期,这一篇来做一些补充. 由于内容跨度大(.NET Core.ASP.NET Core),所以文 ...
- 深入理解net core中的依赖注入、Singleton、Scoped、Transient(四)
相关文章: 深入理解net core中的依赖注入.Singleton.Scoped.Transient(一) 深入理解net core中的依赖注入.Singleton.Scoped.Transient ...
- ASP.NET Core 中的依赖注入
目录 什么是依赖注入 ASP .NET Core 中使用依赖注入 注册 使用 释放 替换为其它的 Ioc 容器 参考 什么是依赖注入 软件设计原则中有一个依赖倒置原则(DIP),为了更好的解耦,讲究要 ...
- 深入理解net core中的依赖注入、Singleton、Scoped、Transient(四)【转】
原文链接:https://www.cnblogs.com/gdsblog/p/8465401.html 相关文章: 深入理解net core中的依赖注入.Singleton.Scoped.Transi ...
- ASP.NETCore使用AutoFac依赖注入
原文:ASP.NETCore使用AutoFac依赖注入 实现代码 1.新建接口类:IRepository.cs,规范各个操作类的都有那些方法,方便管理. using System; using Sys ...
- Net core学习系列(三)——Net Core中的依赖注入
本文通过一个维修工与工具库的例子形象的描述一下为什么要用依赖注入.它的工作原理是什么样的, 然后根据这个类比一下ASP.NET Core 中的依赖注入, 从而深刻了解它的使用方法.注意事项以及回收机制 ...
- web API .net - .net core 对比学习-依赖注入
今天我们来看一下 .net web api 和 .net core web api依赖注入机制的差异. 首先我们分别在.net web api 和 .net core web api新建文件夹Serv ...
- Autofac 依赖注入小知识
Autofac 依赖注入小知识 控制反转/依赖注入 IOC/DI 依赖接口而不依赖于实现,是面向对象的六大设计原则(SOLID)之一.即依赖倒置原则(Dependence Inversion Prin ...
- ASP.NET Core中的依赖注入(1):控制反转(IoC)
ASP.NET Core在启动以及后续针对每个请求的处理过程中的各个环节都需要相应的组件提供相应的服务,为了方便对这些组件进行定制,ASP.NET通过定义接口的方式对它们进行了"标准化&qu ...
随机推荐
- Okhttp https
1. 绕过CA证书,不建议使用 private void ingoreCA() throws NoSuchAlgorithmException, KeyManagementException { SS ...
- linux下如何开启oracle服务和开启监听
su - oracle 切换到oracle用户模式下 sqlplus /nolog //登录sqlplus SQL> connect /as sysdba //连接oracle SQL&g ...
- 初识Android && 搭建Android开发环境
搭建Android开发环境需要的工具: JDK(Java Development Kit) Eclipse Android Sdk(Software Development Kit) ADT (And ...
- (转)EntityFrameword “Reverse Engineer Code First” 连接 MySql
转自:http://stackoverflow.com/questions/19676624/error-trying-to-reverse-engineer-code-first-mysql-dat ...
- 用PhpStorm IDE创建GG App Engine PHP应用教程
在上一篇教程里我们已经介绍了如何为PhpStorm搭建软件环境,那么今天就该是正式的开始创建App了: 3.创建首个Google App Engine PHP Application 现在我们就可以开 ...
- WPF之 DataGrid数据绑定
DataGrid控件是显示数据的控件,从一个对象集合获取信息并在具有行和单元格的网格中显示信息.每行和一个单独的对象相对应,并且每列和该对象中的一个属性相对应. DataGrid控件添加了许多在WPF ...
- Install nutch
1. Install nutch on single node: $apt-get install subversion $apt-get install ant $svn co https://sv ...
- 125 Valid Palindrome
public class Solution { public boolean isPalindrome(String s) { if(s==null) return false; s=s.toLowe ...
- 阿里云中Centos下配置防火墙
一.检查iptables服务状态 [root@woxplife ~]# service iptables status iptables: Firewall is not running. 说明ipt ...
- [转]DOS特殊字符转义方法
http://www.robvanderwoude.com/escapechars.php 期望得到的字符 转义后字符 说明 % %% May not always be required in do ...