Interface IInterfaceInterceptor

Describes the contract how to implement an interface interceptor.

Namespace: Solti.Utils.DI.Interfaces
Assembly: Solti.Utils.DI.Interfaces.dll
Syntax
public interface IInterfaceInterceptor
Remarks

Interceptors are used to hook into method invocations e.g. for logging:

public class LoggerInterceptor : IInterfaceInterceptor
{
    public LoggerInterceptor(IDependency dependency) {...}

    public object Invoke(IInvocationContext context, Next<IInvocationContext, object?> callNext)
    {
        Console.WriteLine(context.InterfaceMethod);
        return callNext(context);
    }
}
...
ScopeFactory.Create
(
    svcs => svcs
        .Service<ISemeInterface, SomeImplementation>()
        .Decorate<LoggerInterceptor>(),
    ...
)

Methods

Invoke(IInvocationContext, CallNextDelegate<IInvocationContext, Object>)

Contains the interception logic.

Declaration
object Invoke(IInvocationContext context, CallNextDelegate<IInvocationContext, object> callNext)
Parameters
Type Name Description
IInvocationContext context

Context related to the actual method call.

CallNextDelegate<IInvocationContext, System.Object> callNext

Calls the next interceptor in the invocation chain.

Returns
Type Description
System.Object

Value to be passed to the caller (or NULL in case of void methods).

Remarks

Interceptors may modify the input and output parameters or alter the result itself:

// Invoking the generated proxy instance will trigger this method
object IInterfaceInterceptor.Invoke(IInvocationContext context, Next<IInvocationContext, object?> callNext)
{
    if (suppressOriginalMethod)
    {
        return something;
        // ref|out parameters can be assigned by setting the corresponding "context.Args[]" item 
    }
    context.Args[0] = someNewVal; // "someNewVal" will be forwarded to the original method 
    return callNext(context); // Let the original method do its work
}
In This Article
Back to top Generated by DocFX