在C#for Azure Function应用程序中获取错误



有人能帮我修复这些错误吗?

#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.Table;
public static async Task<IActionResult> Run
(HttpRequest req,
CloudTable objUserProfileTable,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string firstname = null, lastname = null;
string requestBody = await new 
StreamReader(req.Body).ReadToEndAsync();
dynamic inputJson = JsonConvert.DeserializeObject(requestBody);
firstname = firstname ?? inputJson?.firstname;
lastname = inputJson?.lastname;
UserProfile objUserProfile = new UserProfile(firstname, lastname) ;
TableOperation objTblOperationInsert = 
TableOperation.Insert(objUserProfile);
await objUserProfileTable.ExecuteAsync(objTblOperationInsert);
return (lastname + firstname) != null 
? (ActionResult)new OkObjectResult($"Hello, {firstname + " " + lastname}")
:new BadRequestObjectResult("Please pass a name on the query" + "string or in the request body");
}
class UserProfile : TableEntity
{
public UserProfile(string firstName, string lastName)
{
this.PartitionKey = "p1";
this.RowKey = Guid.NewGuid().ToString();
this.FirstName = firstName;
this.LastName = lastName;
}
UserProfile() {}
public string FirstName { get; set; }
public string LastName { get; set; }
}

我得到这些错误:

错误CS0006:找不到元数据文件"Microsoft.WindowsAzure.Storage">

错误CS0234:命名空间"Microsoft"中不存在类型或命名空间名称"WindowsAzure"(是否缺少程序集引用?(

错误CS0246:未能找到类型或命名空间名称"CloudTable"(是否缺少using指令或程序集引用?(

错误CS0246:未能找到类型或命名空间名称"TableEntity"(是否缺少using指令或程序集引用?(

错误CS0246:找不到类型或命名空间名称"TableOperation"(是否缺少using指令或程序集引用?(

错误CS0103:名称"TableOperation"在当前上下文中不存在

错误CS1061:"UserProfile"不包含"PartitionKey"的定义,并且找不到可访问的扩展方法"PartitionKey',该方法接受"UserProfile'"类型的第一个参数(是否缺少using指令或程序集引用?(

错误CS1061:"UserProfile"不包含"RowKey"的定义,并且找不到接受"UserProfile"类型的第一个参数的可访问扩展方法"RowKey"(是否缺少using指令或程序集引用?(

编译失败。

我认为微软可能需要更新他们的文档。

Microsoft.WindowsAzure.Storage已被弃用,并已被其他一些Nuget包所取代。

https://www.nuget.org/packages/WindowsAzure.Storage/9.3.3?_src=template

对于您想要做的事情,据我所知,您需要对表使用Azure.Data.Tables

https://github.com/Azure/azure-sdk-for-net/blob/Azure.Data.Tables_12.4.0/sdk/tables/Azure.Data.Tables/README.md

问题是,它不是一个可以使用#r语句提取的包。

要直接在门户中引用它,您需要在函数中创建一个函数.proj文件,并将其作为引用包含在内。

基本上看起来是这样的。。。

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Data.Tables" Version="12.4.0" />
</ItemGroup>
</Project>

然后,您可以像创建任何其他程序一样,在正常的using语句中使用它。

接下来的问题是如何将它合并到函数中,因为我可以看到,通过绑定或其他方式,它是Run方法签名的一部分。

如果您无法使绑定工作,您只需要在代码级别直接使用连接字符串或类似性质的东西来连接它。

最新更新