ContainerOptionsRegisterResolveInterceptor Method

Simple Injector

Namespace:  SimpleInjector
Assembly:  SimpleInjector (in SimpleInjector.dll) Version: 5.3.0
Syntax

public void RegisterResolveInterceptor(
	ResolveInterceptor interceptor,
	Predicate<InitializationContext> predicate
)

Parameters

interceptor
Type: SimpleInjectorResolveInterceptor
The ResolveInterceptor delegate to register.
predicate
Type: SystemPredicateInitializationContext
The predicate that will be used to check whether the given delegate must be applied to a registration or not. The given predicate will be called once for each registration in the container.
Exceptions

ExceptionCondition
ArgumentNullException Thrown when either the interceptor or predicate are null references.
InvalidOperationException Thrown when this container instance is locked and can not be altered.
Remarks

If multiple registered ResolveInterceptor instances must be applied, they will be applied/wrapped in the order of registration, i.e. the first registered interceptor will call the original instance producer delegate, the second interceptor will call the first interceptor, etc. The last registered interceptor will become the outermost method in the chain and will be called first.
Examples

The following example shows the usage of the RegisterResolveInterceptor(ResolveInterceptor, PredicateInitializationContext) method:
C#
var container = new Container();

container.Options.RegisterResolveInterceptor((context, producer) =>
    {
        object instance = producer.Invoke();
        Console.WriteLine(instance.GetType().Name + " resolved for " + context.Producer.ServiceType.Name);
        return instance;
    },
    context => context.Producer.ServiceType.Name.EndsWith("Controller"));

container.Register<IHomeViewModel, HomeViewModel>();
container.Register<IUserRepository, SqlUserRepository>();

// This line will write "HomeViewModel resolved for IHomeViewModel" to the console.
container.GetInstance<IHomeViewModel>();
See Also

Reference