I'm trying to register some endpoints I've created, however, Autofac is throwing an exception, saying I haven't registered my BControl class. Here is my registration code:
builder.RegisterType<GControlEndpoint>().As<IBusConsumer>().SingleInstance();
builder.RegisterType<BControlEndpoint>().As<IBusConsumer>().SingleInstance();
builder.RegisterType<BSControlEndpoint>().As<IBusConsumer>().SingleInstance()
.WithParameter((pi, c) => pi.Name == "bControlEndpoint",
(pi, c) => c.Resolve<BControlEndpoint>())
.WithParameter((pi, c) => pi.Name == "gControlEndpoint",
(pi, c) => c.Resolve<GControlEndpoint>());
And my BSControlEndpoint code is as follows:
private BControlEndpoint bControlEndpoint;
private GControlEndpoint gControlEndpoint;
public BSControlEndpoint(
BControlEndpoint bControlEndpoint,
GControlEndpoint gControlEndpoint)
{
this.bControlEndpoint = bControlEndpoint;
this.gControlEndpoint = gControlEndpoint;
}
Any help would be appreciated.
Autofac error is :
Autofac.Core.Registration.ComponentNotRegisteredException: The requested service 'Sa.BControlEndpoint' 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.
Add
AsSelfto the registration definition of dependencies:Also this should make
WithParametercall not needed:Another option is to look into keyed dependencies registration (docs)