I have got a Circular dependency to extend the functionality of an object adding a cache abstraction.
public class CachedDecorator : IParameter
{
public CachedDecorator (IParameter decorated);
...
}
public class MyImplementationParameter : IParameter
{
...
}
Using .Net core Dependency injection container i can do something like this to create those dependencies and it works:
services.AddSingleton<IParameter>(provider => new CachedDecorator
(provider.GetRequiredService<MyImplementationParameter >()));
How Can I replicate that declaration using Autofac?
I am trying something like this:
builder.RegisterType<CachedDecorator>()
.As<IParameter>()
.WithParameter(
new ResolvedParameter(
(pi, ctx) => pi.ParameterType == typeof(MyImplementationParameter ),
(pi, ctx) => ctx.Resolve<MyImplementationParameter>()));
But i get a "Circular component dependency detected: "
As of Autofac 4.9, you can register your decorator as follows:
builder.RegisterType<MyImplementationParameter>().As<IParameter>();
builder.RegisterDecorator<CachedDecorator, IParameter>();
See the documentation for more information.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.