I have a strange problem with following structuremap configuration example:
IReturnValue
public interface IReturnValue {
}
IService
public interface IService<T> where T : IReturnValue {
void Execute();
}
with two implementations:
ServiceA:
public class ServiceA : IService<IReturnValue> {
public void Execute() {
//do something
}
}
ServiceB
public class ServiceB : IService<IReturnValue> {
public void Execute() {
//do something
}
}
Registry
So now I register ServiceA and ServiceB in the main method: (note the order!)
static void Main(string[] args)
{
IContainer container = new Container();
container.Configure(c => c.For<ServiceB>().Use<ServiceB>());
container.Configure(c => c.For<IService<IReturnValue>>().Use<ServiceA>());
}
Problem:
During the registration of ServiceA this exception throws:
Specified argument was out of the range of valid values.
Parameter name: instance 'Example.ServiceA' with ReturnType
Example.ServiceA cannot be cast to Example.ServiceB
No InnerException and no other errors.
Registry changed order
When I change the order of the registration, everything works and I can use the services.
static void Main(string[] args)
{
IContainer container = new Container();
container.Configure(c => c.For<IService<IReturnValue>>().Use<ServiceA>());
container.Configure(c => c.For<ServiceB>().Use<ServiceB>());
}
Can someone explain me why structuremap has a problem with the first registration example?
- StructureMap: 3.0.5.130
- .Net 4.5