0

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?

Adrian
  • 670
  • 10
  • 24
  • 0 I found a similar problem and solution here: How to read request body in an asp.net core webapi controller? Using request.EnableBuffering() before I read the body and req.Body.Position = 0 to reset it to be read elsewhere. – Adrian Apr 25 '22 at 14:26

1 Answers1

0

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

Mohit Ganorkar
  • 1,917
  • 2
  • 6
  • 11