Greg Smalter
Greg Smalter

Reputation: 6699

Why do method overloads break Azure Function routing?

Let's say I have a C# method in an Azure Function project that looks like this, and works perfectly:

[FunctionName( nameof( GetContents) )]
public static async Task<IActionResult> GetContents( [HttpTrigger( AuthorizationLevel.Function, "get", Route="/v1/Contents/{id}" )] HttpRequest req, ILogger log, string id ) {
// ...
}

So far so good. Now, create an overload that has no route at all, is not a web service at all, and is only intended to be used as a helper for other services:

public static async Task<ObjectResult> GetContents( ILogger log, string id, bool isAdmin ) {
// ...
}

Now, the original web service method no longer routes properly. You get a 404 if you try to hit it. This, despite it already working perfectly and nothing changing about it. How does the overload manage to destroy the functionality of the original method?

Upvotes: 1

Views: 482

Answers (1)

David Garza
David Garza

Reputation: 173

The Azure Function host attempts to resolve the method using a cascading approach, starting with an explicitly defined method named as the entryPoint inside the compiled function.json. See FunctionEntryPointResolver.cs#L39

If the entry point method name could return more than 1 public method, the Azure Function resolver will throw an exception due to ambiguity. See FunctionEntryPointResolver#L94

private static T GetNamedMethod<T>(IEnumerable<T> methods, string methodName, StringComparison stringComparison) where T : IMethodReference
{
    var namedMethods = methods
                .Where(m => m.IsPublic && string.Compare(m.Name, methodName, stringComparison) == 0)
                .ToList();

    // If we have single method that matches the provided name, use it.
    if (namedMethods.Count == 1)
    {
        return namedMethods[0];
    }

    // If we have multiple public methods matching the provided name, throw a compilation exception
    if (namedMethods.Count > 1)
    {
        throw CreateCompilationException(DotNetConstants.AmbiguousFunctionEntryPointsCompilationCode,
            $"Ambiguous function entry points. Multiple methods named '{methodName}'.", $"Multiple methods named '{methodName}'. Consider renaming methods.");
    }
}

Upvotes: 3

Related Questions