I need 3 different type triggered azure function in a single project

Akash Madhu 0 Reputation points
2023-07-06T14:00:25.04+00:00

I need to create a project with three different types of Azure Functions triggers: HTTP, Timer, and Queue. I want to have all these functions in a single C# file. I have attempted to implement this, but I'm encountering errors. When I select the HTTP trigger, the other two triggers give me errors. The reason I'm trying to do this is that I only have one Function App resource in my Azure resource group, and I need to implement all three functions within that resource. I have attached the code for reference. Could you please help me resolve the issue?

Here's the code you provided:

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

public static class MyFunction
{
    [FunctionName("MyHttpTrigger")]
    public static async Task<HttpResponseMessage> HttpTrigger(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req,
        ILogger log)
    {
        log.LogInformation("HTTP trigger function processed a request.");

        // Process your logic here

        return req.CreateResponse(HttpStatusCode.OK, "HTTP trigger function executed successfully");
    }

    [FunctionName("MyTimerTrigger")]
    public static async Task TimerTrigger(
        [TimerTrigger("0 */5 * * * *")] TimerInfo myTimer,
        ILogger log)
    {
        log.LogInformation($"Timer trigger function executed at: {DateTime.Now}");

        // Process your logic here
    }

    [FunctionName("MyQueueTrigger")]
    public static void QueueTrigger(
        [QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem,
        ILogger log)
    {
        log.LogInformation($"Queue trigger function processed message: {myQueueItem}");

        // Process your logic here
    }
}

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

3 answers

Sort by: Most helpful
  1. Sander van de Velde 28,546 Reputation points MVP
    2023-07-06T17:31:32.73+00:00

    Hello @Akash Madhu,

    are you sure you need to add all three functions in one file, although you are using a Visual Studio project?

    We just create separate files inside our Azure Function projects and Azure is perfectly capable to identify all functions while putting them in the same web app service.

    Please check again what happens if you split them up into separate files.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.

    0 comments No comments

  2. MuthuKumaranMurugaachari-MSFT 22,146 Reputation points
    2023-07-10T15:31:25.47+00:00

    Akash Madhu Thanks for posting your question in Microsoft Q&A. Based on the code snippet, I assume you are looking to create In-process Functions (.NET 6) project with multiple triggers in the same project file.

    I have modified the code snippet as below and added NuGet package Microsoft.Azure.WebJobs.Extensions.Storage to the project and able to run in Visual Studio.

    [FunctionName("SampleHttp")]
            public static async Task<IActionResult> Run(
                [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
                ILogger log)
            {
                log.LogInformation("C# HTTP trigger function processed a request.");
    
                string name = req.Query["name"];
    
                string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
                dynamic data = JsonConvert.DeserializeObject(requestBody);
                name = name ?? data?.name;
    
                string responseMessage = string.IsNullOrEmpty(name)
                    ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                    : $"Hello, {name}. This HTTP triggered function executed successfully.";
    
                return new OkObjectResult(responseMessage);
            }
    
            [FunctionName("SampleTimer")]
            public static void SampleTimer(
            [TimerTrigger("0 */5 * * * *")] TimerInfo myTimer,
            ILogger log)
            {
                log.LogInformation($"Timer trigger function executed at: {DateTime.Now}");
            }
    
            [FunctionName("SampleQueue")]
            public static void SampleQueue(
            [QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem,
            ILogger log)
            {
                log.LogInformation($"Queue trigger function processed message: {myQueueItem}");
            }
    

    Output:

    User's image

    Can you follow the steps and the same code snippet and test it in your local development environment? If you face any issues in deploying or after the deployment to azure, provide the details such as full exception/error message including stack trace, deployment info (steps followed to deploy) etc. That would help in understanding the issue better.

    Also, as suggested by Sander van de Velde, it is generally recommended to create multiple C# files in the same project and with this way, you can create a single Function app with multiple functions such as SampleHttp, SampleTimer, SampleQueue etc. in the resource group. Check out a sample reference and similar examples in the repo such as Azure serverless community library.

    I hope this helps and let us know if you have any questions. Would be happy to answer any.

    0 comments No comments

  3. Zoran Gladoic 0 Reputation points
    2024-04-18T12:05:44.4633333+00:00

    MuthuKumaranMurugaachari-MSFT Can this be done with the isolated functions too?

    0 comments No comments