diff --git a/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpInterceptionRegistrationExtensions.cs b/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpInterceptionRegistrationExtensions.cs
index 52503f7abb..fcd7752fe0 100644
--- a/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpInterceptionRegistrationExtensions.cs
+++ b/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpInterceptionRegistrationExtensions.cs
@@ -166,7 +166,9 @@ public static class AbpInterceptionRegistrationExtensions
/// proxies a name of their own instead of one shared by every implementation of a common interface, which keeps them
/// spread over the proxy builder's modules. It is only preferred when neither interface has a public attribute other
/// than the compiler's nullable annotations, so the proxy type's attributes stay as they were whatever Castle is
- /// configured to copy.
+ /// configured to copy. It is not preferred either when the type implements a variant generic interface more than
+ /// once, since a call through a variant interface that matches several of them runs the first one in the proxy's
+ /// interface order.
///
private sealed class ProxiedInterfaceSet
{
@@ -185,7 +187,8 @@ public static class AbpInterceptionRegistrationExtensions
var primary = interfaces[0];
var own = interfaces.FirstOrDefault(i => i.Name == "I" + type.Name);
- if (own != null && own != primary && !HasPublicAttributes(primary) && !HasPublicAttributes(own))
+ if (own != null && own != primary && !HasPublicAttributes(primary) && !HasPublicAttributes(own) &&
+ !ImplementsAVariantInterfaceMoreThanOnce(interfaces))
{
primary = own;
}
@@ -209,6 +212,15 @@ public static class AbpInterceptionRegistrationExtensions
(attribute.AttributeType.IsPublic || attribute.AttributeType.IsNestedPublic) &&
!NullableAnnotations.Contains(attribute.AttributeType.FullName));
}
+
+ private static bool ImplementsAVariantInterfaceMoreThanOnce(Type[] interfaces)
+ {
+ return interfaces
+ .Where(i => i.IsGenericType && i.GetGenericTypeDefinition().GetGenericArguments()
+ .Any(argument => (argument.GenericParameterAttributes & GenericParameterAttributes.VarianceMask) != 0))
+ .GroupBy(i => i.GetGenericTypeDefinition())
+ .Any(group => group.Count() > 1);
+ }
}
private static void EnsureInterfaceInterceptionApplies(IComponentRegistration componentRegistration)
diff --git a/framework/src/Volo.Abp.Autofac/Volo/Abp/Autofac/AutofacServiceImplementationTypeProvider.cs b/framework/src/Volo.Abp.Autofac/Volo/Abp/Autofac/AutofacServiceImplementationTypeProvider.cs
deleted file mode 100644
index d0511a34f3..0000000000
--- a/framework/src/Volo.Abp.Autofac/Volo/Abp/Autofac/AutofacServiceImplementationTypeProvider.cs
+++ /dev/null
@@ -1,45 +0,0 @@
-using System;
-using System.Linq;
-using Autofac.Core;
-using Autofac.Core.Activators.Reflection;
-using Autofac.Extensions.DependencyInjection;
-using Volo.Abp.DependencyInjection;
-
-namespace Volo.Abp.Autofac;
-
-public class AutofacServiceImplementationTypeProvider : IServiceImplementationTypeProvider, ITransientDependency
-{
- protected IServiceProvider ServiceProvider { get; }
-
- public AutofacServiceImplementationTypeProvider(IServiceProvider serviceProvider)
- {
- ServiceProvider = serviceProvider;
- }
-
- public virtual Type? GetImplementationTypeOrNull(Type serviceType)
- {
- if (ServiceProvider is not AutofacServiceProvider autofacServiceProvider)
- {
- return null;
- }
-
- var registry = autofacServiceProvider.LifetimeScope.ComponentRegistry;
- var service = new TypedService(serviceType);
- if (!registry.TryGetServiceRegistration(service, out var serviceRegistration))
- {
- return null;
- }
-
- // A decorator or other service middleware may return another object than the registration creates.
- if (registry.ServiceMiddlewareFor(service).Any())
- {
- return null;
- }
-
- // The reflection activator always creates its type (a class proxy, when the service is intercepted);
- // a delegate may create any type.
- return serviceRegistration.Registration.Activator is ReflectionActivator activator
- ? activator.LimitType
- : null;
- }
-}
diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/IServiceImplementationTypeProvider.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/IServiceImplementationTypeProvider.cs
deleted file mode 100644
index 183ec7d15f..0000000000
--- a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/IServiceImplementationTypeProvider.cs
+++ /dev/null
@@ -1,13 +0,0 @@
-using System;
-
-namespace Volo.Abp.DependencyInjection;
-
-public interface IServiceImplementationTypeProvider
-{
- ///
- /// Gets the type of every instance the container creates for , without creating one.
- /// Returns null when the registration does not fix the type (such as a factory method or a decorator) or the
- /// container can not tell it.
- ///
- Type? GetImplementationTypeOrNull(Type serviceType);
-}
diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/NullServiceImplementationTypeProvider.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/NullServiceImplementationTypeProvider.cs
deleted file mode 100644
index 5a676c22d2..0000000000
--- a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/NullServiceImplementationTypeProvider.cs
+++ /dev/null
@@ -1,12 +0,0 @@
-using System;
-
-namespace Volo.Abp.DependencyInjection;
-
-[Dependency(TryRegister = true)]
-public class NullServiceImplementationTypeProvider : IServiceImplementationTypeProvider, ITransientDependency
-{
- public Type? GetImplementationTypeOrNull(Type serviceType)
- {
- return null;
- }
-}
diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IocEventHandlerFactory.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IocEventHandlerFactory.cs
index 7a51a2f16f..14f522cd32 100644
--- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IocEventHandlerFactory.cs
+++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IocEventHandlerFactory.cs
@@ -1,10 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
-using System.Runtime.CompilerServices;
-using System.Threading;
using Microsoft.Extensions.DependencyInjection;
-using Volo.Abp.DependencyInjection;
namespace Volo.Abp.EventBus;
@@ -18,8 +15,6 @@ public class IocEventHandlerFactory : IEventHandlerFactory, IDisposable
protected IServiceScopeFactory ScopeFactory { get; }
- private StrongBox? _implementationType;
-
public IocEventHandlerFactory(IServiceScopeFactory scopeFactory, Type handlerType)
{
ScopeFactory = scopeFactory;
@@ -36,14 +31,6 @@ public class IocEventHandlerFactory : IEventHandlerFactory, IDisposable
try
{
var handler = (IEventHandler)scope.ServiceProvider.GetRequiredService(HandlerType);
-
- var implementationType = Volatile.Read(ref _implementationType)?.Value;
- if (implementationType != null && implementationType != handler.GetType())
- {
- // The container created another type than its registration told, so it is not asked again.
- Volatile.Write(ref _implementationType, new StrongBox(null));
- }
-
return new EventHandlerDisposeWrapper(handler, () => scope.Dispose());
}
catch
@@ -61,33 +48,6 @@ public class IocEventHandlerFactory : IEventHandlerFactory, IDisposable
}
}
- ///
- /// Gets the type of the handlers this factory creates when the container can tell it without creating one,
- /// otherwise null. The container is asked once.
- ///
- internal Type? GetImplementationTypeOrNull()
- {
- var implementationType = Volatile.Read(ref _implementationType);
- if (implementationType == null)
- {
- // Never replaces what GetHandler wrote meanwhile.
- Interlocked.CompareExchange(ref _implementationType, new StrongBox(FindImplementationType()), null);
- implementationType = Volatile.Read(ref _implementationType)!;
- }
-
- return implementationType.Value;
- }
-
- private Type? FindImplementationType()
- {
- using (var scope = ScopeFactory.CreateScope())
- {
- return scope.ServiceProvider
- .GetService()?
- .GetImplementationTypeOrNull(HandlerType);
- }
- }
-
public bool IsInFactories(List handlerFactories)
{
return handlerFactories
diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs
index 9ac45bc546..32dfbc0ee0 100644
--- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs
+++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs
@@ -284,9 +284,9 @@ public class LocalEventBus : EventBusBase, ILocalEventBus, ISingletonDependency
///
/// Gets the order of the handler the factory creates.
- /// When the handler type is known without creating a handler (the built-in factories of a fixed type, and an
- /// IoC handler whose registration in the container fixes its type), the order is read from that type.
- /// Otherwise the handler is created to read its type and is disposed at once, on each publish. Callers
+ /// When the handler type is known without creating a handler (the built-in factories of a fixed type), the order
+ /// is read from that type. Otherwise the handler is created to read its type and is disposed at once, on each
+ /// publish, since what the container creates for a handler can change from one resolve to another. Callers
/// enumerate a snapshot of the factories, since that dispose runs handler code which may unsubscribe.
///
protected virtual int GetHandlerOrder(IEventHandlerFactory factory)
@@ -334,12 +334,6 @@ public class LocalEventBus : EventBusBase, ILocalEventBus, ISingletonDependency
return ((TransientEventHandlerFactory)factory).HandlerType;
}
- // Only the built-in IoC factory, for the same reason.
- if (factoryType == typeof(IocEventHandlerFactory))
- {
- return ((IocEventHandlerFactory)factory).GetImplementationTypeOrNull();
- }
-
return null;
}
diff --git a/framework/test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/AutofacServiceImplementationTypeProvider_Tests.cs b/framework/test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/AutofacServiceImplementationTypeProvider_Tests.cs
deleted file mode 100644
index 503dad0b91..0000000000
--- a/framework/test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/AutofacServiceImplementationTypeProvider_Tests.cs
+++ /dev/null
@@ -1,138 +0,0 @@
-using System.Threading.Tasks;
-using Autofac;
-using Microsoft.Extensions.DependencyInjection;
-using Shouldly;
-using Volo.Abp.DependencyInjection;
-using Volo.Abp.DynamicProxy;
-using Volo.Abp.Testing;
-using Xunit;
-
-namespace Volo.Abp.Autofac;
-
-public class AutofacServiceImplementationTypeProvider_Tests : AbpIntegratedTest
-{
- protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options)
- {
- options.UseAutofac();
- }
-
- protected override void AfterAddApplication(IServiceCollection services)
- {
- services.AddTransient();
- services.AddTransient();
- services.OnRegistered(context =>
- {
- if (context.ImplementationType == typeof(TypeProviderInterceptedService))
- {
- context.Interceptors.TryAdd();
- }
- });
-
- var builder = services.GetObject();
- builder.RegisterType();
- builder.RegisterType().As();
- builder.Register(_ => new TypeProviderService()).As();
- builder.RegisterType().As();
- builder.RegisterType().As();
- builder.RegisterDecorator();
- }
-
- [Fact]
- public void Should_Replace_The_Null_Provider()
- {
- GetRequiredService().ShouldBeOfType();
- }
-
- [Fact]
- public void Should_Tell_The_Type_A_Registration_Creates()
- {
- var provider = GetRequiredService();
-
- provider.GetImplementationTypeOrNull(typeof(TypeProviderService)).ShouldBe(typeof(TypeProviderService));
- provider.GetImplementationTypeOrNull(typeof(ITypeProviderService)).ShouldBe(typeof(TypeProviderService));
- provider.GetImplementationTypeOrNull(typeof(TypeProviderReplaced)).ShouldBe(typeof(TypeProviderReplacement));
- }
-
- [Fact]
- public void Should_Tell_The_Proxy_Type_Of_An_Intercepted_Class()
- {
- var type = GetRequiredService().GetImplementationTypeOrNull(typeof(TypeProviderInterceptedService));
-
- type.ShouldNotBeNull();
- type.ShouldNotBe(typeof(TypeProviderInterceptedService));
- type.ShouldBe(GetRequiredService().GetType());
- }
-
- [Fact]
- public void Should_Not_Tell_A_Type_It_Can_Not_Know()
- {
- var provider = GetRequiredService();
-
- provider.GetImplementationTypeOrNull(typeof(TypeProviderCreatedByMethod)).ShouldBeNull();
- provider.GetImplementationTypeOrNull(typeof(ITypeProviderDecoratedService)).ShouldBeNull();
- provider.GetImplementationTypeOrNull(typeof(TypeProviderNotRegistered)).ShouldBeNull();
- }
-
- [Fact]
- public void Should_Not_Tell_A_Type_For_Another_Container()
- {
- var services = new ServiceCollection();
- services.AddTransient();
- using var serviceProvider = services.BuildServiceProvider();
-
- new AutofacServiceImplementationTypeProvider(serviceProvider)
- .GetImplementationTypeOrNull(typeof(TypeProviderService))
- .ShouldBeNull();
- }
-
- public interface ITypeProviderService
- {
- }
-
- public interface ITypeProviderDecoratedService
- {
- }
-
- public class TypeProviderService : TypeProviderCreatedByMethod, ITypeProviderService, ITypeProviderDecoratedService
- {
- }
-
- public class TypeProviderCreatedByMethod
- {
- }
-
- public class TypeProviderReplaced
- {
- }
-
- public class TypeProviderReplacement : TypeProviderReplaced
- {
- }
-
- public class TypeProviderNotRegistered
- {
- }
-
- public class TypeProviderDecorator : ITypeProviderDecoratedService
- {
- public TypeProviderDecorator(ITypeProviderDecoratedService inner)
- {
- }
- }
-
- public class TypeProviderInterceptedService
- {
- public virtual Task DoItAsync()
- {
- return Task.CompletedTask;
- }
- }
-
- public class TypeProviderInterceptor : AbpInterceptor
- {
- public override Task InterceptAsync(IAbpMethodInvocation invocation)
- {
- return invocation.ProceedAsync();
- }
- }
-}
diff --git a/framework/test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/Interception/Autofac_Interface_Interception_Test.cs b/framework/test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/Interception/Autofac_Interface_Interception_Test.cs
index ec0b7987d9..dd2f40b128 100644
--- a/framework/test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/Interception/Autofac_Interface_Interception_Test.cs
+++ b/framework/test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/Interception/Autofac_Interface_Interception_Test.cs
@@ -24,9 +24,11 @@ public class Autofac_Interface_Interception_Test : AbpAsyncIntegratedTest();
services.AddTransient();
+ services.AddTransient();
services.OnRegistered(registration =>
{
- if (typeof(InterfaceInterceptionTarget) == registration.ImplementationType)
+ if (typeof(InterfaceInterceptionTarget) == registration.ImplementationType ||
+ typeof(VariantTarget) == registration.ImplementationType)
{
registration.Interceptors.Add();
}
@@ -68,6 +70,32 @@ public class Autofac_Interface_Interception_Test : AbpAsyncIntegratedTest();
+
+ ProxyUtil.IsProxy(target).ShouldBeTrue();
+ // IVariant