Convert Windows service to Azure Function

In the past I wrote a post how to migrate Windows service to Azure using Azure WebJobs. Another option for migration of Windows service to Azure is to use Azure Functions.


Udemy course: Migrate Windows service to Azure

Windows service I want to migrate to Azure acts as runtime for execution of scheduled jobs using Timer, which run every 60 seconds to execute scheduled jobs. JobSchedulerService execute JobExecutor.Execute() method every 60 seconds as visible in source code of Windows service:

public partial class JobSchedulerService : ServiceBase
{
    private Timer timer;

    public JobSchedulerService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        timer = new Timer();
        timer.Elapsed += OnTimer;
        timer.Interval = 60 * 1000;
        timer.Start();
    }

    protected override void OnStop()
    {
        timer.Stop();
    }

    private void OnTimer(object sender, ElapsedEventArgs e)
    {
        JobExecutor.Execute();
    }
}

To convert Windows service to Azure Function, add new project to Visual Studio solution:

Visual Studio - Add a new project

Select Azure Functions v1 (.NET Framework) as version, because Windows service is build on .NET Framework 4.7.2. Then select Timer trigger, none Storage Account and enter schedule “0 * * * * *”, which is CRON expression for triggering run every 60 seconds.

Visual Studio - Create a new Azure Functions Application

Code changes in function are simple, only call of JobExecutor.Execute() is added into Run method:

public static class JobSchedulerFunction
{
    [FunctionName("JobSchedulerFunction")]
    public static void Run([TimerTrigger("0 * * * * *")]TimerInfo timer, TraceWriter log)
    {
        JobExecutor.Execute();
    }
}

Next step is to create Azure resources to run Azure Function using Azure CLI:

# Before running this script check if Application Insights extension is installed:
# az extension show --name application-insights
# If not installed, install it by:
# az extension add --name application-insights

az group create --name WinServiceToAzureTest --location westeurope

az storage account create --resource-group WinServiceToAzureTest --name winservicetoazuretest --sku Standard_LRS --kind StorageV2
az monitor app-insights component create --resource-group WinServiceToAzureTest --app WinServiceToAzureTest --location westeurope --kind web

az functionapp create --name WinServiceToAzureTest --resource-group WinServiceToAzureTest --storage-account winservicetoazuretest --app-insights WinServiceToAzureTest --consumption-plan-location westeurope --runtime dotnet
az functionapp config appsettings set --name WinServiceToAzureTest --resource-group WinServiceToAzureTest --settings "FUNCTIONS_EXTENSION_VERSION=~1"

After finishing of run is created new Resource Group, Storage Account, Application Insights and finally Function App. Important part is here to set Azure Functions runtime to version 1 using application setting FUNCTIONS_EXTENSION_VERSION.
Deployment of Azure Functions to Azure can be done by combination of PowerShell and Azure CLI:

Compress-Archive -Path .\bin\Release\net472\* -DestinationPath WindowsServiceToAzure.Job.Function.zip
az functionapp deployment source config-zip --resource-group WinServiceToAzureTest --name WinServiceToAzureTest --src .\WindowsServiceToAzure.Job.Function.zip

After deployment is Azure Function running, which is visible on Function App dashboard:

Azure Portal - Function App

Azure Function integration diagram displays function input and outputs:

Azure Portal - Function Integration

Detailed logs about the activity of Azure Function is visible in monitor:

Azure Portal - Function Monitor

Cleanup of used Azure resources can be done using Azure CLI:

az group delete --name WinServiceToAzureTest --yes -y

If you are interested in Windows services and how to migrate them to Azure, take my Udemy course Migrate Windows service to Azure.

Azure WebJobs or how to migrate Windows Service to Azure

Some time ago I solved problem how to migrate solution consisting from ASP.NET MVC website, Microsoft SQL Server DB and Windows Service to Microsoft Azure.


Udemy course: Migrate Windows service to Azure

Migration of website and DB is straightforward, but migration of Windows Service can be realized different ways. Purpose of Windows Service was act as runtime for execution of scheduled jobs using Timer, which run every 60 seconds to execute scheduled jobs as can be seen in source code:

public partial class JobSchedulerService : System.ServiceProcess.ServiceBase
{
    private Timer timer;

    protected override void OnStart(string[] args)
    {
        timer = new Timer();
        timer.Elapsed += OnTimer;
        timer.Interval = 60 * 1000;  // 60 seconds
        timer.AutoReset = false;
        timer.Start();
    }

    protected override void OnStop()
    {
        timer.Stop();
    }

    private void OnTimer(object sender, ElapsedEventArgs e)
    {
        JobExecutor.ExecuteScheduledJobs();
    }
}

JobSchedulerService execute JobExecutor.ExecuteScheduledJobs() method every 60 seconds. I found 3 options how to migrate this component into Azure infrastructure:

1. Azure Virtual Machine

Pros

  • No code changes in Windows Service required
  • The same deployment method as at on premise solution

Cons

  • Price of Azure VM

2. Azure Cloud Service

Pros

  • Scalability

Cons

  • Price of Azure Cloud Service
  • Complex code changes required

3. Azure WebJob

Pros

  • No price of Azure WebJob because it is part od Azure App Service

Cons

  • Minimal code changes required

Due to cost I decided for Azure WebJob. Transformation of Windows Service to Azure WebJob is simple. I created Azure WebJob project with the following implementation of Program and Function classes:

public class Program
{
    static void Main()
    {
        var host = new JobHost();
        host.Call(typeof(Functions).GetMethod("ExecuteJobs"));
    } 
}

public class Functions
{
    [NoAutomaticTrigger]
    public static void ExecuteJobs()
    {
        JobExecutor.ExecuteScheduledJobs();
    } 
}

Then I created ZIP package from build output and uploaded it to Azure Portal with WebJob type Triggered and CRON schedule “0 * * * * *” to execute every 60 seconds.

If you are interested about more detailed step by step instructions, take my Udemy course Migrate Windows service to Azure. This course describes all aspects of migration Windows service to Azure and help you to find and implement cost effective solution, which help you to save the money in future.