3

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
feitzi
  • 98
  • 7
  • Why are you registering `ServiceB`? the container would be able to resolve classes even without registering them. – Yacoub Massad Oct 24 '15 at 00:22
  • Yes this is correct. But i think that the registration of ServiceB should be also possible and the exception makes no sense for me. – feitzi Oct 24 '15 at 12:08
  • I stumbled on the same limitation but for me this is a showstopper. Did you ever find out a solution? – julealgon Sep 08 '17 at 16:02

0 Answers0