Azure Portal中C#函数连接Azure存储表失败及程序集引用问题
问题场景
在Azure Portal中编写Azure Function代码,用于连接Azure存储表、校验表中RowKey对应的指定变量后返回HTTP响应,但出现编译错误,不清楚如何在Portal中添加程序集引用。
错误信息
run.csx(2,1): error CS0006: Metadata file 'Microsoft.WindowsAzure.Storage' could not be found
run.csx(8,17): error CS0234: The type or namespace name 'WindowsAzure' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
run.csx(27,5): error CS0246: The type or namespace name 'CloudStorageAccount' could not be found (are you missing a using directive or an assembly reference?)
原代码
#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; public static async Task<IActionResult> Run(HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string xx= req.Query["xx"]; if (string.IsNullOrEmpty(xx)) { return new BadRequestObjectResult("Please enter an xx."); } string account = "XXW"; string accountKey = "YYW"; string tableName = "TWY"; string connectionString = $"DefaultEndpointsProtocol=https;AccountName={account};AccountKey={accountKey};EndpointSuffix=core.windows.net"; CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); CloudTable table = tableClient.GetTableReference(tableName); TableOperation operation = TableOperation.Retrieve(xx, xx); TableResult result = await table.ExecuteAsync(operation); string responseMessage = (result.Result != null) ? $"xx: {xx} exists in the table." : $"xx: {xx} does not exist in the table."; return new OkObjectResult(responseMessage); }
解决方案
1. 替换过时的存储程序集引用
Microsoft.WindowsAzure.Storage是旧版存储SDK,Azure Portal的Functions运行时已不再默认提供,需替换为官方推荐的新版SDKAzure.Data.Tables。
2. 适配新版SDK修改代码
更新引用指令、命名空间及API调用逻辑,修改后的代码如下:
#r "Newtonsoft.Json" #r "Azure.Data.Tables" using System.Net; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Primitives; using Newtonsoft.Json; using Azure.Data.Tables; public static async Task<IActionResult> Run(HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string xx = req.Query["xx"]; if (string.IsNullOrEmpty(xx)) { return new BadRequestObjectResult("Please enter an xx."); } string account = "XXW"; string accountKey = "YYW"; string tableName = "TWY"; string connectionString = $"DefaultEndpointsProtocol=https;AccountName={account};AccountKey={accountKey};EndpointSuffix=core.windows.net"; // 初始化TableClient TableClient tableClient = new TableClient(connectionString, tableName); // 查询实体(假设xx同时作为PartitionKey和RowKey) var entity = await tableClient.GetEntityAsync<TableEntity>(xx, xx); string responseMessage = entity != null ? $"xx: {xx} exists in the table." : $"xx: {xx} does not exist in the table."; return new OkObjectResult(responseMessage); }
3. Portal中程序集引用说明
在Azure Portal的Function代码编辑器中,通过#r "AssemblyName"指令引用程序集时,仅能引用运行时默认提供的程序集或自定义部署包中的私有程序集。Azure.Data.Tables属于当前Functions运行时默认支持的程序集,直接通过#r "Azure.Data.Tables"引用即可,无需额外安装步骤。
若需使用未默认提供的程序集,需将函数以自定义部署包(如Zip部署)的方式发布,并将所需NuGet包的DLL包含在包内,再通过相对路径引用。但存储表场景使用Azure.Data.Tables即可满足需求,无需额外部署操作。
内容的提问来源于stack exchange,提问作者carlplusplus

