Azure函数输入绑定:“表”属性的哪个NuGet包?

时间:2019-09-06 13:25:38

标签: c# azure-functions azure-table-storage

我正在使用HttpTrigger创建一个新的Azure函数。我想实现一个天蓝色表存储表作为输入绑定。遵循msdn中的源代码示例,我无法弄清楚可以在哪个NuGet包中找到“ Table”属性。

编译问题:

找不到类型或名称空间“ TableAttribute”(您是否缺少using指令或程序集引用?)

代码行,引起问题:

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

我要引用的MSDN中的源代码示例可以在这里找到:

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

在这里:

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

第二个示例还显示了using指令。但是即使复制示例,表属性也无法正确解析。

我也看到了这个stackoverflow线程:

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

但是第一个解决方案对我来说只是一个解决方法,因为表存储连接在函数执行期间完成,而不是作为输入绑定。如果您看到第二个建议的解决方案,它显示的代码与MSDN中的代码相同。

这是我的代码:

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");
        }
    }
}

建议的实施是否仍然有效?如果是的话,我必须安装哪个NuGet软件包来解析table属性?

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

根据我的测试,如果要实现“ Table”属性,则可以扩展类TableEntity。例如

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

有关更多详细信息,请参阅document