Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 19, 2021 04:07 am GMT

Deploy Azure web job on Docker

What is WebJobsSDK?

The Azure WebJobs SDK is a framework that simplifies the task of writing background processing code that runs in Azure WebJobs. It includes a declarative binding and trigger system that works with Azure Storage Blobs, Queues and Tables as well as Service Bus. The binding system makes it incredibly easy to write code that reads or writes Azure Storage objects. The trigger system automatically invokes a function in your code whenever any new data is received in a queue or blob.

The version 3.x of the WebJobs SDK supports both.NET Core and.NET Framework console apps. This opens all sorts of possibilities of hosting it apart from App Service. In this article I'll walk you through process of hosting the Web Job on Docker.

Create Web job using Web Job SDK3

Let's create simple queue triggered web job which will print the message received in the azure queue.

  1. open a command prompt and enter the below command.
dotnet new console -o "ContainerizedWebJob"cd ContainerizedWebJob
  1. Install the latest stable 3.x versions of the following NuGet packages.
    • Microsoft.Azure.WebJobs
    • Microsoft.Azure.WebJobs.Extensions

Here are the commands:

dotnet add package Microsoft.Azure.WebJobs --version 3.0.11dotnet add package Microsoft.Azure.WebJobs.Extensions --version 3.0.2
  1. We will need console logging using which we will print the recieved message from the queue on the console. Install the latest version of belo packages
dotnet add package Microsoft.Extensions.Logging --version 2.2.0dotnet add package Microsoft.Extensions.Logging.Console --version 2.2.0
  1. Install storage binding extension Starting with version 3.x, you must explicitly install the Storage binding extension required by the WebJobs SDK. In prior versions, the Storage bindings were included in the SDK. Install latest version of below package
dotnet add package Microsoft.Azure.WebJobs.Extensions.Storage --version 3.0.7
  1. Now we will create the host and configure it.Add program.cs file and replace the content of with below.
using Microsoft.Extensions.Hosting;using Microsoft.Extensions.Logging;namespace ContainerizedWebJob{    class Program    {        static void Main(string[] args)        {            var builder = new HostBuilder();            builder.UseEnvironment(EnvironmentName.Development);            builder.ConfigureWebJobs(b =>            {                b.AddAzureStorageCoreServices();                b.AddAzureStorage();            });            builder.ConfigureLogging((context, b) =>            {                b.AddConsole();            });            var host = builder.Build();            using (host)            {                host.Run();            }        }    }}

Add Functions.cs file which will have the functions which will process the queue message.

using Microsoft.Azure.WebJobs;using Microsoft.Extensions.Logging;namespace ContainerizedWebJob{    public class Functions    {        public static void ProcessQueueMessage(            [QueueTrigger("message-queue")] string message,            ILogger logger)        {            logger.LogInformation(message);        }    }}

Add appsettings.json

{  "AzureWebJobsStorage": "UseDevelopmentStorage=true"}
  1. Modify ContainerizedWebJob.csproj and add new <ItemGroup>.
<ItemGroup>  <None Update="appsettings.json">    <CopyToOutputDirectory>Always</CopyToOutputDirectory>  </None></ItemGroup>
  1. Lets run an test it.
dotnet builddotnet run

Create a queue named message-queue add the message.

Deploy Web job ondocker.

To deploy this application on docker we need to specify docker configuration. Add Dockerfile at the root of the application.

FROM mcr.microsoft.com/dotnet/core/runtime:2.2WORKDIR /appCOPY ./bin/Debug/netcoreapp2.2/publish .ENTRYPOINT [ "dotnet", "ContainerizedWebJob.dll" ]

Before we deploy this app on docker we need to make changes to our appsettings.json file as below.

{    "AzureWebJobsStorage": "UseDevelopmentStorage=true;DevelopmentStorageProxyUri=http://host.docker.internal"}

Note: The Azure Storage Emulator is bound in a local-only network configuration and Docker for Windows runs in a VM using Hyper-V. Hence to access the emulator running on host (Windows) we need to use the proxy http://host.docker.internal instead of http://127.0.0.1.

Now that we are done with all the configuration we shall create and run the docker image. To create a docker image, run the below command where you have put the Dockerfile.

docker build -t containerized-web-job

Once the image is create we shall run it using below command.

docker run -it --rm containerized-web-job

Note: above command will delete the container as soon as we stop the web job.

You can find source code here.


Original Link: https://dev.to/koditkarvedant/deploy-azure-web-job-on-docker-3cog

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To