简体   繁体   English

如何获取Autofac for WebAPI2的容器?

[英]How to get container for Autofac for WebAPI2?

In Ninject I can get object needed for interface by using class WebContainerManager 在Ninject中,我可以通过使用类WebContainerManager获得接口所需的对象

Ninject definition: Ninject定义:

 var logManager = new LogManagerAdapter(); 
 container.Bind<ILogManager>().ToConstant(logManager); 

Ninject usage: Ninject用法:

var log = WebContainerManager.Get<ILogManager>().GetLog(typeof(WebApiApplication));

My question is how to do the same in Autofac, to get needed class for interface? 我的问题是如何在Autofac中执行相同操作,以获取所需的接口类?

UPDATE 1: Im using WebAPi 2, not MVC. 更新1:我正在使用WebAPi 2,而不是MVC。

You can create your builder. 您可以创建您的构建器。

var builder = new ContainerBuilder();

// Usually you're only interested in exposing the type
// via its interface:
builder.RegisterType<SomeType>().As<IService>();

// However, if you want BOTH services (not as common)
// you can say so:
builder.RegisterType<SomeType>().AsSelf().As<IService>();

Then you will be able to build your IoC: 然后,您将能够构建自己的IoC:

IContainer Container = builder.Build();

And a simple example of How to get resource from container: 还有一个如何从容器中获取资源的简单示例:

// Create the scope, resolve your IService,
// use it, then dispose of the scope.
using (var scope = Container.BeginLifetimeScope())
{
  var writer = scope.Resolve<IService>();
  writer.DoSomething();
}

If you need access to Autofac container from the class that was resolved by Autofac itself, then you can specify dependency on IComponentContext that is automatically provided by Autofac. 如果需要从由Autofac本身解析的类访问Autofac容器,则可以指定对由Autofac自动提供的IComponentContext的依赖。

Example: 例:

public void SomeComponent(IComponentContext context)
{
   this.context = context;
}
...
// somewhere inside SomeComponent
context.Resolve<ILogManager>();

If your code is running inside ASP.Net environment, then you most probably set its DependencyResolver, thus you can always access it like: 如果您的代码在ASP.Net环境中运行,则很可能会设置其DependencyResolver,因此您始终可以像以下方式访问它:

DependencyResolver.Current.GetService<ILogManager>();

but as it is already mentioned in other comments, Service Locator is an anti-pattern that should be avoided. 但是正如其他评论中已经提到的那样,服务定位器是一种应避免的反模式。

In order to integrate autofac container with standard MVC dependency resolution mechanism you need to: 为了将autofac容器与标准MVC依赖项解析机制集成在一起,您需要:

  • install Autofac.Mvc5 nuget package 安装Autofac.Mvc5 nuget软件包
  • set DependencyResolver with the following code 使用以下代码设置DependencyResolver

    var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

And in case you don't mind having explicit dependency on Autofac in your application code you can access global Autofac resolver reference the same way you use Ninject WebContainerManager: 并且如果您不介意在应用程序代码中显式依赖Autofac,则可以使用使用Ninject WebContainerManager的相同方式访问全局Autofac解析器引用:

var log = AutofacDependencyResolver.Current.Resolve<ILogManager>().GetLog(typeof(WebApiApplication));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM