隨着我的寵物項目,我試圖學習使用渦輪作爲DI容器。參數化控制器構造函數永遠不會被擊中
我註冊統一的locatorprovider這樣:
static MvcApplication()
{
ServiceLocatorManager.SetLocatorProvider(() => new UnityServiceLocator());
}
我的用戶信息庫有一個參數的構造函數,我註冊爲這樣:
public class UserRepositoryRegistration : IServiceRegistration
{
public void Register(IServiceLocator locator)
{
locator.Register<IUserRepository, UserRepository>();
}
}
然後,我有我的HomeController這應該接受IUserRepository
public class HomeController : Controller
{
private readonly IUserRepository userRepository;
public HomeController(IUserRepository repository)
{
userRepository = repository;
}
}
如果我離開了參數的CT或(如上面的代碼段)我得到這個(full here):
Server Error in '/' Application. No parameterless constructor defined for this object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.MissingMethodException: No parameterless constructor defined for this object. [InvalidOperationException: An error occurred when trying to create a controller of type 'Boris.BeekProject.Guis.Web.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor.]
所以我的HomeController有義務有parless構造函數。 看來它不是實例化的Unity控制器工廠,而是默認的工廠。
UPDATE
我MVCApplication被繼承TurbineApplication而且由於RouteRegistration拾取就好了,我認爲問題出在別處。
UPDATE
作爲建議由托馬斯Eyde我想重寫TurbineApplication.AutoComponent
方法,但是檢查的對象瀏覽器,我看不到這種方法的任何引用。 此外,當我看着NerdDinner示例時,它似乎也沒有重寫此方法。 檢查online documentation about it後,我沒有得到任何明智的,並通過鏈接到文檔約doing the registration manually服務我一個佔位符頁面。 任何人都可以填寫我做錯了什麼?
我錯過了什麼嗎?
此外,格式化此異常的任何提示將很好。我在meta上發佈了一個相關的問題:http://meta.stackexchange.com/questions/32264/posting-a-stack-trace – 2009-12-08 11:21:05
你是否繼承了TurbineApplication類給你Global.asax MvcApplication類? – 2009-12-08 13:46:52
@Magnus:是的,它是從TurbineApplication繼承而來的。奇怪的是,IRouteRegistration收到罰款。 – 2009-12-08 14:31:50