Home > database >  Run program.cs file of console application in Azure Function?
Run program.cs file of console application in Azure Function?

Time:11-30

I have a console application which I want to convert to an Azure Function Timer Trigger app which will run every hour after some data processing and uploads are done. The data processing and uploads are being done via classes which are injected in the program.cs file of the console application. Somewhere in the classes I have a task.delay by 1hour where it will query new data after the data has been queried and uploaded for the first time. So, I copied the entire code of the console application with its packages to the Azure Function Timer trigger app. What I am trying to do is to run the program.cs file of the console application first in the azure function app in order to do its job (data processing, querying data, uploading data to azure...). and then initiate the timer trigger. Is that doable ? What line of code can I add in the run method of the azure function app to execute the program.cs file first and then initiate the trigger. You can find here the startup code of the azure function time trigger app.

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

namespace ExportServiceFunctionApp
{
    public static class ExportServiceFunctionApp
    {
        [FunctionName("ExportServiceFunctionApp")]

        public static void Run([TimerTrigger("0 0 */1 * * * ")]TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
        }
    }
}

CodePudding user response:

Azure functions is event driven in nature. If function trigger means the event handled.

Run method of function means that function has triggered and its entry point for it.

If you want any processing or code execution before it you may need to write one more function and perform the steps and then trigger another function of either timer trigger or ant different type.

CodePudding user response:

There are a few solutions to achieve this.

Solution 1. Temporarily replace the timer trigger with http trigger

While debugging the app we just comment the first line of the original Run function and add an http trigger instead like this:

public static async Task Run([HttpTrigger] Microsoft.AspNetCore.Http.HttpRequest req, ILogger log)
// public static async Task Run([TimerTrigger("0 0 * * * *")] TimerInfo myTimer, ILogger log)
{
   // Your code here
}

Then when running the app you'll see an endpoint like this:

enter image description here

Just open the endpoint in browser (or postman) and the function will get called.

And right before pushing the code to the repo, just bring back the original Run function and remove the http trigger one.

Solution 2: Add another http trigger that calls the timer function
Add the following function to your app to expose an http trigger.

        [FunctionName("Test")]
        public static async Task Test([HttpTrigger] Microsoft.AspNetCore.Http.HttpRequest req, ILogger log)
        {
            Run(null, log);
        }

The function basically calls the Run function. So when you run the app, again you'll get an endpoint from the console that can be used from the browser to trigger the function. The url will look like this:
http://localhost:7071/api/Test

  • Related