2
votes

I am creating a new azure function using HttpTrigger. I want to implement an azure table storage table as input binding. Following a source code example in msdn, I am not able to figure out in which NuGet package the "Table" attribute can be found.

Compile issue:

The type or namespace 'TableAttribute' could not be found (are you missing a using directive or an assembly reference?)

Line of code, causing the issue:

public static async Task<IActionResult>
         Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, 
         [Table("AzureWebJobsHostLogscommon")] CloudTable cloudTable, 
         ILogger log)

The source code examples in MSDN I am refering to can be found here:

https://github.com/MicrosoftDocs/azure-docs/blob/master/articles/azure-functions/functions-bindings-storage-table.md#input---c-example---one-

and here:

https://docs.microsoft.com/de-de/azure/azure-functions/functions-bindings-storage-table#input---c-example---cloudtable

The second example also shows the using directives. But even when copying the example the table-attribute cannot be resolved properly.

I have also seen this stackoverflow thread:

input-binding to table storage with an http-triggered function

but the first solution is only a workaround for me, as the table-storage connection is done during execution of the function and not as input binding. If you see the second proposed solution, it shows same code as in MSDN.

Here is my code:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage.Table;
using System;
using System.Net;
using System.Threading.Tasks;

namespace TableStorageIntegration.HTTPTrigger
{
    public static class Function1
    {
        [FunctionName("DoSomething")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,      
            [Table("AzureWebJobsHostLogscommon")] CloudTable cloudTable,
            ILogger log)
        {
            string someHttpGetParameter = req.Query["someParameter"];
            // .... 
            // Some addition code to be executed here but not relevant for the issue
            // .....
            return new OkObjectResult($"Data provided");
        }
    }
}

Is the proposed implementation still valid? And if yes, which NuGet package do I have to install to resolve the table attribute?

2
Yes, that worked. Could you just send your comment as an answer so I can accept it? Tvm!rekcul
glad it helped. see belowsilent

2 Answers

0
votes

According to my test, if you want to implement "Table" attribute, you can extend the class TableEntity. For example

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Data.SqlClient;
using System.Text;
using System.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.WindowsAzure.Storage.Table;

namespace TestFunapp
{
    public static class Function1
    {
        # install package Microsoft.Azure.WebJobs.Extensions.Storage
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            [Table("People")] CloudTable cloudTable,
            ILogger log, ExecutionContext  context)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];
            TableOperation retrieveOperation = TableOperation.Retrieve<People>("Jim", "Xu");

            TableResult retrievedResult = await cloudTable.ExecuteAsync(retrieveOperation);
            if (retrievedResult.Result != null)
                log.LogInformation(((People)retrievedResult.Result).Email);

            else
                log.LogInformation("The Email could not be retrieved.");

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            return name != null
                ? (ActionResult)new OkObjectResult($"Hello, {name}")
                : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
        }
    }
    public class People : TableEntity
    {

        public People(string lastName, string firstName)
        {
            this.PartitionKey = lastName;
            this.RowKey = firstName;
        }

        public People() { }

        public string Email { get; set; }

    }
}

enter image description here

For more details, please refer to the document.