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": ""
}
}
@MrMikeFloyd
Copy link
Author

MrMikeFloyd commented Feb 22, 2018

Steps necessary for reproducing:

  1. Create new Azure Functions project in VS Code
  2. Create new Azure HTTP Trigger Function
  3. Insert Code from HttpTrigger.cs
  4. Change local.settings.json according to match the desired table storage account
  5. Change class implementing TableEntity to match the underlying data model

@LocalJoost
Copy link

Can you please post the solution and the project too? I can be very bare bones. But there's something missing. If I copy your file into my project I get a "Type or namespace TableAttribute not found"

@artemious7
Copy link

My project also cannot find the TableAttribute.
I'm in .NET Core project with Azure Function v2 in Visual Studio 2017 15.8.7 on Windows 10. Looks like some Nuget package is missing. How to know which one?

@artemious7
Copy link

I was missing the Table attribute, having only these Nuget packages in my .NET Standard 2.0 project: Microsoft.NET.Sdk.Functions 1.0.23 and NETStandard.Library 2.0.3. After digging I found out that this attribute is in package Microsoft.Azure.WebJobs.Extensions.Storage 3.0.1, which I had to include.

@yzorg
Copy link

yzorg commented Nov 1, 2018

You're assigning to responseContent inside the foreach loop. If I'm reading that correctly you're only sending the last row of the table, but doing the work to deserialize all rows. That was intentional?

@MrMikeFloyd
Copy link
Author

@yzorg sorry for the late response. Your assumption is accurate. This is a very minimal example to demonstrate accessing TableStorage, so don't expect any serious functionality here :)

@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