1

I have a default Azure function, and some middleware.

What I want is when xml is sent, the middleware parses it into an object, and passes this object to the azure function. The code would look like this:

public class MyMiddleware : IFunctionsWorkerMiddleware
{
    public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
    {
        // Get the posted data from the http context
        var theData = context.GetTheIncomingXMLData();
        // Create a new object
        var myComplexObject = ParseXMLToObject(theData);
        // Somehow pass this to the function? 
        context.Items.Add("someObject", myComplexObject); // Don't think this is right

        await next(context);
    }
}

Then my function would be:

[Function("Function1")]
        public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
            ComplexObject someObject) // Automatically populated from the middleware
        { ...
}

I've done this kind of thing using value providers in MVC but wondering if you can do it with Azure Functions?

NibblyPig
  • 51,118
  • 72
  • 200
  • 356
  • Sure, looks like it … https://www.re-mark-able.net/blogs/2021/05/09/azure-functions-middleware.html#:~:text=Middleware%20is%20a%20piece%20of,until%20it%20reaches%20your%20function. – Skin Jan 27 '23 at 19:56
  • 1
    Yep you can add middleware this way (this is what I am doing in my first example block) but I can't see how you can alter the context to provide a value for the function – NibblyPig Jan 30 '23 at 09:00

0 Answers0