带有MVC的ASP.NET WebAPI - 针对HTTPPOST的Unity Controller Injection失败

时间:2014-01-10 23:22:55

标签: asp.net-mvc-4 dependency-injection asp.net-web-api http-post unity-container

我有一个简单的登录控制器,如下所示:

public class LoginController : Controller
{
    private IAccountService _accountService = null;
    private IFormsAuthenticationService _formsAuthenticationService = null;

    public LoginController() {}

    [InjectionConstructor]
    public LoginController(IAccountService accountService, IFormsAuthenticationService formsAuthenticationService)
    {
        _accountService = accountService;
        _formsAuthenticationService = formsAuthenticationService;
    }

    //
    // GET: /Login/

    public ActionResult Index()
    {
        return View();          
    }

    //
    // POST: /Login/

    [HttpPost]
    public ActionResult Index(Credentials credentials)
    {
        try
        {
            if (_accountService.ValidateUser(credentials) != null)
            {
                _formsAuthenticationService.SignIn(credentials.UserName, true);
                return RedirectToAction("Index", "Home");
            }
            else
                return RedirectToAction("Index", new { failed = true });
        }
        catch
        {
            throw;
        }
    }
}

在当地,一切正常。当我将应用程序放在我的Web服务器(共享主机,ASP.NET 4.0)上时,构造函数注入对于GET工作正常,但不会为POST启动。

基本上对于POST,将触发默认构造函数而不是注入构造函数。我不确定Unity是否还在为POST动词做准备(我该如何测试?)。

我正在使用Boostrapper.cs,正如我所说,在我的开发PC上一切正常,所以可能在Web服务器上存在问题。我可能会更改一些服务器设置。有什么建议吗?

干杯 -

1 个答案:

答案 0 :(得分:0)

Bollocks - 看起来问题是由于我试图加载错误版本的EntityFramework DLL(v6而不是4.1)。这不会出现在堆栈跟踪中,但是当我发现这个并替换了DLL时,事情再次开始正常工作。