我正在編寫一個Web應用程序,我已經使用默認的Microsoft MVC站點作爲起點。在此之後,我創建了一個食譜數據庫,用於使用實體框架在我的Web應用程序中,並編寫了一個存儲庫和一些業務層方法。然後我使用Unity的依賴注入來消除它們之間的耦合。我使用了放在global.asax.cs中的MvcApplication類中的這段代碼。依賴注入與默認MVC中的統一破解登錄
private IUnityContainer Container;
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
ConfigureObjects();
}
private void ConfigureObjects()
{
Container = new UnityContainer();
Container.RegisterType<IRecipeRequest, BasicRecipeRequest>();
Container.RegisterType<IRecipeRepository, RecipeRepositoryEntityFramework>();
Container.RegisterType<IRecipeContext, RecipeContext>();
DependencyResolver.SetResolver(new UnityDependencyResolver(Container));
}
我的位只是與依賴注入有關的位。 。
這樣使用與用戶登錄任何網頁將返回值和錯誤例如註冊,登錄後,它會返回標題爲一個錯誤這樣的:
The current type, Microsoft.AspNet.Identity.IUserStore`1[UKHO.Recipes.Www.Models.ApplicationUser], is an interface and cannot be constructed. Are you missing a type mapping?
考慮使用診斷在Visual Studio中拋出的異常工具,我得到這個:
"An error occurred when trying to create a controller of type 'UKHO.WeeklyRecipes.Www.Controllers.AccountController'. Make sure that the controller has a parameterless public constructor."
AccountsContoler是deafult創造了一個控制器,我沒有碰過它包含一個參數的構造函數和另一個構造。他們是這樣的:
public AccountController()
{
}
public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
{
UserManager = userManager;
SignInManager = signInManager;
}
通過將[InjectionConstructor()]
在參數的構造函數的錯誤走的前面。在我看來,Unity試圖解決ApplicationUserManager和ApplicationSignInManager,即使我沒有註冊這些類型,並且使得統一看到空構造函數,所以什麼都不做。我主要想知道爲什麼會發生這種情況,因爲我的印象是團結只會干擾你登記的類型。黃油解決方案也是受歡迎的。
編輯:當您希望更改帳戶設置時也會發生這種情況,但使用ManageContoler出現錯誤時,也可以通過將[InjectionConstructor()]
放在空的構造函數前解決。