Skip to content

Implement IAsyncInterceptor interface to intercept invocations, invocation.Proceed() re enter InterceptAsynchronous if we perform async operations with awai #232

@ng-le

Description

@ng-le

Implement Option 1 like this

public void InterceptAsynchronous<TResult>(IInvocation invocation)
{
    invocation.ReturnValue = InternalInterceptAsynchronous<TResult>(invocation);
}

private async Task<TResult> InternalInterceptAsynchronous<TResult>(IInvocation invocation)
{
    // Step 1. Do something prior to invocation.

    invocation.Proceed();
    var task = (Task<TResult>)invocation.ReturnValue;
    TResult result = await task;

    // Step 2. Do something after invocation.

    return result;
}

If in Step 1 and Step, if we use await with asynchronous operations, then when invocation.Proceed() it will re enter the InterceptAsynchronous

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions