autofac - Not able to use EnableClassInterceptors with expression registration or lambda registration techniuqe

50 Views Asked by At

Below is my normal registration line for the Service and it was working fine till now. I have to use EnableClassInterceptors and I can't use EnableInterfaceInterceptors because of reason provided here Can't use EnableInterfaceInterceptors

  builder.RegisterType<Service>().As<IService>().PropertiesAutowired().SingleInstance().EnableClassInterceptors().InterceptedBy(typeof(MyInterceptor));

Now, for some reason, the we had to add a constructor parameter to Service, and that constructor parameter type is registered as Named registration in container. So now, above registration is failing as it is not able to resolve that parameter by it's own. I have the option to use registration expression where I can resolve all constructor parameters manually and create the Service object, but it causes me unable to use the EnableClassInterceptors because of below error

Error CS0311 The type 'Autofac.Builder.SimpleActivatorData' cannot be used as type parameter 'TConcreteReflectionActivatorData' in the generic type or method 'RegistrationExtensions.EnableClassInterceptors<TLimit, TConcreteReflectionActivatorData, TRegistrationStyle>(IRegistrationBuilder<TLimit, TConcreteReflectionActivatorData, TRegistrationStyle>)'. There is no implicit reference conversion from 'Autofac.Builder.SimpleActivatorData' to 'Autofac.Builder.ConcreteReflectionActivatorData'.

I wanted to know the way to go from here.

1

There are 1 best solutions below

0
QI You On

From the error point of view, it should be a problem with the variable. Autofac.Builder.SimpleActivatorData is of a different type from Autofac.Builder.ConcreteReflectionActivatorData, with different attributes and behaviors.

Try this code:

c => new ConcreteReflectionActivatorData(c.Resolve<SimpleActivatorData>())

What this code means is to create a new instance of the ConcreteReflectionActivatorData class and initialize it with the data parsed from SimpleActivatorData.