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 AsSelf
to the registration definition of dependencies:
builder.RegisterType<GControlEndpoint>()
.As<IBusConsumer>()
.AsSelf()
.SingleInstance();
builder.RegisterType<BControlEndpoint>()
.As<IBusConsumer>()
.AsSelf()
.SingleInstance();
Also this should make WithParameter
call not needed:
builder.RegisterType<BSControlEndpoint>().As<IBusConsumer>().SingleInstance();
Another option is to look into keyed dependencies registration (docs)