2
votes

Let's have the following code

public class Handler : IHandle<ICommentInfo>{}

public class Command1 : ICommentInfo{}

public interface ICommentInfo{}

public interface IHandle<T> where T : class{}

I'd like to resolve the service like this

var service = c.Resolve<IHandle<Command1>>();

Is this even possible ?

I tried this configuration

 builder.RegisterType<Handler>().As<IHandle<ICommentInfo>>();

But I'm getting this exception

The requested service 'Icp.Test.QuerySpec.Class1+IHandle`1[[Icp.Test.QuerySpec.Class1+Command1, Icp.Test.QuerySpec, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.
2

2 Answers

0
votes

Why does you do not want to use

ContainerBuilder builder = new ContainerBuilder();
builder.RegisterType<LocalCommand>().As(typeof (ICommentInfo));
builder.RegisterType<Handler>().As(typeof(IHandle<ICommentInfo>));
var c = builder.Build();
var handler = c.Resolve<IHandle<ICommentInfo>>();
0
votes
bulider.RegisterSource(new ContravariantRegistrationSource());

Switches on this behaviour.