温莎城堡 3,构造函数参数为字符串

发布于 2024-12-18 12:01:51 字数 1795 浏览 2 评论 0原文

我以前从未使用过 Windsor,但使用过其他 DI 框架,目前我遇到了一个相当奇怪的问题。

我有一个工厂类,它在其构造函数中接受一个字符串,但是每当我尝试解析该对象时,我都会收到一个异常:

Handler for System.String was not found.

<Message>Handler for System.String was not found.</Message>
<StackTrace>at Castle.MicroKernel.Resolvers.DefaultDependencyResolver
     .TryGetHandlerFromKernel(DependencyModel dependency, CreationContext context) 
     in d:\60b7fa65294e7792\src\Castle.Windsor\MicroKernel\Resolvers\DefaultDependencyResolver.cs:line 403
at Castle.MicroKernel.Resolvers.DefaultDependencyResolver.ResolveCore(CreationContext context, ComponentModel model, DependencyModel dependency) in d:\60b7fa65294e7792\src\Castle.Windsor\MicroKernel\Resolvers\DefaultDependencyResolver.cs:line 270</StackTrace>
<Type>Castle.MicroKernel.Handlers.HandlerException</Type>
</InnerException>
<Message>Missing dependency.
Component SomeExampleFactory has a dependency on System.String, which could not be 
resolved.
Make sure the dependency is correctly registered in the container as a service, or 
provided as inline argument.</Message>

该类看起来像:

public interface IDummyFactory
{
    void DoSomething();
}

public class DummyFactory : IDummyFactory
{
    private string someString;

    public DummyFactory(string someConstructorArg)
    {
        someString = someConstructorArg;
    }
}

使用下面的 DI 设置:

var someString = "some constructor arg";
_container.Register(Component.For<IDummyFactory>()
                             .ImplementedBy<DummyFactory>()
                             .DependsOn(someString));

我假设它正在尝试进行某种类型的转换或格式化导致它爆炸,但由于类型本身是一个字符串,并且变量在字符串中传递......甚至可能是它试图映射该变量的类型而不是内容可变,但对DI框架和文档了解不够在这个区域周围

I have never used Windsor before but have used other DI frameworks, and I have got a rather strange issue at the moment.

I have a factory class which takes a string in its constructor, however whenever I try and resolve that object I get an exception saying:

Handler for System.String was not found.

<Message>Handler for System.String was not found.</Message>
<StackTrace>at Castle.MicroKernel.Resolvers.DefaultDependencyResolver
     .TryGetHandlerFromKernel(DependencyModel dependency, CreationContext context) 
     in d:\60b7fa65294e7792\src\Castle.Windsor\MicroKernel\Resolvers\DefaultDependencyResolver.cs:line 403
at Castle.MicroKernel.Resolvers.DefaultDependencyResolver.ResolveCore(CreationContext context, ComponentModel model, DependencyModel dependency) in d:\60b7fa65294e7792\src\Castle.Windsor\MicroKernel\Resolvers\DefaultDependencyResolver.cs:line 270</StackTrace>
<Type>Castle.MicroKernel.Handlers.HandlerException</Type>
</InnerException>
<Message>Missing dependency.
Component SomeExampleFactory has a dependency on System.String, which could not be 
resolved.
Make sure the dependency is correctly registered in the container as a service, or 
provided as inline argument.</Message>

The class looks something like:

public interface IDummyFactory
{
    void DoSomething();
}

public class DummyFactory : IDummyFactory
{
    private string someString;

    public DummyFactory(string someConstructorArg)
    {
        someString = someConstructorArg;
    }
}

With DI setup below:

var someString = "some constructor arg";
_container.Register(Component.For<IDummyFactory>()
                             .ImplementedBy<DummyFactory>()
                             .DependsOn(someString));

I am assuming it is trying to do some sort of casting or formatting that is causing it to bomb out, but as the type itself is a string, and the variable being passed in a string... it may even be a case that it is trying to map the type of that variable rather than the variable content, but I do not know enough about the DI framework and the documentation around this area

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

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

发布评论

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

评论(2

自控 2024-12-25 12:01:51

我也在寻找这个问题的答案,看起来他们现在有一些更简单的东西,他们称之为“内联依赖项”,它是通过(以及其他方式) Dependency.OnValue() 方法实现的。

以下是文档中的通用示例:

var twitterApiKey = @"the key goes here";

 container.Register(
    Component.For<ITwitterCaller>().ImplementedBy<MyTwitterCaller>()
        .DependsOn(Dependency.OnValue("APIKey", twitterApiKey))
    );

它将使用 twitterApiKey 中的值作为名称为“APIKey”(不区分大小写)的参数。

https://github.com/castleproject/Windsor/blob/ master/docs/inline-dependency.md

看起来这可能已经在 3.1 版本中出现了,但我无法完全破译他们的更新标记约定。

I was looking for an answer to this too and it appears like they now have something a little simpler which they call "Inline Dependencies" that is implemented by (among other ways) the Dependency.OnValue() method.

Here is the generic example from the documentation:

var twitterApiKey = @"the key goes here";

 container.Register(
    Component.For<ITwitterCaller>().ImplementedBy<MyTwitterCaller>()
        .DependsOn(Dependency.OnValue("APIKey", twitterApiKey))
    );

It will use the value in twitterApiKey for the paramter that has the name "APIKey" (not case sensitive).

https://github.com/castleproject/Windsor/blob/master/docs/inline-dependencies.md

It looks like this may have gone in as of version 3.1, but I can't quite decipher their update tagging convention.

栖迟 2024-12-25 12:01:51

尝试调用 DependsOn 的重载,它采用键/值对的 IDictionary 来指定依赖项:

_container.Register(
    Component.For<IDummyFactory>()
        .ImplementedBy<DummyFactory>()
        .DependsOn(new Hashtable 
        { 
            { "someConstructorArg", "some constructor arg" }
        }));

Try calling the overload of DependsOn which takes an IDictionary of Key/Value pairs to specify dependencies:

_container.Register(
    Component.For<IDummyFactory>()
        .ImplementedBy<DummyFactory>()
        .DependsOn(new Hashtable 
        { 
            { "someConstructorArg", "some constructor arg" }
        }));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文