Castle Windsor ResolveAll 因类型参数异常而失败

发布于 2024-10-03 11:35:55 字数 1468 浏览 0 评论 0原文

我正在使用温莎城堡进行 IoC。它一直工作得很好,但是每次我尝试运行我的应用程序时,它都会在 ResolveAll 调用期间失败:

var resolved = container.ResolveAll<IValidator>();

它会抛出此异常:

System.ArgumentException occurred
  Message=The number of generic arguments provided doesn't equal the arity of the generic type definition.
Parameter name: instantiation
  Source=mscorlib
  ParamName=instantiation
  StackTrace:
       at System.RuntimeType.MakeGenericType(Type[] instantiation)
       at Castle.MicroKernel.Handlers.DefaultGenericHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\Handlers\DefaultGenericHandler.cs:line 51
  InnerException: 

真正奇怪的是,它一直工作得很好,直到今天。如果我回滚到开始执行此操作之前的版本,即使回滚到上周,我仍然会收到此错误。我尝试过重新启动等。以前有人见过这个吗?

编辑

这是我注册 IValidator 服务的方式:

    private void registerFromAssembly(IWindsorContainer container, Assembly assembly)
    {
        container.Register(
            AllTypes.FromAssembly(assembly)
                .BasedOn<IValidator>()
        );
    }

这是我注册 IPresenterResolver 服务的方式:

        container.Register(
            Component.For<IPresenterResolver>()
                .ImplementedBy<CommandLineArgumentPresenterResolver>()
        );

我必须删除这两个服务才能让应用程序立即运行。

I'm using Castle Windsor for IoC. It's been working great, but all of a sudden every time I try to run my application, it fails during a ResolveAll call:

var resolved = container.ResolveAll<IValidator>();

It throws this exception:

System.ArgumentException occurred
  Message=The number of generic arguments provided doesn't equal the arity of the generic type definition.
Parameter name: instantiation
  Source=mscorlib
  ParamName=instantiation
  StackTrace:
       at System.RuntimeType.MakeGenericType(Type[] instantiation)
       at Castle.MicroKernel.Handlers.DefaultGenericHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\Handlers\DefaultGenericHandler.cs:line 51
  InnerException: 

The really odd thing is that it's been working great up until today. If I roll back to a version before it started doing this, even back to last week, I still get this error. I've tried rebooting, etc. Has anyone seen this before?

EDIT:

Here's how I'm registering the IValidator ones:

    private void registerFromAssembly(IWindsorContainer container, Assembly assembly)
    {
        container.Register(
            AllTypes.FromAssembly(assembly)
                .BasedOn<IValidator>()
        );
    }

Here's how I'm registering the IPresenterResolver service:

        container.Register(
            Component.For<IPresenterResolver>()
                .ImplementedBy<CommandLineArgumentPresenterResolver>()
        );

I have to remove both of these to get the application to run now.

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

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

发布评论

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

评论(1

秋心╮凉 2024-10-10 11:35:55

我必须下载温莎城堡源代码,以便我可以在故障源处对其进行调试。事实证明,我错误地包含了一些通用基本类型服务,而我只期望非通用具体实现。我必须将我的注册代码更改为此才能使其工作:

    private void registerFromAssembly(IWindsorContainer container, 
        Assembly assembly)
    {
        container.Register(
            AllTypes.FromAssembly(assembly)
                .BasedOn<IValidator>()
                    .Unless(type => type == typeof(FluentValidatorWrapper<>))
                    .Unless(type => type == typeof(PassEverythingValidator<>))
        );
    }

在本例中,FluentValidatorWrapper 是我用来构建特定于类型的验证器的通用基本类型,而 PassEverythingValidator 是一个通用的当我的验证逻辑找不到特定于类型的验证器时手动实例化的类型。这些不应该包含在容器中。

我在温莎城堡邮件列表上做了一个注释,如果具体的类型信息已包含在异常中,那么调试起来会更容易。

I had to download the Castle Windsor source code so I could debug it at the source of the failure. It turns out that I was incorrectly including some generic base type services where I was only expecting non-generic concrete implementations. I had to change my registration code to this to make it work:

    private void registerFromAssembly(IWindsorContainer container, 
        Assembly assembly)
    {
        container.Register(
            AllTypes.FromAssembly(assembly)
                .BasedOn<IValidator>()
                    .Unless(type => type == typeof(FluentValidatorWrapper<>))
                    .Unless(type => type == typeof(PassEverythingValidator<>))
        );
    }

In this case FluentValidatorWrapper is a generic base type I use to build type-specific validators and PassEverythingValidator is a generic type that my validation logic manually instantiates when it can't find a type-specific validator. These shouldn't have been included in the container.

I made a note on the Castle Windsor mailing list that if the concrete type information had been included in the exception, it would have been easier to debug.

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