Piotr Perak
Piotr Perak

Reputation: 11088

WorkerService deployed to Azure App Service is not starting

I'm creating a new WorkerService in .NET Core 3.1 (https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-3.1&tabs=visual-studio) and deploying it to Azure App Service. In it it has nothing more then on Worker service which logs a message every 3 seconds.

public class Worker : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            Log.Information("Worker running");
            await Task.Delay(3000, stoppingToken);
        }
    }
}

I'm deploying it using Azure Release pipeline's Azure App Service deploy task. Deployment goes through without any errors but the application is not starting. I know this because I check logs. Except logging in worker I also log application startup in Program.cs

When I navigate to console on Azure portal and start it with dotnet Worker.dll it starts and works fine. I see messages logged into blob container.

Do I need to do anything special for worker service? I was deploying .NET Core 3.1 web apps from Azure Release pipeline and I didn't have any problems.

Edit:

When published from VS2019 as Azure WebJob (this and folder are publish targets you can choose) it works as expected. However now it is visible in WebJobs blade. So this is different kind of deployment then from Azure Devops I think.

Upvotes: 2

Views: 4489

Answers (2)

Haider Al-Delfi
Haider Al-Delfi

Reputation: 21

To anyone still facing this and wants to avoid having to use WebJobs (including SDK) or paying for a VM etc..... All you need to do is go to your ****.csproj file for the project replace the second line from

<Project Sdk="Microsoft.NET.Sdk.Worker">

to

<Project Sdk="Microsoft.NET.Sdk.Web">

Now you can deploy your worker service on App-Service and it will start automatically including on restarts etc. as if it's a web app. You will have to restrict network traffic etc. for security.

Upvotes: -2

Jack Jia
Jack Jia

Reputation: 5549

Firstly, I suggest you write webjob apps with WebJobs SDK. The official way is to create a common console app, and then add functions.

Secondly, you need to add a run.cmd script in your webjob directory.

Here is a sample:

  1. Add additional argument to publish to specific path in Publish dotnet task.

--output $(Build.ArtifactStagingDirectory)\WebJob\app_data\Jobs\Continuous\WebJob

enter image description here

  1. Add a PowerShell task to generate run.cmd script

Important: Please ensure to set the right working directory as follwoing:

Script: "dotnet WebJob.dll %*" | Out-File run.cmd -Encoding ASCII , please change WebJob.dll to your dll name.

Working directory: $(Build.ArtifactStagingDirectory)\WebJob\app_data\Jobs\Continuous\WebJob

enter image description here

  1. Publish the artifact

Path to publish: $(Build.ArtifactStagingDirectory)\WebJob\

enter image description here

  1. If everything is OK, you will get an artifact after running build pipeline :

enter image description here

  1. Create the release pipeline

Choose the right folder. Mine is $(System.DefaultWorkingDirectory)/_Webjob-ASP.NET Core-CI/drop, you should change it with your correct folder path.

enter image description here

  1. Check web app:

enter image description here

enter image description here

Upvotes: 7

Related Questions