Queue Triggered Azure Function App 2
Architecture
I'll implements this architecture using azure table storage as datastore and console application program as web app.
I recommand you to follow everything in previous practice. [1]
Procedure
If you complete all procedure in privious practice, comtinue following procedure.
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/002.queueTriggeredFunction_tableInsert/*" >> .git/info/sparse-checkout
git pull origin master
cd azure/002.queueTriggeredFunction_tableInsert/
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. Deploy Table Storage
az storage table create --name {yourTableStorageName} --account-name {yourStorageAccountName}
You can reuse storage account already created in previous practice.
Determine your table storage name and replace {yourTableStorageName} with that.
output
{
"created": true
}
6. Editting {FunctionAppName}.cs Source [2]
Open ~/source/azure/002.queueTriggeredFunction_tableInsert/(projectname).cs
Now our codes are
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("yourqueuename", Connection = "MyServiceBusConnection")]string myQueueItem, ILogger log)
{
log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
}
}
}
You should add some codes like this.
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Azure.WebJobs.Extensions.Storage;
namespace funcgkim0012
{
public class MyHealthData
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public int heat { get; set; }
public int heartbaet { get; set; }
}
public static class funcgkim0012
{
[FunctionName("funcgkim0012")]
[return: Table("gkHealthCareData")]
public static MyHealthData Run([ServiceBusTrigger("q-gkim-02", Connection = "MyServiceBusConnection")]string myQueueItem, ILogger log)
{
MyHealthData md = JsonConvert.DeserializeObject<MyHealthData>(myQueueItem);
log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
return md;
}
}
}
7. 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
## Import package of Json
dotnet add package Newtonsoft.Json --version 11.0.2
8. Local Build
## bash
func start --build
If error occur saying
[10/8/19 8:44:12 AM] A host error has occurred
[10/8/19 8:44:12 AM] System.Private.CoreLib: Could not load file or assembly 'Microsoft.Azure.WebJobs.Extensions.Storage, Version=3.0.8.0, Culture=neutral, PublicKeyToken=.........'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621). System.Private.CoreLib: Could not load file or assembly 'Microsoft.Azure.WebJobs.Extensions.Storage, Version=3.0.8.0, Culture=neutral, PublicKeyToken=........'.
[10/8/19 8:44:12 AM] Stopping JobHost
Value cannot be null.
Parameter name: provider
Then you have to use other extension version of azuer Storage. This error was occured because visual studio code and package version didn't match.
I recommand 3.0.4 version of Microsoft.Azure.WebJobs.Extensions.Storage
1) Test 1
Open another tab at your browser and open additional terminal in azure. And then type..
cd ~/source/azure/001.queueTriggeredFunction/sender/"
Open Program.cs
...
static string ServiceBusConnectionString="....";
static string QueueName="...";
...
Insert your queue name and connectionString. Save and run “dotnet run”
## bash
dotnet run
In your terminal running azure function app project, you can see a message log.
9. 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