Autofac + MVC3 + Html.Action

时间:2012-02-28 10:23:41

标签: asp.net-mvc-3 controller autofac

我遇到与this question

相同的问题

错误消息是: 控制器“Search.Web.Controllers.AdvancedController”的单个实例不能用于处理多个请求。如果正在使用自定义控制器工厂,请确保为每个请求创建一个新的控制器实例。

Global.asax中的代码:

protected void Application_Start()
{
  var containerBuilder = new ContainerBuilder();

  containerBuilder.RegisterType<AdvancedController>().InstancePerHttpRequest();
  containerBuilder.RegisterType<MemoryBodyTypeRepository>().As<IBodyTypeRepository>;
  containerBuilder.RegisterType<BodyTypePictureClassFinder>().As<IBodyTypePictureClassFinder>();

  var container = containerBuilder.Build();

  DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
  AreaRegistration.RegisterAllAreas();

  RegisterGlobalFilters(GlobalFilters.Filters);
  RegisterRoutes(RouteTable.Routes);
}

这是来自AdvancedController的一些代码:

private readonly IBodyTypeRepository _bodyTypeRepository;
private readonly IBodyTypePictureClassFinder _bodyTypePictureClassFinder;

public AdvancedController(IBodyTypeRepository bodyTypeRepository, IBodyTypePictureClassFinder bodyTypePictureClassFinder)
{
    _bodyTypeRepository = bodyTypeRepository;
    _bodyTypePictureClassFinder = bodyTypePictureClassFinder;
}

[HttpGet]
public ActionResult Index()
{
    var advancedSearchViewModel = new AdvancedSearchViewModel();        

    return View(advancedSearchViewModel);
}

public ActionResult BodyTypes()
{
    // this uses the repositories to create the ViewModel

    return View(bodyTypesViewModel);
}

索引视图:

<div>
    @Html.Action("BodyTypes","Advanced")   
</div>

如果我执行此视图,我会收到上述消息。我还尝试删除InstancePerHttpRequest或使用RegisterControllers而不是显式重新设置它们,但这也不起作用。 如果我使用RegisterControllers,我会得到相同的错误。如果我删除InstancePerHttpRequest它会以某种方式执行整个View两次,这也不是我想做的;)

我希望任何人都可以提供帮助。对我来说这是一个真正的Showstopper。

Thansk很多!!!!

此致 Florian Fanderl

1 个答案:

答案 0 :(得分:0)

我知道这已经很久以前发过了。 我有同样的问题,我可以用你的问题解决我的问题。 希望这对某些使用者有用。

var builder = new ContainerBuilder();

            builder.RegisterType<ExtensibleActionInvoker>().As<IActionInvoker>().InstancePerHttpRequest();
            builder.RegisterControllers(Assembly.GetExecutingAssembly()).InjectActionInvoker().InstancePerHttpRequest();
            builder.RegisterType<ConfigService>().As<IConfigService>().InstancePerLifetimeScope();
            builder.RegisterType<EntryService>().As<IEntryService>().InstancePerLifetimeScope();
            builder.RegisterType<UserService>().As<IUserService>().InstancePerLifetimeScope();
            builder.RegisterType<MessageService>().As<IMessageService>().InstancePerLifetimeScope();
            builder.RegisterType<CloudService>().As<ICloudService>().InstancePerLifetimeScope();
            builder.RegisterType<Services>().As<IServices>().InstancePerLifetimeScope();

            builder.RegisterType<AccountController>().InstancePerDependency();


            _containerProvider = new ContainerProvider(builder.Build());

            ControllerBuilder.Current.SetControllerFactory(new AutofacControllerFactory(ContainerProvider));

            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(System.Web.Optimization.BundleTable.Bundles);
            AuthConfig.RegisterAuth();

            HtmlHelper.ClientValidationEnabled = true;
            HtmlHelper.UnobtrusiveJavaScriptEnabled = true;

            // Quartz.NET scheduler
            ISchedulerFactory factory = new StdSchedulerFactory();
            var scheduler = factory.GetScheduler();
            scheduler.JobFactory = new AutofacJobFactory(ContainerProvider);
            scheduler.Start();
        }

如果你注意到这是我的someController

builder.RegisterType<AccountController>().InstancePerDependency();