Skip to content

Instantly share code, notes, and snippets.

@MrMikeFloyd
Last active January 17, 2021 22:00
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrMikeFloyd/dacb049eaa1d35bd4f93eb02728a9f39 to your computer and use it in GitHub Desktop.
Save MrMikeFloyd/dacb049eaa1d35bd4f93eb02728a9f39 to your computer and use it in GitHub Desktop.
Sample for binding an Azure Function (v2) to an external Table Storage using CloudTable instead of IQueryable. Tested on Visual Studio Code 1.19.3 and .NET Core 2.0.5 on macOS.
using System.IO;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs.Host;
using Newtonsoft.Json;
using System;
using Microsoft.WindowsAzure.Storage.Table;
using System.Collections.Generic;
using System.Net.Http;
using System.Net;
using System.Text;
namespace Company.Function
{
public class TabEntity : TableEntity
{
public Int32 rowX { get; set; }
public Int32 rowY { get; set; }
public Int32 rowZ { get; set; }
}
public static class HttpTrigger
{
[FunctionName("HttpTrigger")]
public static HttpResponseMessage Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = null)]HttpRequest req,
[Table("sampletable")] CloudTable tab,
TraceWriter log)
{
log.Info("Kicking off.");
// Load data from TableStorage using CloudTable
var querySegment = tab.ExecuteQuerySegmentedAsync(new TableQuery<TabEntity>(), null);
StringContent responseContent = null;
foreach (TabEntity item in querySegment.Result)
{
log.Info($"Data loaded: '{item.PartitionKey}' | '{item.RowKey}' | '{item.rowX}' | '{item.rowY}'");
responseContent = new StringContent(JsonConvert.SerializeObject(item), Encoding.UTF8, "application/json");
}
log.Info("Done.");
return new HttpResponseMessage(HttpStatusCode.OK){
Content = responseContent
};
}
}
}
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=xyz;AccountKey=01234567890",
"AzureWebJobsDashboard": ""
}
}
@Preeti1910
Copy link

it was very useful for me. solved my problem. also tell how can I add condition in query like filter in this statement tab.ExecuteQuerySegmentedAsync(new TableQuery(), null);

@MrMikeFloyd
Copy link
Author

MrMikeFloyd commented Jul 22, 2019

it was very useful for me. solved my problem. also tell how can I add condition in query like filter in this statement tab.ExecuteQuerySegmentedAsync(new TableQuery(), null);

Glad to hear it helped @Preeti1910! TableQuery supports "Builder Pattern"-Style Where clauses like so (Example taken from the official docs found here: var tableQuery = new TableQuery() .where('Name == ? or Name <= ?', 'Person1', 'Person2'); .or('Age >= ?', 18);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment