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

Abdallah Shukor 1 Reputation point
2021-11-30T11:58:17.96+00:00

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. 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 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 time trigger.

153706-functionapptimertrigger.png

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,911 questions
Developer technologies .NET .NET CLI
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MayankBargali-MSFT 70,936 Reputation points Moderator
    2021-12-01T09:13:00.497+00:00

    @Abdallah Shukor If you have long running task then you can use Durable functions as you have mentioned that it will process and upload as it may take time. But running the program.cs first is not possible in either the Durable functions or Timmer trigger functions.

    Once your timmer trigger function will execute then only your copied code from program.cs inside the run method will be executed.

    The alternative that I could think of would be using two functions and service bus i.e. one function would have the business logic to process and upload (same code as program.cs) that send the message to the service bus with the scheduled time (you need to send schedule new message every hour). Another function that will listen to messages to your entity (queue/subscription) and based on the event perform the further operation.

    2 people found this answer helpful.

  2. sadomovalex 3,636 Reputation points
    2021-12-02T14:43:40.473+00:00

    just create class with 1 static method (e.g. MyClass.Run()) and copy code from Program.cs > Main() method to this MyClass.Run(). After that add this class to the same project where your timer-triggered function is and call it from it:

    public static class MyTimerTriggeredFunction
    {
        [FunctionName("MyTimerTriggeredFunction")]
        public static void Run([TimerTrigger("0 0 4 * * *")]TimerInfo myTimer, TraceWriter log)
        {
            MyClass.Run();
        }
    }
    

    Azure functions project is like regular .Net class library - it is compiled to dll and you may even use it as regular class library (e.g. reference in other projects). The only thing which is harded with timer-triggered functions is debugging because you need to wait when it will be triggered. But there is method to force timer-triggered function to run: How to test and debug Timer triggered Azure functions running locally on localhost.

    1 person found this answer helpful.
    0 comments No comments

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.