【问题标题】:Azure function input binding: Which NuGet package for "Table" attribute?Azure 函数输入绑定:“表”属性的哪个 NuGet 包?
【发布时间】:2019-09-06 13:25:38
【问题描述】:

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

编译问题:

找不到类型或命名空间“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 包来解析表属性?

【问题讨论】:

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


【解决方案1】:

【讨论】:

【解决方案2】:

所以他们似乎出于某种原因拥有removed TableAttribute

不过,他们是 working,希望在 2022 年初将其恢复。

与此同时,如果您使用的是 Azure Tables,有几个解决方法:

  • 在新的扩展发布之前,继续使用支持表格的函数扩展的 v4 版本
  • 使用上面链接的 Azure.Data.Tables 库从 Azure Function 代码直接与 Azure Tables 服务交互。

【讨论】:

    【解决方案3】:

    根据我的测试,如果要实现“表”属性,可以扩展类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; }
    
        }
    }
    

    更多详情请参考document

    【讨论】:

    • 感谢您的示例。对我来说,最重要的信息是 # install package Microsoft.Azure.WebJobs.Extensions.Storage 这行解决了找不到类型或命名空间的问题。与@silent 在他的评论中已经提到的一样。
    猜你喜欢
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    相关资源
    最近更新 更多