-
-
Save MrMikeFloyd/dacb049eaa1d35bd4f93eb02728a9f39 to your computer and use it in GitHub Desktop.
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": "" | |
} | |
} |
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"
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?
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.
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?
@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 :)
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);
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);
Steps necessary for reproducing:
HttpTrigger.cs
local.settings.json
according to match the desired table storage accountTableEntity
to match the underlying data model