From Azure Portal,
Click New
Search for Function App
Click Function App in Results
Click Create
Supply App name, Subscription, Resource Group, Hosting Plan, Location and Storage and click Create
After Function App is created, navigate to it.
From here we'll use a pre-made function and alter it to write to Table Storage.
Select Webhook + API, C# as the language and click Create this Function
From the Integrate option, add Azure Table Storage. Note the Table parameter name.
Add file item.csx (Click Save and Run to ensure no errors)
This is an object that inherits TableEntity and adds ItemName
#r "Microsoft.WindowsAzure.Storage"
using Microsoft.WindowsAzure.Storage.Table;
public class Item : TableEntity
{
public string ItemName {get;set;}
}
Add a reference to item.csx to run.csx(Click Save and Run to ensure no errors)
This allows Item object to be used in run.csx
#load "item.csx"
Change into Synchronous Function (Click Save and Run to ensure no errors)
This is required for the next step, out is not allowed on Asynchronous functions
...
public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log)
...
dynamic data = req.Content.ReadAsAsync<object>().Result;
...
Add an out variable and assign value (Click Save and Run to ensure no errors)
Add out item with the name of the table parameter name
...
public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log, out Item outputTable)
...
outputTable = new Item() {PartitionKey = "some-partition-key", RowKey=Guid.NewGuid().ToString(), ItemName = name ?? "No name"};
...
Here is complete listing of run.csx
#load "item.csx"
using System.Net;
public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log, out Item outputTable)
{
log.Info("C# HTTP trigger function processed a request.");
// parse query parameter
string name = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
.Value;
// Get request body
dynamic data = req.Content.ReadAsAsync<object>().Result;
// Set name to query string or body data
name = name ?? data?.name;
outputTable = new Item() {PartitionKey = "some-partition-key", RowKey=Guid.NewGuid().ToString(), ItemName = name ?? "No name"};
return name == null
? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
: req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
}
Every time this function runs, an entry will be created in the Azure Table Storage.
You can use Microsoft Azure Storage Explorer to view these entries.
Please do not forget to press the "Thumb's Up" button if this article was helpful and valuable for EE members.
It also provides me with positive feedback. Thank you!
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Commented: