大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全家桶1年46,售后保障稳定
AutoFac使用方法
设置读取配置文件的方法
AutoFacConfig.cs:
需要安装引用
Autofac3.5.2
Autofac.Configuration3.3.0 =>ConfigurationSettingsReader
Autofac.Owin4.0.0
Autofac.WebApi24.1.0
Autofac.WebApi2.Owin4.0.0
代码
public static class AutoFacConfig
{
public static void Register(HttpConfiguration config)
{
//AutoFac4.0以上版本才支持此方法
//var builder = new ContainerBuilder();
//builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
//builder.RegisterControllers(Assembly.GetExecutingAssembly());
//builder.RegisterControllers();
//var container = builder.Build();
//DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
var builder = new ContainerBuilder();
// 家在所有程序集
builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); // IHttpController in current assembly
//builder.RegisterModule<ApiControllerModule>(); // IHttpController in separate assemblies
builder.RegisterModule(new ConfigurationSettingsReader());
builder.RegisterWebApiFilterProvider(config);
// Set the dependency resolver to be Autofac
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
}
}
注册AutoFac
protected void Application_Start(object sender, EventArgs e)
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
AutoFacConfig.Register(GlobalConfiguration.Configuration);
//GlobalConfiguration.Configure(AutoFacConfig.Register);
}
配置文件
添加引用
配置autofac
<configSections>
<section name="autofac" type="Autofac.Configuration.SectionHandler, Autofac.Configuration"/>
</configSections>
<autofac>
<components>
<!—type=实现 service=接口 格式 MyType MyAssembly-->
<!--Business Registration of Controllers -->
<!--<component type="SAC.CMD.Http.Controller.FillingDataController, SAC.CMD.Http.Controller" />-->
<!--Business Registration of Services -->
<component type="SAC.CMD.Service.FillingDataService, SAC.CMD.Service" service="SAC.CMD.IService.IFillingDataService, SAC.CMD.IService"/>
<!--Business Registration of Repositories -->
<component type="SAC.CMD.Repository.FillingDataRepository, SAC.CMD.Repository" service="SAC.CMD.IRepository.IFillingDataRepository, SAC.CMD.IRepository" />
</components>
</autofac>
注:转载请注明出处
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/215558.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...