Context: Service Fabric cluster with AutoFac 4.9.4 for DI, Castle.Core.AsyncInterceptor for telemetry injection. We noticed DynamicProxyGenAssembly2 modules leaking.
This is the setup for classes and DI:
public class ServiceInstanceGenerator
{
private readonly LoggingInterceptor _loggingInterceptor;
private readonly ProxyGenerator _proxyGenerator;
public ServiceInstanceGenerator(LoggingInterceptor loggingInterceptor, ProxyGenerator proxyGenerator)
{
_loggingInterceptor = loggingInterceptor;
_proxyGenerator = proxyGenerator;
}
public TInterface GetActor<TInterface>(ActorId id) where TInterface : class, IActor
{
var service = ActorProxy.Create<TInterface>(id);
return _proxyGenerator.CreateInterfaceProxyWithTargetInterface(service, _loggingInterceptor);
}
public TInterface GetService<TInterface>(Uri uri) where TInterface : class, IService
{
var service = ServiceProxy.Create<TInterface>(uri);
return _proxyGenerator.CreateInterfaceProxyWithTargetInterface(service, _loggingInterceptor);
}
}
internal class ContainerModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<ProxyGenerator>().AsSelf().SingleInstance();
builder.RegisterInstance(new JsonSerializerOptions() { Converters = { new ByteArrayConverter() } });
builder.RegisterType<LoggingInterceptor>().AsSelf().SingleInstance();
builder.RegisterType<ServiceInstanceGenerator>().AsSelf().SingleInstance();
builder.RegisterType<AssignmentsActor>().AsSelf().SingleInstance();
builder.RegisterType<TelemetryClient>().AsSelf().SingleInstance();
builder.Register((c, p) => c.Resolve<ServiceInstanceGenerator().GetActor<ITransactions>p.TypedAs<ActorId>))).As<ITransactions>();
}
}
public class AssignmentsActor
{
private readonly Func<ActorId, ITransactions> _actorFactory;
public AssignmentsActor(Func<ActorId, ITransactions> actorFactory)
{
_actorFactory = actorFactory;
}
private ITransactions GetActor(ActorId id)
{
return _actorFactory(id);
}
We tought that by registering the dependencies as single istance and the factory we could prevent the DynamicProxyGenLeaking but with this setup we can find this in the dumps: