单个控制器实例不能用于处理多个请求 - Autofac

HaB*_*aBo 1 controller autofac requestfactory asp.net-mvc-4

Autofac.ContainerBuilder在我的ASP.Net MVC 4应用程序中使用,这使得默认控制器出现问题以生成多个实例.

我试着这样处理它

 builder.RegisterType<AccountController>().As<IController>().PreserveExistingDefaults();
Run Code Online (Sandbox Code Playgroud)

但没用

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();
    }
Run Code Online (Sandbox Code Playgroud)

当我尝试访问默认控制器帐户/管理我得到以下错误.

这我是如何看待的

  @Html.Action("RemoveExternalLogins")

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

调节器

  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();
        }
Run Code Online (Sandbox Code Playgroud)

错误信息

   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.
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

HaB*_*aBo 11

这样做了

builder.RegisterType<AccountController>().InstancePerDependency();
Run Code Online (Sandbox Code Playgroud)

  • 就像一个注释 - 确保在注册控制器后添加它:builder.RegisterControllers(typeof(MvcApplication).Assembly).InstancePerRequest(); builder.RegisterType <的AccountController>()InstancePerDependency(); (2认同)