How to use Dependency.OnValue to differentiate between 2 parameters with the same name but different types

StackOverflow https://stackoverflow.com/questions/23509455

  •  16-07-2023
  •  | 
  •  

Question

I encountered an interesting scenario while trying to use an autogenerated web service client. This means I cannot modify the other class constructor. It had 2 constructors where it was as follows:-

public FooServicePortTypeClient(string endPointConfigurationName, string remoteAddress)
public FooServicePortTypeClient(string endPointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress)

Note how the parameter name "remoteAddress" is used twice but for one constructor it is a string and for the other it is an EndpointAddress object.

I was trying to register my web service through the windsor container with inline dependencies by using the DependsOn to pass the arguments for the service client in as such:-

container.Register(Component.For<FooServicePortTypeClient>().DependsOn(Dependency.OnValue("endPointConfigurationName", "fooEndpointConfigname"), Dependency.Onvalue("remoteAddress", "fooRemoteAddress"))));

I then noticed that castle windsor was trying to match the string for the remoteAddress to the constructor with the typed parameter EndpointAddress (and consequently failing) instead of choosing the string version.

Is there a way to actually be able to differentiate between the 2 "different" parameters so that I can choose either to use the one with string or the one with EndpointAddress?

I found I can only specify the parameter name with Dependency.OnValue and its pointless to try the typed version because there are 2 strings (endPointConfigurationName and remoteAddress) in the constructor and it will not be able to differentiate between them.

Personally this is trivial and I could definitely just do what Castle Windsor is looking for by supplying a new object of the EndpointAddress to it. However, I just wanted to know how castle windsor decides which constructor version to use with the Dependency.OnValue and if there is actually a way to make it prefer one or the other. Certainly I think this is a trap for inexperienced users like me that was expecting the string version will just work.

Was it helpful?

Solution

The Dependency.OnValue has an overload that allows you to supply a type:

Dependency.OnValue<System.ServiceModel.EndpointAddress>(endpointAddress)

This tells Windsor to resolve the "higher level" type (vs a basic type like string or int). See the docs for more details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top