1.初始化

using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using System;
using System.IO;
using Autofac;
using Autofac.Core;
using Autofac.Integration.Mvc; public static IContainer RegisterIOC()
{
ContainerBuilder builder = new ContainerBuilder();
//构造函数注入
builder.RegisterAssemblyTypes(Assembly.GetCallingAssembly()).AsImplementedInterfaces();
//注入控制器并实现属性注入
builder.RegisterControllers(Assembly.GetCallingAssembly()).PropertiesAutowired();
//支持过滤器的属性注入
builder.RegisterFilterProvider(); builder.RegisterType<AccountService>().As<IAccountService>(); var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); return container;
}

2.在global.asax.cs

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles); Func<ILifetimeScope, ILifetimeScope> scopeProvider = (ILifetimeScope container) =>
{
if (HttpContext.Current.Items[typeof(ILifetimeScope)] == null)
{
HttpContext.Current.Items[typeof(ILifetimeScope)] = container.BeginLifetimeScope("AutofacWebRequest");
} return (ILifetimeScope)HttpContext.Current.Items[typeof(ILifetimeScope)];
}; scopeProvider (RegisterIOC())
}

3.手工获取

 public class BeanFactory
{
/// <summary>
/// 获取Bean
/// </summary>
/// <typeparam name="T">获取的实例类型</typeparam>
/// <returns>获取的实例</returns>
public static T GetBean<T>()
{
try
{
if (System.Web.HttpContext.Current != null)
{
ILifetimeScope scope = System.Web.HttpContext.Current.Items[typeof(ILifetimeScope)] as ILifetimeScope;
if (scope != null)
{
return scope.Resolve<T>();
}
}
}
catch
{
} return ServiceLocator.Current.GetInstance<T>();
} /// <summary>
/// 获取Bean
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static object GetBean(System.Type type)
{
try
{
if (System.Web.HttpContext.Current != null)
{
ILifetimeScope scope = System.Web.HttpContext.Current.Items[typeof(ILifetimeScope)] as ILifetimeScope;
if (scope != null)
{
return scope.Resolve(type);
}
}
}
catch
{
} return ServiceLocator.Current.GetInstance (type) ;
}
}

4.参考 http://www.360doc.com/content/14/0620/09/10504424_388250715.shtml

最新文章

  1. 跳转到下一个activity
  2. EF架构~DefaultValue让我的UnitOfWork更可读
  3. RDLC系列之一 简单示例
  4. java实现身份证校验
  5. arguments.callee 调用自身
  6. android datepicker源码
  7. 十一、oracle 数据库管理员
  8. Java复习随笔
  9. sudo,visudo
  10. voip技术研究
  11. c语言基础学习01
  12. Spring配置集群定时任务
  13. lucene查询索引库、分页、过滤、排序、高亮
  14. nodejs笔记之流(stream)
  15. stringstream
  16. 腾讯的h5制作工具教程
  17. windows修改PowerShell(命令提示符)默认中文编码方式
  18. 局域网内远程连接OPC配置方法详解
  19. C++标准 bind函数用法与C#简单实现
  20. Java实现两个变量的互换(不借助第3个变量)

热门文章

  1. oralce错误总结
  2. 19. AUTO INCREMENT 字段
  3. R dataframe 遗忘, which 矩阵搜索
  4. Quartus II 14.0正式版 下载链接和破解器
  5. 如何在DOS下以管理员身份执行命令?
  6. 实现求解线性方程(矩阵、高斯消去法)------c++程序设计原理与实践(进阶篇)
  7. 初学python - 零碎的知识点
  8. Sqoop 遇到的问题
  9. map/fileter
  10. 【bzoj3512】DZY Loves Math IV 杜教筛+记忆化搜索+欧拉函数