替换Autofac.Integration.Web.Mvc(从autofac 2.3升级到2.6)

时间:2012-11-01 19:11:58

标签: autofac

autofac版本2.3.2.632中的Autofac.Integration.Web.Mvc dll将类AutofacControllerFactory定义为

 public class AutofacControllerFactory : DefaultControllerFactory
    {
        public AutofacControllerFactory(IContainerProvider containerProvider);

        protected override IController GetControllerInstance(RequestContext context, Type controllerType);
        public override void ReleaseController(IController controller);
    }

最新版本的autofac(2.6.3.862)没有Autofac.Integration.Web.Mvc,而是有 Autofac.Integration.Mvc。但是这个dll没有AutofacControllerFactory类。 dll Autofac.Integration.Mvc有一个名为AutofacDependencyResolver的类,定义为

public class AutofacDependencyResolver : IDependencyResolver
{
    public AutofacDependencyResolver(ILifetimeScope container);
    public AutofacDependencyResolver(ILifetimeScope container, Action<ContainerBuilder> configurationAction);
    public AutofacDependencyResolver(ILifetimeScope container, ILifetimeScopeProvider lifetimeScopeProvider);
    public AutofacDependencyResolver(ILifetimeScope container, ILifetimeScopeProvider lifetimeScopeProvider, Action<ContainerBuilder> configurationAction);

    public ILifetimeScope ApplicationContainer { get; }
    public static AutofacDependencyResolver Current { get; }
    public ILifetimeScope RequestLifetimeScope { get; }

    public object GetService(Type serviceType);
    public IEnumerable<object> GetServices(Type serviceType);
}

最新版Autofac中AutofacControllerFactory的替代品是什么?

2 个答案:

答案 0 :(得分:2)

AutofacControllerFactory没有替代品。相反,请使用DependencyResolver

The Autofac wiki has documentation on the new way to integrate with later versions of ASP.NET MVC.该Wiki页面包含指向示例应用程序的链接,您可以在其中查看实际工作代码。

答案 1 :(得分:1)

我相信这是通过版本改变MVC的API。在MVC 3/4中,相信您需要放入Application_Start的所有内容:

DependencyResolver.SetResolver(new AutofacDependencyResolver(container));