1

Autofac.ContainerBuilder在我的 ASP.Net MVC 4 应用程序中使用,这给默认控制器生成多个实例带来了问题。

我试着这样处理

 builder.RegisterType<AccountController>().As<IController>().PreserveExistingDefaults();

但没有用。

代码

protected void Application_Start()
    {

      var dataPath = "~/App_Data/" + ConfigurationManager.AppSettings["Blog_Site"];

        var builder = new ContainerBuilder();

        builder.RegisterType<ExtensibleActionInvoker>().As<IActionInvoker>().InstancePerHttpRequest();
        builder.RegisterControllers(Assembly.GetExecutingAssembly()).InjectActionInvoker().InstancePerHttpRequest();

        builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
        builder.RegisterType<JsonRepository>().As<IRepository>().InstancePerLifetimeScope().WithParameter("dataPath", HttpContext.Current.Server.MapPath(dataPath));
        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();

        _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();
    }

当我尝试访问默认控制器帐户/管理时,出现以下错误。

这就是视图的调用方式

  @Html.Action("RemoveExternalLogins")

    <h3>Add an external login</h3>
    @Html.Action("ExternalLoginsList", new { ReturnUrl = ViewBag.ReturnUrl })

控制器

  public ActionResult Manage(ManageMessageId? message)
        {
            ViewBag.StatusMessage =
                message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."
                : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set."
                : message == ManageMessageId.RemoveLoginSuccess ? "The external login was removed."
                : "";
            ViewBag.HasLocalPassword = OAuthWebSecurity.HasLocalAccount(WebSecurity.GetUserId(User.Identity.Name));
            ViewBag.ReturnUrl = Url.Action("Manage");
            return View();
        }

错误信息

   A single instance of controller 'MVC.Controllers.AccountController' cannot be used to handle multiple requests. If a custom controller factory is in use, make sure that it creates a new instance of the controller for each request.

我该如何解决这个问题?

4

1 回答 1

12

这成功了

builder.RegisterType<AccountController>().InstancePerDependency();
于 2012-12-28T16:08:31.633 回答