ASP MVC ControllerFactory在AccountController上失败

时间:2015-11-03 10:43:31

标签: c# asp.net-mvc dependency-injection unity-container

我正在使用Unity,即使我不应该注册我的控制器(据我所知),我在我的AccountController上收到以下错误,这只是从默认的MVC模板中获取的错误在制作新项目时。

The IControllerFactory 'WebApplication1.Models.ControllerFactory' did not return a controller for the name 'Account'.

堆栈追踪:

[InvalidOperationException: The IControllerFactory 'WebApplication1.Models.ControllerFactory' did not return a controller for the name 'Account'.]
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +336
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +50
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +103
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

ControllerFactory如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.Practices.Unity;

namespace WebApplication1.Models
{
public class ControllerFactory : DefaultControllerFactory
{
    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        try
        {
            if (controllerType == null)
            {
                throw new ArgumentNullException("controllerType");
            }
            if (!typeof(IController).IsAssignableFrom(controllerType))
            {
                throw new ArgumentException(string.Format("Type requested is not a controller: {0}", controllerType.Name), "controllerType");
            }
            return MvcUnityContainer.Container.Resolve(controllerType) as IController; //This is where it fails
        }
        catch (Exception e)
        {
            return null;

        }
    }
}

public static class MvcUnityContainer
{
    public static UnityContainer Container { get; set; }
}
}

抓住的异常给了我这个:

e = {"Resolution of the dependency failed, type = \"WebApplication1.Controllers.AccountController\", name = \"(none)\".\r\nException occurred while: while resolving.\r\nException is: InvalidOperationException - The current type, Microsoft.AspNet.Identity.IUserS...

我不知道从哪里开始,任何帮助都表示赞赏。

更新:

AccountController类:

[Authorize]
public class AccountController : Controller
{
    private ApplicationSignInManager _signInManager;
    private ApplicationUserManager _userManager;

    public AccountController()
    {
    }

    public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
    {
        UserManager = userManager;
        SignInManager = signInManager;
    }

    public ApplicationSignInManager SignInManager
    {
        get
        {
            return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
        }
        private set
        {
            _signInManager = value;
        }
    }

    public ApplicationUserManager UserManager
    {
        get
        {
            return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }

    //
    // GET: /Account/Login
    [AllowAnonymous]
    public ActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;
        return View();
    }
}

更新2:

正如Steven所说,我已经从工厂中移除了try-catch并获得了这个例外:

An exception of type 'Microsoft.Practices.Unity.ResolutionFailedException' occurred in Microsoft.Practices.Unity.dll but was not handled in user code

Additional information: Resolution of the dependency failed, type = "WebApplication1.Controllers.AccountController", name = "(none)".

Exception occurred while: while resolving.

Exception is: InvalidOperationException - The current type, Microsoft.AspNet.Identity.IUserStore`1[WebApplication1.Models.ApplicationUser], is an interface and cannot be constructed. Are you missing a type mapping?

-----------------------------------------------

At the time of the exception, the container was:



  Resolving WebApplication1.Controllers.AccountController,(none)

  Resolving parameter "userManager" of constructor WebApplication1.Controllers.AccountController(WebApplication1.ApplicationUserManager userManager, WebApplication1.ApplicationSignInManager signInManager)

    Resolving WebApplication1.ApplicationUserManager,(none)

    Resolving parameter "store" of constructor WebApplication1.ApplicationUserManager(Microsoft.AspNet.Identity.IUserStore`1[[WebApplication1.Models.ApplicationUser, WebApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] store)

      Resolving Microsoft.AspNet.Identity.IUserStore`1[WebApplication1.Models.ApplicationUser],(none)

3 个答案:

答案 0 :(得分:1)

我通过将以下两行添加到Bootstrapper中的BuildUnityContainer()方法来修复错误。

container.RegisterType<AccountController>(new InjectionConstructor());
container.RegisterType<ManageController>(new InjectionConstructor());

答案 1 :(得分:1)

安装Unity.MVC之后,我遇到了这个问题。我在登录/登出网站时遇到错误。事实证明,控制器构造函数必须在Unity参数和标识参数之间进行分隔,如下所示:

public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
{
    UserManager = userManager;
    SignInManager = signInManager;
}

public AccountController(IUserRepository _repUser, IConfigRepository _repConfig)
{
    repUser = _repUser;
    repConfig = _repConfig;
}

答案 2 :(得分:0)

从您的自定义try中删除catch - ControllerFactory.GetControllerInstance语句,您将立即看到问题所在。您可以将该方法剥离为:

protected override IController GetControllerInstance(RequestContext requestContext, 
    Type controllerType)
{
    return (IController)MvcUnityContainer.Container.Resolve(controllerType);
}