I have a method:
public virtual async Task<IActionResult> GetEmployees([HttpTrigger(AuthorizationLevel.Admin, "get", Route = null)] HttpRequest req) {
return OkObjectResult(null);
}
I know I can intercept this synchronously using autofac:
public class CallLogger : IInterceptor
{
TextWriter _output;
public CallLogger(TextWriter output)
{
_output = output;
}
public void Intercept(IInvocation invocation)
{
_output.Write("Calling method {0} with parameters {1}... ",
invocation.Method.Name,
string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray()));
invocation.Proceed();
_output.WriteLine("Done: result was {0}.", invocation.ReturnValue);
}
}
But how can I do this asynchronously while potentially overwriting the result the method I'm intercepting returns? It currently returns an OkObjectResult
, I may want to return a 404 instead, for example.
Psuedo Code
public async Task Intercept(IInvocation invocation)
{
var myAsyncResult = await _myAsyncClass.MyAsyncMethod();
if (myAsyncResult == expected)
{
invocation.Proceed();
}
else
{
invocation.ReturnValue = // some overwrite of the value - and don't proceed with the invocation.
}
}
Note
I know there are clever approaches to async in autofac, but this doesn't allow me to prevent execution of the original method and overwrite the value, instead I need to 'proceed' the invocation and use it's return: https://stackoverflow.com/a/39784559/12683473