Asp.Net Web Forms/MVC/Console App中使用Autofac
本来简单介绍了Autofac在Asp.Net Web Forms中的应用,后来又添加了mvc、控制台应用程序中使用Autofac,详情请看源码。
ASP.NET Web Forms使用Autofac,至少需要一下步骤:
1,引用Autofac程序集。
2,添加Autofac Web Modules 到 Web.config。
3,在Global.asax中实现IContainerProviderAccessor接口。
我们创建一个ASP.NET Web Forms项目,命名为WebFormStudy。
添加引用
添加引用的最简单方式就是用NuGet,右击WebFormStudy项目下的References,选择Manage NuGet Packages,如下图:

在Search Online中输入auto.web字样,Autofac WebForms Intergration 就搜索到了,点击Install。
安装完后,我们就可以在References中看到添加了Autofac.dll和Autofac.Integration.Web.dll,如下图:

添加Modules到Web.config
Autofac管理组件的生命周期并且添加依赖注入到Asp.net管道是通过IHttpModule实现的(注:在HttpApplication 初始化过程中,会根据配置文件加载并初始化相应的实现了IHttpModule接口的HttpModule 对象。对于HttpApplication来说,在它处理HTTP 请求的不同阶段会触发不同的事件,而HttpModule 的意义在于通过注册HttpApplication 的相应的事件,将所需的操作注入整个HTTP 请求的处理流程。ASP.NET 的很多功能,比如身份验证、授权、缓存等,都是通过相应的HttpModule 实现的。摘自:Asp.net Mvc4框架揭秘),你需要在web.config中配置这些Modules。
幸运的是,如果通过NuGet添加Autofac程序集,在安装的时候自动在Web.config中配置了相应的Modules,如下图:

Global.aszx中实现IContainerProviderAccessor接口
依赖注入模块需要HttpApplication实例实现IContainerProviderAccessor接口。一个完整的全局Application类如下所示:
public class Global : HttpApplication,IContainerProviderAccessor
{
static IContainerProvider _containerProvider;
public IContainerProvider ContainerProvider
{
get { return _containerProvider; }
}
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
#region 我们添加的代码
var builder = new ContainerBuilder();
//注册将被通过反射创建的组件
builder.RegisterType<DatabaseManager>();
builder.RegisterType<OracleDatabase>().As<IDatabase>();
_containerProvider = new ContainerProvider(builder.Build());
#endregion
}
}
DatabaseManager、OracleDatabase等类代码:
public interface IDatabase
{
string Name { get; } string Select(string commandText); string Insert(string commandText); string Update(string commandText); string Delete(string commandText);
}
IDatabase
public class DatabaseManager
{
IDatabase _database; public DatabaseManager(IDatabase database)
{
_database = database;
} public string Search(string commandText)
{
return _database.Select(commandText);
} public string Add(string commandText)
{
return _database.Insert(commandText);
} public string Save(string commandText)
{
return _database.Update(commandText);
} public string Remove(string commandText)
{
return _database.Delete(commandText);
} }
DatabaseManager
public class SqlDatabase : IDatabase
{
public string Name
{
get { return "sqlserver"; }
} public string Select(string commandText)
{
return string.Format("'{0}' is a query sql in {1}!", commandText, Name);
} public string Insert(string commandText)
{
return string.Format("'{0}' is a insert sql in {1}!", commandText, Name);
} public string Update(string commandText)
{
return string.Format("'{0}' is a update sql in {1}!", commandText, Name);
} public string Delete(string commandText)
{
return string.Format("'{0}' is a delete sql in {1}!", commandText, Name);
}
}
SqlDatabase
public class OracleDatabase : IDatabase
{
public string Name
{
get { return "oracle"; }
} public string Select(string commandText)
{
return string.Format("'{0}' is a query sql in {1}!", commandText, Name);
} public string Insert(string commandText)
{
return string.Format("'{0}' is a insert sql in {1}!", commandText, Name);
} public string Update(string commandText)
{
return string.Format("'{0}' is a update sql in {1}!", commandText, Name);
} public string Delete(string commandText)
{
return string.Format("'{0}' is a delete sql in {1}!", commandText, Name);
}
}
OracleDatabase
运行下,糟糕,报错了,如下图:

没关系,注释如下配置信息:

再次运行,ok,如下图:

参考引用: http://www.cnblogs.com/liping13599168/archive/2011/07/16/2108209.html
Asp.Net Web Forms/MVC/Console App中使用Autofac的更多相关文章
- 如何在 ASP.Net Web Forms 中使用依赖注入
依赖注入技术就是将一个对象注入到一个需要它的对象中,同时它也是控制反转的一种实现,显而易见,这样可以实现对象之间的解耦并且更方便测试和维护,依赖注入的原则早已经指出了,应用程序的高层模块不依赖于低层模 ...
- 【翻译】使用Knockout, Web API 和 ASP.Net Web Forms 进行简单数据绑定
原文地址:http://www.dotnetjalps.com/2013/05/Simple-data-binding-with-Knockout-Web-API-and-ASP-Net-Web-Fo ...
- Asp.Net学习进度备忘(第一步:ASP.NET Web Forms)
书签:“Web Pages”和“MVC”跳过:另外跳过的内容有待跟进 __________________ 学习资源:W3School. _________________ 跳过的内容: 1.ASP. ...
- ASP.NET Web Forms的改进
虽然ASP.NET Web Forms不是vNext计划的一部分,但它并没有被忽视.作为Visual Studio 2013 Update 2的一部分,它重新开始支持新工具.EF集成和Roslyn. ...
- ASP.NET Web Forms 4.5的新特性
作者:Parry出处:http://www.cnblogs.com/parry/ 一.强类型数据控件 在出现强类型数据控件前,我们绑定数据控件时,前台一般使用Eval或者DataBinder.Eval ...
- Knockout, Web API 和 ASP.Net Web Forms 进行简单数据绑定
使用Knockout, Web API 和 ASP.Net Web Forms 进行简单数据绑定 原文地址:http://www.dotnetjalps.com/2013/05/Simple-da ...
- ASP.NET Web Forms 的 DI 應用範例
跟 ASP.NET MVC 与 Web API 比起来,在 Web Forms 应用程式中使用 Dependency Injection 要来的麻烦些.这里用一个范例来说明如何注入相依物件至 Web ...
- [转]Bootstrap 3.0.0 with ASP.NET Web Forms – Step by Step – Without NuGet Package
本文转自:http://www.mytecbits.com/microsoft/dot-net/bootstrap-3-0-0-with-asp-net-web-forms In my earlier ...
- Using Friendly URLs in ASP.NET Web Forms
Introduction Websites often need to generate SEO friendly URLs. In ASP.NET Web Forms applications, a ...
随机推荐
- Graph Theory
Description Little Q loves playing with different kinds of graphs very much. One day he thought abou ...
- node.js安装部署
node js 安装部署学习 CentOS 下安装 Node.js 1.下载源码,你需要在https://nodejs.org/en/download/下载最新的Nodejs版本,链接: http ...
- k邻近算法理解及代码实现
github:代码实现 本文算法均使用python3实现 1 KNN KNN(k-nearest neighbor, k近邻法),故名思议,是根据最近的 $ k $ 个邻居来判断未知点属于哪个类别 ...
- NSDate常用的日期操作
// 当前时间创建NSDate NSDate *myDate = [NSDate date]; NSLog(@"myDate = %@",myDate); //从现在开始的24小时 ...
- OSG学习:矩阵变换节点示例
#include<osgViewer\Viewer> #include<osg\Node> #include<osg\Geode> #include<osg\ ...
- C# Dsoframer.ocx 如何在winform中嵌入Excel,内嵌Excel,word
如果你还不太清楚Dspframer.ocx怎么放到窗体上就看上一篇文章,里面详细介绍了是如何放到窗体上的. 链接:http://www.cnblogs.com/pingming/p/4182045.h ...
- 【Docker】- 基本命令
1.docker ps -a 显示所有容器 2.doker ps -l 显示最近一次启动的容器 3.docker ps 显示正在运行的容器 4.docker start [容器ID] 启动 ...
- (sender as TButton).some 和 TButton(sender).some 的区别是什么?
(sender as TButton).some 和 TButton(sender).some 的区别是什么? (Sender as TButton) 与 TButton(Sender) 都是 Typ ...
- 【hdu4507】吉哥系列故事——恨7不成妻 数位dp
题目描述 求 $[L,R]$ 内满足:数位中不包含7.数位之和不是7的倍数.本身不是7的倍数 的所有数的平方和 mod $10^9+7$ . 输入 输入数据的第一行是case数T(1 <= T ...
- 三节点搭建openstack-Mitaka版本
前言: 现在的云计算平台已经非常火,也非常的稳定了.像阿里云平台,百度云平台等等,今天咱们基于openstack来搭建一个云平台 注意: 本次平台搭建为三节点搭建(没有外部存储节点,所有存储为本地存储 ...