How can I have 2 triggers in an Azure Function - .NET 8

David Thielen 3,121 Reputation points
2024-01-29T22:56:50.7866667+00:00

Hi all; Having 2 Run methods does not work. The following code:

public class Functions
{
    private readonly ILogger<Functions> _logger;

    public Functions(ILogger<Functions> logger)
    {
        _logger = logger;
    }

    [Function("RunNow")]
    public IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req)
    {

        string? message = req.Query["message"];

        if (_logger.IsEnabled(LogLevel.Trace))
            _logger.LogTrace($"RunNow?message={message} called");

        return new OkObjectResult("RunNow successfully completed.");
    }

    [Function("MidnightRun")]
    public void Run([TimerTrigger("0 0 9 * * *")] TimerInfo myTimer)
    {
        if (_logger.IsEnabled(LogLevel.Debug))
            _logger.LogDebug($"MidnightRun executed at: {DateTime.Now}");

        try
        {
            // Worker.ProcessAllPendingItems(null);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, $"MidnightRun threw error {ex.Message}");
            throw;
        }
    }
}

Throws the following exceptions:

[2024-01-29T21:57:29.759Z] Worker failed to load function: 'RunNow' with functionId: '3842070629'.
[2024-01-29T21:57:29.759Z] Worker failed to load function: 'MidnightRun' with functionId: '2487480743'.
[2024-01-29T21:57:29.760Z] Result: Failure
Exception: System.Reflection.AmbiguousMatchException: Ambiguous match found for 'LouisHowe.function.Functions Microsoft.AspNetCore.Mvc.IActionResult Run(Microsoft.AspNetCore.Http.HttpRequest)'.
[2024-01-29T21:57:29.760Z] Result: Failure
Exception: System.Reflection.AmbiguousMatchException: Ambiguous match found for 'LouisHowe.function.Functions Microsoft.AspNetCore.Mvc.IActionResult Run(Microsoft.AspNetCore.Http.HttpRequest)'.
[2024-01-29T21:57:29.761Z]    at System.RuntimeType.GetMethodImplCommon(String name, Int32 genericParameterCount, BindingFlags bindingAttr, Binder binder, CallingConventions callConv, Type[] types, ParameterModifier[] modifiers)
[2024-01-29T21:57:29.761Z]    at System.RuntimeType.GetMethodImplCommon(String name, Int32 genericParameterCount, BindingFlags bindingAttr, Binder binder, CallingConventions callConv, Type[] types, ParameterModifier[] modifiers)
[2024-01-29T21:57:29.762Z]    at System.RuntimeType.GetMethodImpl(String name, BindingFlags bindingAttr, Binder binder, CallingConventions callConv, Type[] types, ParameterModifier[] modifiers)
[2024-01-29T21:57:29.762Z]    at System.RuntimeType.GetMethodImpl(String name, BindingFlags bindingAttr, Binder binder, CallingConventions callConv, Type[] types, ParameterModifier[] modifiers)
[2024-01-29T21:57:29.763Z]    at System.Type.GetMethod(String name, BindingFlags bindingAttr)
[2024-01-29T21:57:29.763Z]    at System.Type.GetMethod(String name, BindingFlags bindingAttr)
[2024-01-29T21:57:29.764Z]    at Microsoft.Azure.Functions.Worker.Invocation.DefaultMethodInfoLocator.GetMethod(String pathToAssembly, String entryPoint) in D:\a\_work\1\s\src\DotNetWorker.Core\Invocation\DefaultMethodInfoLocator.cs:line 32
[2024-01-29T21:57:29.764Z]    at Microsoft.Azure.Functions.Worker.Invocation.DefaultMethodInfoLocator.GetMethod(String pathToAssembly, String entryPoint) in D:\a\_work\1\s\src\DotNetWorker.Core\Invocation\DefaultMethodInfoLocator.cs:line 32
[2024-01-29T21:57:29.765Z]    at Microsoft.Azure.Functions.Worker.Definition.GrpcFunctionDefinition..ctor(FunctionLoadRequest loadRequest, IMethodInfoLocator methodInfoLocator) in D:\a\_work\1\s\src\DotNetWorker.Grpc\Definition\GrpcFunctionDefinition.cs:line 56
[2024-01-29T21:57:29.765Z]    at Microsoft.Azure.Functions.Worker.Definition.GrpcFunctionDefinition..ctor(FunctionLoadRequest loadRequest, IMethodInfoLocator methodInfoLocator) in D:\a\_work\1\s\src\DotNetWorker.Grpc\Definition\GrpcFunctionDefinition.cs:line 56
[2024-01-29T21:57:29.766Z]    at Microsoft.Azure.Functions.Worker.Rpc.RpcExtensions.ToFunctionDefinition(FunctionLoadRequest loadRequest, IMethodInfoLocator methodInfoLocator) in D:\a\_work\1\s\src\DotNetWorker.Grpc\RpcExtensions.cs:line 118
[2024-01-29T21:57:29.766Z]    at Microsoft.Azure.Functions.Worker.Rpc.RpcExtensions.ToFunctionDefinition(FunctionLoadRequest loadRequest, IMethodInfoLocator methodInfoLocator) in D:\a\_work\1\s\src\DotNetWorker.Grpc\RpcExtensions.cs:line 118
[2024-01-29T21:57:29.767Z]    at Microsoft.Azure.Functions.Worker.GrpcWorker.FunctionLoadRequestHandler(FunctionLoadRequest request, IFunctionsApplication application, IMethodInfoLocator methodInfoLocator) in D:\a\_work\1\s\src\DotNetWorker.Grpc\GrpcWorker.cs:line 268
Stack:    at System.RuntimeType.GetMethodImplCommon(String name, Int32 genericParameterCount, BindingFlags bindingAttr, Binder binder, CallingConventions callConv, Type[] types, ParameterModifier[] modifiers)
[2024-01-29T21:57:29.767Z]    at Microsoft.Azure.Functions.Worker.GrpcWorker.FunctionLoadRequestHandler(FunctionLoadRequest request, IFunctionsApplication application, IMethodInfoLocator methodInfoLocator) in D:\a\_work\1\s\src\DotNetWorker.Grpc\GrpcWorker.cs:line 268
Stack:    at System.RuntimeType.GetMethodImplCommon(String name, Int32 genericParameterCount, BindingFlags bindingAttr, Binder binder, CallingConventions callConv, Type[] types, ParameterModifier[] modifiers)
[2024-01-29T21:57:29.768Z]    at System.RuntimeType.GetMethodImpl(String name, BindingFlags bindingAttr, Binder binder, CallingConventions callConv, Type[] types, ParameterModifier[] modifiers)
[2024-01-29T21:57:29.769Z]    at System.RuntimeType.GetMethodImpl(String name, BindingFlags bindingAttr, Binder binder, CallingConventions callConv, Type[] types, ParameterModifier[] modifiers)
[2024-01-29T21:57:29.770Z]    at System.Type.GetMethod(String name, BindingFlags bindingAttr)
[2024-01-29T21:57:29.770Z]    at System.Type.GetMethod(String name, BindingFlags bindingAttr)
[2024-01-29T21:57:29.771Z]    at Microsoft.Azure.Functions.Worker.Invocation.DefaultMethodInfoLocator.GetMethod(String pathToAssembly, String entryPoint) in D:\a\_work\1\s\src\DotNetWorker.Core\Invocation\DefaultMethodInfoLocator.cs:line 32
[2024-01-29T21:57:29.771Z]    at Microsoft.Azure.Functions.Worker.Invocation.DefaultMethodInfoLocator.GetMethod(String pathToAssembly, String entryPoint) in D:\a\_work\1\s\src\DotNetWorker.Core\Invocation\DefaultMethodInfoLocator.cs:line 32
[2024-01-29T21:57:29.772Z]    at Microsoft.Azure.Functions.Worker.Definition.GrpcFunctionDefinition..ctor(FunctionLoadRequest loadRequest, IMethodInfoLocator methodInfoLocator) in D:\a\_work\1\s\src\DotNetWorker.Grpc\Definition\GrpcFunctionDefinition.cs:line 56
[2024-01-29T21:57:29.772Z]    at Microsoft.Azure.Functions.Worker.Definition.GrpcFunctionDefinition..ctor(FunctionLoadRequest loadRequest, IMethodInfoLocator methodInfoLocator) in D:\a\_work\1\s\src\DotNetWorker.Grpc\Definition\GrpcFunctionDefinition.cs:line 56
[2024-01-29T21:57:29.773Z]    at Microsoft.Azure.Functions.Worker.Rpc.RpcExtensions.ToFunctionDefinition(FunctionLoadRequest loadRequest, IMethodInfoLocator methodInfoLocator) in D:\a\_work\1\s\src\DotNetWorker.Grpc\RpcExtensions.cs:line 118
[2024-01-29T21:57:29.773Z]    at Microsoft.Azure.Functions.Worker.Rpc.RpcExtensions.ToFunctionDefinition(FunctionLoadRequest loadRequest, IMethodInfoLocator methodInfoLocator) in D:\a\_work\1\s\src\DotNetWorker.Grpc\RpcExtensions.cs:line 118
[2024-01-29T21:57:29.774Z]    at Microsoft.Azure.Functions.Worker.GrpcWorker.FunctionLoadRequestHandler(FunctionLoadRequest request, IFunctionsApplication application, IMethodInfoLocator methodInfoLocator) in D:\a\_work\1\s\src\DotNetWorker.Grpc\GrpcWorker.cs:line 268.
[2024-01-29T21:57:29.774Z]    at Microsoft.Azure.Functions.Worker.GrpcWorker.FunctionLoadRequestHandler(FunctionLoadRequest request, IFunctionsApplication application, IMethodInfoLocator methodInfoLocator) in D:\a\_work\1\s\src\DotNetWorker.Grpc\GrpcWorker.cs:line 268.

So 2 Run methods are not allowed. So what can I do to have both a timer and http get trigger? thanks - dave

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,029 questions
{count} votes

Accepted answer
  1. MuthuKumaranMurugaachari-MSFT 22,336 Reputation points
    2024-01-30T15:21:26.6366667+00:00

    David Thielen Thanks for posting your question in Microsoft Q&A. I see you have defined two functions with function names RunNow and MidnightRun however both are defined with the same method name Run that caused this issue (even though with different method signature). You need to define different method names such as Method1 and Method2 (no need for Run specifically) and it should work fine. Check out doc: Methods recognized as functions which talks about this and I will pass feedback to our doc/PG team for adding a note about this ambiguous match exception. User's image Also, there is an open item: Catch ambiguous match exceptions at build to catch this during the build which would be helpful to avoid the runtime errors.

    I hope this helps and let me know if any questions.


    If you found the answer to your question helpful, please take a moment to mark it as Yes for others to benefit from your experience. Or simply add a comment tagging me and would be happy to answer your questions.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.