From 17de27756811d12ebd91c6ea0aaebab6c4cfdad1 Mon Sep 17 00:00:00 2001 From: Orad SA Date: Mon, 21 Jun 2021 17:44:40 +0300 Subject: [PATCH] fix scoped lifetime in web api when registering a class with AddScopedWithInterception, the target class (inside the castle proxy) is not being re-created in each call but remain the same instance --- src/Foil/ServiceCollectionExtensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Foil/ServiceCollectionExtensions.cs b/src/Foil/ServiceCollectionExtensions.cs index 2169ebb..93b5d21 100644 --- a/src/Foil/ServiceCollectionExtensions.cs +++ b/src/Foil/ServiceCollectionExtensions.cs @@ -103,7 +103,7 @@ private static IServiceCollection Add(this IServiceCo interceptionOptions.Interceptors.ForEach(services.TryAddTransient); services.TryAdd(descriptorFactory(lifetime)); - services.AddTransient(sp => + services.Add(ServiceDescriptor.Describe(typeof(TService), sp => { var interceptorInstances = interceptionOptions.Interceptors .Select(sp.GetRequiredService) @@ -118,7 +118,7 @@ private static IServiceCollection Add(this IServiceCo return proxyFactory.CreateInterfaceProxyWithTarget(implementation, proxyGenerationOptions, interceptorInstances); - }); + }, lifetime)); return services; }