I was putting together a .NET 6 Isolated Azure Functions project with a number of Http Trigger end point functions and after each function I built it and ran it. After the last function I added it built fine but failed to run saying "System.Reflection.AmbiguousMatchException: Ambiguous match found."
It build so I knew it must be syntactically fine from a C# language perspective. I'd updated the [Function] name correctly and I'd changed the route accordingly with the appropriate HttpMethod verb.
So what was wrong?
So after commenting out the last couple of functions and adding them back into the code base 1 by 1 I noticed that it errored after adding in the last one. Makes sense. So what's different with this one? Looking at the function method which makes up the Azure Function call it was named the same as the previous function. However as the method signature was different it compiled fine.
[2021-08-12T12:13:36.478Z] Worker failed to function id d088a925-f3d2-4050-841d-ada7757fb5dd. [2021-08-12T12:13:36.480Z] Result: Failure Exception: System.Reflection.AmbiguousMatchException: Ambiguous match found. [2021-08-12T12:13:36.481Z] at System.RuntimeType.GetMethodImplCommon(String name, Int32 genericParameterCount, BindingFlags bindingAttr, Binder binder, CallingConventions callConv, Type types, ParameterModifier modifiers) in System.Private.CoreLib.dll:token 0x6000695+0x5c
Looking at the error message again there is obviously some reflection going on to link the marked function methods and how they are called and having a method named the same in the same class but decorated differently caused the problem.
In this post we have seen how even though the code is C# syntactically correct and the project compiles that an Azure Function project isn't bullet proof against runtime errors due to the way the worker process discovers and executes functions. Just something to remember in the future as I work more with Azure Functions.
Have you come across any oddities working with Azure Functions? Let me know on Twitter @WestDiscGolf