Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using FluentAssertions;
using Polly.Contrib.WaitAndRetry;
using Wemogy.Core.DynamicProxies;
using Wemogy.Core.DynamicProxies.Extensions;
using Wemogy.Core.Errors.Exceptions;
using Wemogy.Core.Tests.DynamicProxies.TestData;
using Xunit;

namespace Wemogy.Core.Tests.DynamicProxies;

public class AsyncInterceptorExtensionsTests
{
[Fact]
public void Wrap_ShouldReuseTheProxyTypeAcrossCalls()
{
// Arrange
var retryProxy = new RetryProxy<PreconditionFailedErrorException>(
Backoff.ExponentialBackoff(TimeSpan.FromMilliseconds(10), 1));

// Act
var first = retryProxy.Wrap<IFlakyService>(new FlakyService(0, () => new Exception()));
var second = retryProxy.Wrap<IFlakyService>(new FlakyService(0, () => new Exception()));
var untyped = retryProxy.Wrap(typeof(IFlakyService), new FlakyService(0, () => new Exception()));

// Assert
// A new ProxyGenerator per call emits a new dynamic assembly with a new proxy type every time.
// Those types can never be unloaded, so a wrapper created per request (e.g. a scoped
// repository) grows the process until it runs out of memory.
second.GetType().Should().BeSameAs(first.GetType());
untyped.GetType().Should().BeSameAs(first.GetType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ namespace Wemogy.Core.DynamicProxies.Extensions
{
public static class AsyncInterceptorExtensions
{
/// <summary>
/// Shared on purpose. Every ProxyGenerator owns its own module scope, so a new instance per call
/// emits a new dynamic assembly with a new proxy type each time — types that can never be
/// unloaded. Wrapping per request (a scoped repository, for example) then leaks memory until
/// the process is killed. One generator caches the proxy type per interface and is thread-safe.
/// </summary>
private static readonly ProxyGenerator ProxyGenerator = new ProxyGenerator();

public static TInterface Wrap<TInterface>(
this IAsyncInterceptor proxy,
object target)
where TInterface : class
{
var proxyGenerator = new ProxyGenerator();

var wrappedImplementation = (TInterface)proxyGenerator.CreateInterfaceProxyWithTarget(
var wrappedImplementation = (TInterface)ProxyGenerator.CreateInterfaceProxyWithTarget(
typeof(TInterface),
target,
proxy.ToInterceptor());
Expand All @@ -25,9 +31,7 @@ public static object Wrap(
Type interfaceType,
object target)
{
var proxyGenerator = new ProxyGenerator();

var wrappedImplementation = proxyGenerator.CreateInterfaceProxyWithTarget(
var wrappedImplementation = ProxyGenerator.CreateInterfaceProxyWithTarget(
interfaceType,
target,
proxy.ToInterceptor());
Expand Down
Loading