2014-04-03 17 views
5

我想設置MVC5應用程序使用Unity 3 我創建從一個標準的模板默認Web應用程序mvc5再加入統一類型IUserStore`1不具有可訪問的構造

當我訪問註冊行動在AccountController中,我收到以下異常:

IUserStore`1類型沒有可訪問的構造函數。

from this post How to add MVC 5 authentication to Unity IoC?我知道問題在於Unity選擇帶較長參數列表的構造函數。

的解決方案是註冊帳號控制器與默認的構造函數可以使用以下方法:

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

我想什麼做的是在配置文件中的代碼 難道沒有註冊它可能在web.config中做同樣的事情?

最好的問候, 塞巴斯蒂安

回答

4

可以使用XML配置來配置統一。在你的情況下,它會是這個樣子:

<configSections> 
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/> 
</configSections>  

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">  
    <container> 
    <register type="MyApp.AccountController, MyApp"> 
     <constructor /> 
    </register> 
    </container> 
</unity> 

然後你需要明確加載配置:

IUnityContainer container = new UnityContainer(); 
container.LoadConfiguration(); 
+0

正是我需要的。乾杯! –

相關問題