温莎城堡 3,构造函数参数为字符串
我以前从未使用过 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也在寻找这个问题的答案,看起来他们现在有一些更简单的东西,他们称之为“内联依赖项”,它是通过(以及其他方式) Dependency.OnValue() 方法实现的。
以下是文档中的通用示例:
它将使用 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:
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.
尝试调用
DependsOn
的重载,它采用键/值对的IDictionary
来指定依赖项:Try calling the overload of
DependsOn
which takes anIDictionary
of Key/Value pairs to specify dependencies: