unitycontainer / interception

Unity.Interception package
Apache License 2.0
22 stars 17 forks source link

Error 'The type is not interceptable' for overwritten registrations #31

Open ArnaudB88 opened 5 years ago

ArnaudB88 commented 5 years ago

Hi,

The following error occurs when resolving the interface IADUtilities:

The type X.Business.Utilities.ADUtilitiesStubbed is not interceptable. Parameter name: interceptedType


Exception occurred while:

·resolving type: 'ADUtilitiesStubbed' •resolving type: 'IADUtilities' mapped to 'ADUtilitiesStubbed'

My setup:

public interface IAdUtilities {}
public class AdUtilities : IAdUtilities {}
public class AdUtilitiesStubbed : IAdUtilities {}

The registrations are build like: 1. Registration by convention:

var assemblies = System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, AssemblyConstants.AssemblyNamePrefix + "*.dll")
    .Select(f => System.Reflection.Assembly.LoadFile(f));

container.RegisterTypes(
    AllClasses.FromAssemblies(assemblies).Where(t => t.Namespace.StartsWith(AssemblyConstants.AssemblyNamePrefix, StringComparison.OrdinalIgnoreCase)),
    WithMappings.FromMatchingInterface,
    WithName.Default,
    t => lifetimeManager());

Result of this:

2. Overwrite convention registrations by more advanced registration

container.RegisterType<IADUtilities, ADUtilities>(
    createLifetimeManager(),
    new Interceptor<InterfaceInterceptor>(),
    new InterceptionBehavior<LogBehavior>(),
    new InterceptionBehavior(new ImpersonationBehavior("mydomain", "myUsername", "myPassword", container.Resolve<Interfaces.ILogger>()))
);

Result of this:

The type X.Business.Utilities.ADUtilitiesStubbed is not interceptable. Parameter name: interceptedType


Exception occurred while:

·resolving type: 'ADUtilitiesStubbed'

This is probably caused by the fact that interception is registered on the FromType (here IAdUtilities).

3. For unit testing, the standard registrations should be overwritten by stubbed classes container.RegisterType<Business.Interfaces.Utilities.IADUtilities, Business.Utilities.ADUtilitiesStubbed>(createLifetimeManager());

Result of this:

The type X.Business.Utilities.ADUtilitiesStubbed is not interceptable. Parameter name: interceptedType


Exception occurred while:

·resolving type: 'ADUtilitiesStubbed' •resolving type: 'IADUtilities' mapped to 'ADUtilitiesStubbed'

It seems like the resolve happens in 2 steps:

Why does resolving the Stubbed class fail? This is not as expected (in old unity versions this works correct). Can this be fixed please?

fyi: When reregistereing AdUtilitiesStubbed as last, resolve succeeds: container.RegisterType<Business.Utilities.ADUtilitiesStubbed, Business.Utilities.ADUtilitiesStubbed>(createLifetimeManager());