参数化控制器构造函数永远不会被命中

发布于 2024-08-14 03:33:09 字数 2229 浏览 6 评论 0原文

在我的宠物项目中,我试图学习使用 Turbine 作为 DI 容器。

我将 unity 注册为 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;
    }
}

如果我省略了无参数构造函数(如上面的代码片段所示) )我明白了(完整内容):

 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 必须有一个无可比拟的 ctor。 看起来实例化的不是 Unity 控制器工厂,而是默认控制器工厂。

更新
我的 MVCApplication 继承了 TurbineApplication,并且由于 RouteRegistration 被很好地拾取,我认为问题出在其他地方。

更新
正如 Thomas Eyde 所建议的,我想重写 TurbineApplication.AutoComponent 方法,但是检查对象浏览器,我看不到对此方法的任何引用。 此外,当我查看 NerdDinner 示例时,它似乎也没有覆盖此方法。 在检查了有关它的在线文档之后,我没有得到任何明智的信息并点击有关手动进行注册的文档链接为我提供了占位符页面。 有人可以告诉我我做错了什么吗?

我错过了什么吗?

With my pet project I'm trying to learn to use Turbine as a DI container.

I'm registering unity as locatorprovider as such:

static MvcApplication()
{
    ServiceLocatorManager.SetLocatorProvider(() => new UnityServiceLocator());
}

My user repository has a parameterless constructor and I'm registering it as such:

public class UserRepositoryRegistration : IServiceRegistration
{
    public void Register(IServiceLocator locator)
    {
        locator.Register<IUserRepository, UserRepository>();
    }
}

Then I have my HomeController which should accept an IUserRepository

public class HomeController : Controller
{
    private readonly IUserRepository userRepository;

    public HomeController(IUserRepository repository)
    {
        userRepository = repository;
    }
}

If I leave out the parameterless ctor (like in the above code snippet) I get this (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.]

So my HomeController is obligated to have a parless ctor.
It seems it's not the Unity controller factory that's instantiating, but rather the default one.

UPDATE
My MVCApplication is inheriting TurbineApplication and since the RouteRegistration is picked up just fine I think the problem lies somewhere else.

UPDATE
As suggested by Thomas Eyde I wanted to override the TurbineApplication.AutoComponent method, but checking the Object Browser, I can't see any reference to this method.
Furthermore when I look at the NerdDinner example, it doesn't seem to override this method either.
After checking the online documentation about it I failed to get any the wiser and following the link to documentation about doing the registration manually serves me a placeholder page.
Can anybody fill me on on what I'm doing wrong?

Am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怼怹恏 2024-08-21 03:33:09

您的 Turbine 版本不支持 MVC2,其中 ControllerFactory 已更改为支持用于创建控制器的额外参数。这解释了为什么你的控制器永远无法正确实例化。 MVC2 的重大更改列出了此更改。

如果您想使用 MVC2为了您的开发 Beta(适用于 .NET4),我建议您获取我刚刚发布的新的MVC2 (.NET4) 位。

希望对您有帮助!

Your build of Turbine doesn't support MVC2, in which the ControllerFactory is changed to support an extra parameter for the creation of controllers. This is explains why your controller never gets instantiated correctly. The breaking changes of MVC2 list this change.

If you're wanting to use MVC2 Beta (for .NET4) for your development, I suggest you get the new MVC2 (.NET4) bits I just released.

Hope that helps you out!

难理解 2024-08-21 03:33:09

您必须重写 TurbineApplication.AutoComponentSetup 方法,如下所示:

protected override void AutoComponentSetup(IServiceLocator locator)
{
    //Remove this if you want to skip all other previous registrations
    base.AutoComponentSetup(locator);

    AutoRegistration<IMyCustomInterface>((loc, type) => loc.Register<IMyCustomInterface>(type));
}

另请参阅 组件自动注册

You have to override the TurbineApplication.AutoComponentSetup-method, something like this:

protected override void AutoComponentSetup(IServiceLocator locator)
{
    //Remove this if you want to skip all other previous registrations
    base.AutoComponentSetup(locator);

    AutoRegistration<IMyCustomInterface>((loc, type) => loc.Register<IMyCustomInterface>(type));
}

See also Auto Registration for Components

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文