5 min read · Aug 14, 2019
Hi guys ! :D In this article I am going to discuss how we can execute an windows executable application (exe file) from an Azure function. Here, I do illustrate this with the real world scenario. Okay, Let’s start :D
Prerequisites
Those prerequisites are based on the example I am discussing in this article.
- Visual Studio 2017 with .NET Core 2.2
- Visual Studio 2015
- Azure Subscription
- Understanding of Azure Functions and Azure Environment.
Problem
Imagine that you have an application which is developed on .NET framework as a Windows Console Application. Now you want to migrate this application to the Azure Environment and you want to work this application as an Azure Function.
In my scenario, I had an Windows Console Application, developed on .NET framework, and hosted in IIS 8, and this application is executed by Windows Task Scheduler, within a Time interval. Simply this is a Windows Job. I had to move this Windows Application to the Azure Environment, by keeping the Job’s purpose as it is.
And next thing was, I had to go for an optimal solution, by considering the following limitations.
- As this application is developed years ago, the Business Scope is very vast and Business Domain is not Centralized. No One Knows exactly & perfectly what the hell application is doing, though it’s still executing. And I should highlight, this Windows Job is a minor unit, in a very diversified large business application.
2. Needed the solution within One Night. Lol ! Means, there was a Limited Time Frame.
3. Better to implement with Less Code Modifications in the existing application/job.
4. No Compatibility Issues. As the Application is on .NET 4.x Framework, and the Azure Function is on .NET Core 2.x.
Whatever the case I know that I have to go for Azure Timer Trigger Function and It’s obvious. With that understanding to implement above requirement I found out following 3 options.
- Write the Azure function from the ground — Develop an Azure Timer Trigger Function from the ground, by gathering the Business requirements all. But It was not a feasible solution, by satisfying the above limitations. Simply this approach would take lot of time. So removed this approach from the bucket.
- Class Library — Publish the application as a Class Library and Reference it to the Azure Function. This was kind of good solution as I don’t want to re-write the whole application from the scratch. But, there was a problem. The existing application is using App.config file to use some Configurations. When reference the application as a Class Library, the application is not capable to read its App.Config file, using ConfigurationManager. To implement this capability, have to modify the existing code base, and the way to implement this was not something satisfying. This also takes time but not much time as in the approach 1. So I had to re-think of this, so I kept this aside for a while.
- Execute an Executable Application — In this approach, publish the existing application as an exe as it is, then execute this exe file from the Azure Function using less than 10 lines of code. No need to Code Modifications inside the application, and when executes, it does its job without Noises :D So I decided to go for this solution. And this overcomes all the above limitations and challenges.
Yo Yo ! Now I feel Super Cool ! :D Let’s see how I implemented this then.
Architecture
The following is the Design Architecture for the solution.
To implement the above solution, have to follow mainly 4 steps.
- Publish the existing application as an executable application.
- Then copy the bin folder content of the above generated executable application.
- Put the above copied files to a Folder in your Function App ( This step is optional)
- Then Execute the above application from the Azure Function.
That’s all. Now let’s go for the Code ;-)
Code
Here, I have developed a sample application which presents the above solution. The Application has two components.
- .NET 4.5 Solution, this can be Windows Application, Console Application or Class Library. here I call this as ProjectA
- .NET Core 2.2 Azure Function, here I call this as ProjectB
Please do consider following steps.
Step 1 — Generate Exe File, from ProjectA.
- Go to ProjectA, Right Click, then Go to Properties, then in the Application Panel, Select Output type as Console Application. This is a MUST !
- Then Build.
- You can Copy the bin Folder, where the Application’s exe is, and paste to a folder inside the Azure Function App.
Step 2 — Create Azure Function to Execute ProjectA exe.
- Create Azure Function. In the Program File, put the following code snippet.
This is the code snippet we use to execute the exe file from Azure function.
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "[Put your Exe Full Path, a/b/c.exe]";
// Keep this False, is MUST !
process.StartInfo.UseShellExecute = false;
// Enabling Reading Application's Outputs
process.StartInfo.RedirectStandardOutput = true;
process.Start();
// This would Return your Application's Only First
// Console.Write(). This is not a MUST !
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit(); log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
That’s all. Now everything is done. Publish to Azure and Run the Azure Function. Here in my example, I just print Hello, World in ProjectA.
So, that’s all about executing exe from Azure Function. So, you enjoyed ? then clap, bookmark, comment and share :D Let’s share what we know and what we don’t know.
Happy Coding ! Enjoy Beer ! :D