Windsor 3 castle with constructor argument as string

I have never used Windsor before, but used a different DI framework, and for now I have a rather strange problem.

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

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 this:

 public interface IDummyFactory { void DoSomething(); } public class DummyFactory : IDummyFactory { private string someString; public DummyFactory(string someConstructorArg) { someString = someConstructorArg; } } 

With the DI setting below:

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

I assume that he is trying to do some kind of discarding or formatting, which will cause it to start, but since the type itself is a string and the variable is passed in the string ... it could even be that it tries to match the type of this variable, not the contents of the variable, but I don’t know enough about the DI structure and documentation for this area.

+6
source share
2 answers

Try calling DependsOn overload, which accepts IDictionary key / value pairs to specify dependencies:

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

I was also looking for the answer to this question, and it looks like they now have something simpler that they call "Inline Dependencies", which is implemented (among other things) by the Dependency.OnValue () method.

Here is a general 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 a parameter named "APIKey" (case insensitive).

http://docs.castleproject.org/Windsor.Inline-Dependencies.ashx

It looks like this could happen with version 3.1, but I can’t fully decipher their update tagging convention.

+10
source

Source: https://habr.com/ru/post/902543/


All Articles