/ CLOUD, AZURE

Queue Triggered Azure Function App 1

Architecture

We are going to make dotnet console application as task, azure function app as service.

Procedure

This pratice is optimized for bash shell and azure cli in Azure Cloud Shell. Go to azure portal and run azure cloud shell

1. Get source from github


git init {source}           #{source} can be replaced with other directory name you want
cd source
git remote add -f origin https://github.com/rudgh1027/cloud.git
git config core.sparseCheckout true
echo "azure/001.queueTriggeredFunction/*" >> .git/info/sparse-checkout
git pull origin master
cd azure/001.queueTriggeredFunction/

2. Editting Parameter Name


## complete names of resources
vi exported.dat

3. Deploy Service Bus Queue and Azure Function App

Just run


## deploy queue
./deployq.sh
## deploy function app
./deployFunc.sh

4. Make Project


./makeProj.sh
## Select a worker runtime:
## 1. dotnet
## 2. node
## 3. python
## 4. powershell (preview)
## Choose option: 1            Select 1

5. Editting {FunctionAppName}.cs Source

Now we can see Visual Studio code edittor. Open ~/source/azure/001.queueTriggeredFunction/(projectname).cs


using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;

namespace funcgkim0012
{
    public static class funcgkim0012
    {
        [FunctionName("funcgkim0012")]
        public static void Run([ServiceBusTrigger("myqueue", Connection = "")]string myQueueItem, ILogger log)
        {
            log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
        }
    }
}

Replace myqueue with your queue name in your sesrvice bus namespace. In my code, I already define connectionString name, MyServiceBusConnection. So replace "" with "MyServiceBusConnection".

Now our code is


using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.WebJobs.Extensions.Storage;

namespace funcgkim0012
{
    public static class funcgkim0012
    {
        [FunctionName("funcgkim0012")]
        public static void Run([ServiceBusTrigger("yourqueuename", Connection = "MyServiceBusConnection")]string myQueueItem, ILogger log)
        {
            log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
        }
    }
}

6. Import Nuget Package


## In your azure cloud shell, cd ..../{yourfunctionapp}/
## Import package of webjobs storage extension
dotnet add package Microsoft.Azure.WebJobs.Extensions.Storage --version 3.0.4

7. Local Build


## bash
func start --build

1) Test_1

Go to “~/source/azure/001.queueTriggeredFunction/sender/”

Open Program.cs and insert your queue name and connectionString.


...
      static string ServiceBusConnectionString="....";
      static string QueueName="..."; 
...

Save and run “dotnet run”


## bash
dotnet run

In your terminal running azure function app project, you can see a message log.

8. Deploy To Azure Function

Go to “~/source/azure/001.queueTriggeredFunction/{FunctionAppName}”


## bash
func azure functionapp publish {FunctionAppName}

1) Test_2

Go to “~/source/azure/001.queueTriggeredFunction/sender/”


## bash
dotnet run