C# Azure函数输入绑定:哪个NuGet包用于;表「;属性

C# Azure函数输入绑定:哪个NuGet包用于;表「;属性,c#,azure-functions,azure-table-storage,C#,Azure Functions,Azure Table Storage,我正在使用HttpTrigger创建一个新的azure函数。我想实现一个azure表存储表作为输入绑定。根据msdn中的一个源代码示例,我无法确定在哪个NuGet包中可以找到“Table”属性 编译问题: public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,

我正在使用HttpTrigger创建一个新的azure函数。我想实现一个azure表存储表作为输入绑定。根据msdn中的一个源代码示例,我无法确定在哪个NuGet包中可以找到“Table”属性

编译问题:

public static async Task<IActionResult>
         Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, 
         [Table("AzureWebJobsHostLogscommon")] CloudTable cloudTable, 
         ILogger log)
找不到类型或命名空间“TableAttribute”(是否缺少using指令或程序集引用?)

导致问题的代码行:

public static async Task<IActionResult>
         Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, 
         [Table("AzureWebJobsHostLogscommon")] CloudTable cloudTable, 
         ILogger log)
公共静态异步任务
运行([HttpTrigger(AuthorizationLevel.Function,“get”,“post”,Route=null)]HttpRequest请求,
[表(“AzureWebJobsHostLogscommon”)]CloudTable,
ILogger日志)
我提到的MSDN中的源代码示例可以在这里找到:

在这里:

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

我还看到了这个stackoverflow线程:

但第一种解决方案对我来说只是一种变通方法,因为表存储连接是在函数执行期间完成的,而不是作为输入绑定。如果您看到第二个建议的解决方案,它将显示与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");
        }
    }
}
使用Microsoft.AspNetCore.Http;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Azure.WebJobs;
使用Microsoft.Azure.WebJobs.Extensions.Http;
使用Microsoft.Extensions.Logging;
使用Microsoft.WindowsAzure.Storage.Table;
使用制度;
Net系统;
使用System.Threading.Tasks;
命名空间表存储集成.HTTPTrigger
{
公共静态类函数1
{
[函数名(“DoSomething”)]
公共静态异步任务运行(
[HttpTrigger(AuthorizationLevel.Function,“get”,“post”,Route=null)]HttpRequest请求,
[表(“AzureWebJobsHostLogscommon”)]CloudTable,
ILogger日志)
{
字符串someHttpGetParameter=req.Query[“someParameter”];
// .... 
//此处将执行一些附加代码,但与问题无关
// .....
返回新的OkObjectResult($“提供的数据”);
}
}
}

拟议的实施仍然有效吗?如果是,我必须安装哪个NuGet包来解析表属性?

根据我的测试,如果你想实现“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; }

    }
}
使用系统;
使用System.IO;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Azure.WebJobs;
使用Microsoft.Azure.WebJobs.Extensions.Http;
使用Microsoft.AspNetCore.Http;
使用Microsoft.Extensions.Logging;
使用Newtonsoft.Json;
使用System.Data.SqlClient;
使用系统文本;
使用系统配置;
使用Microsoft.Extensions.Configuration;
使用Microsoft.WindowsAzure.Storage.Table;
命名空间TestFunapp
{
公共静态类函数1
{
#安装软件包Microsoft.Azure.WebJobs.Extensions.Storage
[功能名称(“功能1”)]
公共静态异步任务运行(
[HttpTrigger(AuthorizationLevel.Anonymous,“get”,“post”,Route=null)]HttpRequest请求,
[表格(“人员”)]CloudTable CloudTable,
ILogger日志,ExecutionContext(上下文)
{
LogInformation(“C#HTTP触发器函数处理了一个请求。”);
字符串名称=请求查询[“名称”];
TableOperation retrieveOperation=TableOperation.Retrieve(“Jim”、“Xu”);
TableResult retrievedResult=等待cloudTable.ExecuteAsync(retrieveOperation);
if(retrievedResult.Result!=null)
log.LogInformation(((人员)retrievedResult.Result).Email);
其他的
log.LogInformation(“无法检索电子邮件”);
string requestBody=等待新的StreamReader(req.Body).ReadToEndAsync();
动态数据=JsonConvert.DeserializeObject(requestBody);
名称=名称??数据?.name;
返回名称!=null
?(ActionResult)新的OkObjectResult($“你好,{name}”)
:new BadRequestObjectResult(“请在查询字符串或请求正文中传递名称”);
}
}
公共类人物:TableEntity
{
公众人物(字符串lastName、字符串firstName)
{
this.PartitionKey=lastName;
this.RowKey=firstName;
}
公众人士({}
公共字符串电子邮件{get;set;}
}
}


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

您需要添加此NuGet软件包:

尝试添加此NuGet软件包:是的,成功了。你能不能把你的评论作为答复发给我,这样我就可以接受了?Tvm!很高兴这有帮助。有关示例,请参见下文。对我来说,最重要的信息是行#install package Microsoft.Azure.WebJobs.Extensions.Storage,它解决了找不到类型或命名空间的问题。和他评论中提到的@silent一样。