This project is read-only.

Autofac servicemanager to resolve from static

Topics: Writing modules
Aug 19, 2011 at 10:16 AM

I'm trying to get a registered service in an HtmlHelper extension. How can I get to the autofac container that has all registered services?

I've tried 

var ctx = DependencyResolver.Current.GetService<IUserContext>();

and even 

var container = new ContainerBuilder().Build();
var ctx = container.Resolve<IUserContext>();

but both don't work.


Is there a way to access the services registered in the container from a static function?

Aug 19, 2011 at 6:46 PM

There is an extension method in the namespace Orchard.Mvc.Html, which lets you do Html.Resolve<TService>

You can find its definition in \src\Orchard\Mvc\Html\ContainerExtensions.cs

Aug 22, 2011 at 7:54 AM

Works like a charm, thanks!