-
C#教程之Autofac依赖注入
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
using Autofac; using Autofac.Extras.Quartz; using Autofac.Integration.WebApi; using Quartz; using STAr.Enterprise.CRM.JobScheduler.Job; using System.Collections.Specialized; using System.Configuration; using System.Reflection; using System.Web.Http; namespace WebAPI { public static class AutoFacConfig { private readonly static string _DICommonConfString = ConfigurationManager.AppSettings["DICommon"].Trim(); private readonly static string _DIRepositoryConfString = ConfigurationManager.AppSettings["DIRepository"].Trim(); private readonly static string _DIServicesConfString = ConfigurationManager.AppSettings["DIServices"].Trim(); private readonly static string _DIIntegrationConfString = ConfigurationManager.AppSettings["DIIntegration"].Trim(); private readonly static string _DIAuthorizationConfString = ConfigurationManager.AppSettings["DIAuthorization"].Trim(); public static IScheduler InitAutoFacInit() { var builder = new ContainerBuilder(); // configure and register Quartz var schedulerConfig = new NameValueCollection { { "quartz.threadPool.threadCount", "10" }, { "quartz.scheduler.threadName", "Scheduler" } }; builder.RegisterModule(new QuartzAutofacFactoryModule { ConfigurationProvider = c => schedulerConfig }); builder.RegisterModule(new QuartzAutofacJobsModule(typeof(TestJob).Assembly)); //注册所有的ApiControllers builder.RegisterApiControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired().InstancePerLifetimeScope(); //注册所有的接口实现 Assembly DICommon = Assembly.Load(_DICommonConfString); Assembly DIRepository = Assembly.Load(_DIRepositoryConfString); Assembly DIServices = Assembly.Load(_DIServicesConfString); Assembly DIIntegration = Assembly.Load(_DIIntegrationConfString); Assembly DIAuthorization = Assembly.Load(_DIAuthorizationConfString); builder.RegisterAssemblyTypes(DICommon, DIRepository, DIServices, DIIntegration, DIAuthorization).AsImplementedInterfaces(); builder.RegisterTypes(DIRepository.GetExportedTypes()).PropertiesAutowired().InstancePerLifetimeScope(); var container = builder.Build(); //注册api容器需要使用HttpConfiguration对象 HttpConfiguration config = GlobalConfiguration.Configuration; config.DependencyResolver = new AutofacWebApiDependencyResolver(container); // 依赖注入Scheduler 并 返回 return container.Resolve<IScheduler>(); } } }
栏目列表
最新更新
C# 面向对象
假设客车的座位数是9行4列,使用二维数
C#基于接口设计三层架构Unity篇
C#线程 入门
C#读取静态类常量属性和值
C# 插件式编程
C# 委托与事件有啥区别?
C#队列学习笔记:队列(Queue)和堆栈(Stack
linq 多表分组左连接查询查询统计
C#队列学习笔记:MSMQ入门一
C# 在Word中添加Latex 数学公式和符号
inncheck命令 – 检查语法
基于UDP的服务器端和客户端
再谈UDP和TCP
在socket编程中使用域名
网络数据传输时的大小端问题
socket编程实现文件传输功能
如何优雅地断开TCP连接?
图解TCP四次握手断开连接
详细分析TCP数据的传输过程
SqlServer 利用游标批量更新数据
BOS只读状态修改
SQL Server等待事件—PAGEIOLATCH_EX
数据库多行转换为单一列
获取数据表最后最后访问,修改,更新,
计算经历的时间
SQL查询结果自定义排序
修改数据库默认位置
日期简单加或减
从日期获取年,月或日