'Azure Function App IFunctionInvocationFilter loses Request body after Request executes

I have a filter on an Azure Function that performs some tasks right after the the main function action has completed.

    public async Task<IActionResult> DoStuff(
                [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "notImportant")]
                HttpRequest req,
                ClaimsPrincipal principal)
            {
              string bodyContent = await new StreamReader(req.Body).ReadToEndAsync();  //Always has content
            }

My Filter looks like this:

public class TraceAuditFilter : IFunctionInvocationFilter
    {
       

        public TraceAuditFilter()
        {
            
        }


        public async Task OnExecutedAsync(FunctionExecutedContext executedContext, CancellationToken cancellationToken)
        {
          var request = (HttpRequest)executedContext.Arguments["req"];
          string body = new StreamReader(req.Body).ReadToEnd(); //Always Empty
        }
}

My filter sucessfully fires, but the 'body' in the filter method is always Empty. Any ideas what is resetting it?



Solution 1:[1]

We need to add the function filter class as an attribute.

That might be a reason as the filter is not being invoked in the functions.

You need to add [TraceAuditFilter] to the function so that the filter can be passed.

Refer the following articles for Indepth explanation: - IFunctionInvocationFilter

function filters

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MohitGanorkar-MT