如何使用REST API从绑定到Azure存储功能的Azure表中获取记录?

问题描述 投票:0回答:1

从现有的Azure存储表(与Azure函数集成)中获取记录时,我在Azure门户编辑器中收到以下编译错误。

错误消息:

2020-04-23T17:05:36.870 [Information] Executing 'Functions.entry' (Reason='This function was programmatically called via the host APIs.', Id=8a9a5688-2dbf-4737-80ff-2a37f1c3a089)
2020-04-23T17:05:36.918 [Error] Function compilation error
Microsoft.CodeAnalysis.Scripting.CompilationErrorException : Script compilation failed.
   at async Microsoft.Azure.WebJobs.Script.Description.DotNetFunctionInvoker.CreateFunctionTarget(CancellationToken cancellationToken) at C:\azure-webjobs-sdk-script\src\WebJobs.Script\Description\DotNet\DotNetFunctionInvoker.cs : 314
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.Azure.WebJobs.Script.Description.FunctionLoader`1.GetFunctionTargetAsync[T](Int32 attemptCount) at C:\azure-webjobs-sdk-script\src\WebJobs.Script\Description\FunctionLoader.cs : 55
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.Azure.WebJobs.Script.Description.DotNetFunctionInvoker.GetFunctionTargetAsync(Boolean isInvocation) at C:\azure-webjobs-sdk-script\src\WebJobs.Script\Description\DotNet\DotNetFunctionInvoker.cs : 183
2020-04-23T17:05:37.000 [Error] run.csx(45,27): error CS1061: 'HttpMethod' does not contain a definition for 'method' and no extension method 'method' accepting a first argument of type 'HttpMethod' could be found (are you missing a using directive or an assembly reference?)
2020-04-23T17:05:37.030 [Error] run.csx(47,11): error CS1061: 'HttpMethod' does not contain a definition for 'method' and no extension method 'method' accepting a first argument of type 'HttpMethod' could be found (are you missing a using directive or an assembly reference?)
2020-04-23T17:05:37.060 [Error] run.csx(52,49): error CS1061: 'HttpMethod' does not contain a definition for 'Body' and no extension method 'Body' accepting a first argument of type 'HttpMethod' could be found (are you missing a using directive or an assembly reference?)
2020-04-23T17:05:37.101 [Error] Executed 'Functions.entry' (Failed, Id=8a9a5688-2dbf-4737-80ff-2a37f1c3a089)
Script compilation failed.

以下是我的代码:

#r "Newtonsoft.Json" 
#r "Microsoft.WindowsAzure.Storage"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using System.Net.Http;

 public class Entry
 {
            public string Id => Guid.NewGuid().ToString("n");
             .......
            public string RowKey => Id;          
}

public static async Task<IActionResult> Run( HttpMethod req, Newtonsoft.Json.Linq.JArray inputTable, IAsyncCollector<Entry> outputTable, ILogger log)  
{  
   log.LogInformation(req.method);

   if(req.method == "GET")
   {
       return (ActionResult) new OkObjectResult(inputTable);
   }  

   var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
   var entry = JsonConvert.DeserializeObject<Entry>(requestBody);

   if (entry != null)
   {
       await outputTable.AddAsync(entry);
       return (ActionResult) new OkObjectResult(entry);
   }

   return new BadRequestObjectResult("Invalid Entry request");
}

我想念什么?

azure azure-functions azure-table-storage
1个回答
0
投票

这是您面临的:

enter image description here

根据您的错误,我认为您需要两个步骤来解决它。

首先,请确保您已安装扩展名,在此位置创建function.proj文件:

enter image description here

Function.proj

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netcoreapp3.0</TargetFramework>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
    </ItemGroup>
</Project>

第二,我认为您现在使用的方法不正确。

您的结构应该是这样的:(HttpMethod没有名为“方法”的方法,只有HttpRequest具有,方法的名称应为“ Method”)]

#r "Newtonsoft.Json" 
#r "Microsoft.WindowsAzure.Storage"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using System.Net.Http;

 public class Entry
 {
            public string Id => Guid.NewGuid().ToString("n");
            public string RowKey => Id;          
}

public static async Task<IActionResult> Run( HttpRequest req, ILogger log)  
{  
   log.LogInformation(req.Method);

   if(req.Method == "GET")
   {
       return new OkObjectResult("OKOK");
   }  

   var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
   var entry = JsonConvert.DeserializeObject<Entry>(requestBody);

   if (entry != null)
   {

   }

   return new BadRequestObjectResult("Invalid Entry request");
}

然后它将正常工作:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.