Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get container for Autofac for WebAPI2?

In Ninject I can get object needed for interface by using class WebContainerManager

Ninject definition:

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

Ninject usage:

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

My question is how to do the same in Autofac, to get needed class for interface?

UPDATE 1: Im using WebAPi 2, not MVC.

like image 783
P.K. Avatar asked Jul 14 '15 11:07

P.K.


People also ask

What is Autofac container?

Autofac is an IoC container for Microsoft . NET 4.5, Silverlight 5, Windows Store apps, and Windows Phone 8 apps. It manages the dependencies between classes so that applications stay easy to change as they grow in size and complexity. This is achieved by treating regular . NET classes as components.


2 Answers

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:

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();
}
like image 115
Andrii Tsok Avatar answered Oct 22 '22 01:10

Andrii Tsok


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.

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:

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:

  • install Autofac.Mvc5 nuget package
  • set DependencyResolver with the following code

    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:

var log = AutofacDependencyResolver.Current.Resolve<ILogManager>().GetLog(typeof(WebApiApplication));
like image 43
Vitaliy Tsvayer Avatar answered Oct 21 '22 23:10

Vitaliy Tsvayer