方法1
1.1、项目情况
框架:.NET Framework 4.5
Autofac 3.5.0
Autofac.WebApi2 4.3.0

 

1.2、定义接口与对应实现
// 接口1
public interface IBaseUserService
{
List<BaseUser> GetBaseUserList();
}
// 接口2
public interface IBaseCloseLoopService
{
List<BaseCloseLoop> GetBaseCloseLoopList();
}

// 实现1
public class BaseUserService : IBaseUserService
{
public List<BaseUser> GetBaseUserList()
{
BaseUserDao dao = new BaseUserDao();
return dao.GetModelList();
}

}
// 实现2
public class BaseCloseLoopService : IBaseCloseLoopService
{
public List<BaseCloseLoop> GetBaseCloseLoopList()
{
BaseCloseLoopDao dao = new BaseCloseLoopDao();
return dao.GetModelList();
}
}
1.3、添加Autofac配置类
public class AutofacConfig
{
public static Autofac.IContainer _container;

public static void Configure()
{
var builder = new ContainerBuilder();
var config = System.Web.Http.GlobalConfiguration.Configuration;

// OPTIONAL: Register the Autofac filter provider.
//builder.RegisterWebApiFilterProvider(config);
// OPTIONAL: Register the Autofac model binder provider.
//builder.RegisterWebApiModelBinderProvider();

// 指定接口的实现类
builder.RegisterType<BaseUserService>().As<IBaseUserService>().AsImplementedInterfaces();
builder.RegisterType<BaseCloseLoopService>().As<IBaseCloseLoopService>().AsImplementedInterfaces();
// 一次性注册所有【实现了baseTyp接口的类】;不建议,无法指定接口实现类
//Assembly[] assemblies = Directory.GetFiles(AppDomain.CurrentDomain.RelativeSearchPath, "*.dll").Select(Assembly.LoadFrom).ToArray();
//List<Type> baseTypeList = new List<Type>()
//{
// typeof(IBaseUserService),
// typeof(IBaseCloseLoopService)
//};
//builder.RegisterAssemblyTypes(assemblies).Where(type => baseTypeList.Any(t => t.IsAssignableFrom(type)) && !type.IsAbstract).AsSelf().AsImplementedInterfaces().PropertiesAutowired().InstancePerLifetimeScope();

// 注册 Web API Controllers
builder.RegisterApiControllers(System.Reflection.Assembly.GetExecutingAssembly());
_container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(_container);
}
}



// ================================ 分割线 ==========================================

// 以下为ASP.NET MVC的Autofac配置,注意引用的DLL有所不同,此处不详述
public class AutofacConfig
{
public static Autofac.IContainer _container;
public static void Register()
{
var builder = new ContainerBuilder();
builder.RegisterType<BaseCloseLoopService>().As<IBaseCloseLoopService>();
builder.RegisterControllers(System.Reflection.Assembly.GetExecutingAssembly());
_container = builder.Build();
System.Web.Mvc.DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(_container));
}
}
1.4、在Global.asax引用配置
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

//autofac ioc配置
AutofacConfig.Configure();
}
}
1.5、ApiController使用
public class CloseLoopController : ApiController
{
// 需要注入的接口
private readonly IBaseUserService _baseUserService;
private readonly IBaseCloseLoopService _baseCloseLoopService;
/// <summary>
/// 构造函数注入
/// </summary>
/// <param name="baseUserService"></param>
/// <param name="baseCloseLoopService"></param>
public CloseLoopController(IBaseUserService baseUserService, IBaseCloseLoopService baseCloseLoopService)
{
_baseUserService = baseUserService;
_baseCloseLoopService = baseCloseLoopService;
}


[HttpGet]
public string GetBaseUser([FromBody] object json)
{
// 直接调用方法即可
var result = _baseUserService.GetBaseUserList();
return JsonConvert.SerializeObject(result);
}

[HttpGet]
public string GetBaseCloseLoop([FromBody] object json)
{
var result = _baseCloseLoopService.GetBaseCloseLoopList();
return JsonConvert.SerializeObject(result);
}

}
方法2
2.1、安装包
前面方法1的前提下,再Nuget安装 Autofac.Configuration

2.2、web.config配置autofac
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="autofac" type="Autofac.Configuration.SectionHandler, Autofac.Configuration" />
</configSections>
<autofac>
<components>
<component type="Autofac.Test.Service.BaseUserService, Autofac.Test.Service" service="Autofac.Test.Contracts.IBaseUserService, Autofac.Test.Contracts" />
<component type="Autofac.Test.Service.BaseCloseLoopService, Autofac.Test.Service" service="Autofac.Test.Contracts.IBaseCloseLoopService, Autofac.Test.Contracts" />
</components>
</autofac>
</configuration>
2.3、新建autofac帮助类,读取配置生成实例
using Autofac;
using Autofac.Configuration;

public class IOCHelper
{
public static TInterface GetObject<TInterface>(string sectionName)
{
Autofac.ContainerBuilder builder = new ContainerBuilder();
builder.RegisterModule(new Autofac.Configuration.ConfigurationSettingsReader(sectionName));
IContainer container = builder.Build();
TInterface dal = container.Resolve<TInterface>();
return dal;
}
}
2.4、调用
private IBaseUserService userService = IOCHelper.GetObject<IBaseUserService>("autofac");

posted @ 2024-08-29 10:42 潇沫明月 阅读(45) 评论(0) 推荐(0) 编辑
摘要: 文章目录一、语法说明exists:not exists:二、常用示例说明1.查询a表在b表中存在数据2.查询a表在b表中不存在数据3.查询时间最新记录4.exists替代distinct剔除重复数据总结一、语法说明exists:括号内子查询sql语句返回结果不为空(即:sql返回的结果为真),子查询 阅读全文
posted @ 2023-08-22 20:21 潇沫明月 阅读(2516) 评论(0) 推荐(0) 编辑
摘要: 作者:HooH链接:https://www.zhihu.com/question/25487297/answer/360089469来源:知乎著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 您的直觉是正确的。 之所以您觉得白酒难喝,多半因为您喝酒的姿势不对,或是酒不对。 能喝不 阅读全文
posted @ 2023-05-26 09:36 潇沫明月 阅读(49) 评论(0) 推荐(0) 编辑
摘要: Java全栈学习路线图 第一阶段:Java基础篇 01 J2SE v2(41课时) 课程链接 02 Mysql数据库(20课时) 课程链接 03 JDBC(9课时) 课程链接 04 Swing图书管理系统V2.0(8课时) 课程链接 第一阶段:学习目标及知识要点 01 熟练掌握Java基础语法,Ja 阅读全文
posted @ 2023-05-12 16:38 潇沫明月 阅读(103) 评论(0) 推荐(0) 编辑
摘要: C#/.NET/.NET Core优秀项目框架推荐 [虚幻私塾】于 2022-09-06 12:39:12 发布12478 收藏 32 分类专栏: 11 文章标签: c# .net .netcore 计算机 版权 11专栏收录该内容 53 篇文章1 订阅 订阅专栏 🚀 优质资源分享 🚀 学习路线 阅读全文
posted @ 2023-04-26 17:30 潇沫明月 阅读(225) 评论(0) 推荐(0) 编辑
摘要: 盘点10个.NetCore实用的开源框架项目 zsw119于 2022-12-12 22:28:03 发布1602 收藏 6 文章标签: .netcore 开源 版权 开源项目管理文章已被社区收录 加入社区 连续分享.Net开源项目快3个月了,今天我们一起梳理下10个,比较受到大家欢迎的.NetCo 阅读全文
posted @ 2023-04-26 17:27 潇沫明月 阅读(276) 评论(0) 推荐(0) 编辑
摘要: 推荐20个.NET/.NET Core 优秀项目框架 精通电脑科技于 2022-10-09 16:58:48 发布2050 收藏 8 文章标签: .net .netcore 前端 版权 前言 分享一些.NET开源项目,通过了解和对比更多的项目来选择最适合我们自己学习、工作开发的一套项目。 优秀的项目 阅读全文
posted @ 2023-04-26 17:18 潇沫明月 阅读(371) 评论(0) 推荐(0) 编辑
摘要: 学习计划 .netcore 工作原理(基本用法已掌握,但是要更深入一点) .netcore的IOC,DI,AOP,DDD ABP框架 linux(常用命令,ftp) 部署.netcore应用到CentOS Nginx反向代理及分布式Session 在容器中部署.netCore应用 通过Nginx反向 阅读全文
posted @ 2023-04-23 13:14 潇沫明月 阅读(109) 评论(0) 推荐(0) 编辑
摘要: layui常用弹窗和qrcode二维码生成 hidecode于 2020-10-21 11:34:26 发布4473 收藏 3 分类专栏: JAVAEE 文章标签: layui vue.js javascript css html 版权 JAVAEE专栏收录该内容 51 篇文章0 订阅 订阅专栏 框 阅读全文
posted @ 2023-04-18 11:05 潇沫明月 阅读(446) 评论(0) 推荐(0) 编辑
摘要: 关于Studio Style不能下载的解决方法 Teriri23IP属地: 德克萨斯州 2020.03.02 23:59:58字数 444阅读 921 最近在做毕设,需要用到 Unity3D 和 VS 。之前一直在用 eclipse 。突然打开VS发现白色的背景好刺眼,于是想要改成黑色。才发现原来可 阅读全文
posted @ 2023-04-10 16:03 潇沫明月 阅读(61) 评论(0) 推荐(0) 编辑
点击右上角即可分享
微信分享提示