在C# 6/.NET 3.1的CSX Azure Function中添加Azure.Communication.Sms引用失败
问题描述
在基于C# 6(对应.NET 3.1)的CSX Azure Function脚本中,使用#r "Azure.Communication.Sms"添加程序集引用时触发错误:error CS0006: Metadata file 'Azure.Communication.Sms' could not be found。但相同代码在本地.NET 6控制台应用中引用该NuGet包可正常运行,导致无法使用SmsClient,相关代码如下:
#r "Azure.Communication.Sms" #r "Azure.Messaging.EventGrid" #r "System.Text.Json" #r "System.Memory.Data" using Azure.Messaging.EventGrid; using Azure.Messaging.EventGrid.SystemEvents; using System.Text.Json; using Azure; using Azure.Communication; using Azure.Communication.Sms; public static void Run(EventGridEvent eventGridEvent, ILogger log) { log.LogInformation(eventGridEvent.EventType.ToString()); if (eventGridEvent.EventType == "Microsoft.Communication.SMSReceived") { var msg = JsonSerializer.Deserialize<Azure.Messaging.EventGrid.SystemEvents.AcsSmsReceivedEventData>(eventGridEvent.Data); log.LogInformation(msg.Message); // now send out to others string connectionString = "ENTER CONNECTION STRING HERE"; SmsClient smsClient = new SmsClient(connectionString); // ... } }
解决思路
- 使用强命名程序集的完整名称:CSX脚本的
#r指令不支持直接用NuGet包名,需要指定程序集的完整强名称。获取方式:- 在本地.NET 6项目中安装
Azure.Communication.Sms包,找到包目录下的Azure.Communication.Sms.dll - 用
sn -T Azure.Communication.Sms.dll命令获取公钥令牌,拼接出完整引用格式,例如:#r "Azure.Communication.Sms, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
- 在本地.NET 6项目中安装
- 手动上传依赖程序集:
- 从本地项目或NuGet包中提取
Azure.Communication.Sms.dll及其依赖的Azure.Communication.Common.dll、Azure.Core.dll等文件 - 通过Azure门户的Function应用>高级工具>Kudu,进入
site\wwwroot\<你的函数名>\bin目录,上传这些程序集 - 在CSX脚本中用相对路径引用,如
#r "./bin/Azure.Communication.Sms.dll"
- 从本地项目或NuGet包中提取
- 升级Function运行时:将Azure Function的运行时升级到.NET 6,此时可以直接使用
#r "nuget: Azure.Communication.Sms"指令引用NuGet包,无需手动处理程序集,同时获得更好的兼容性 - 确认包版本兼容性:.NET 3.1是旧版LTS,部分新版本
Azure.Communication.Sms可能不再支持该框架。查看NuGet包的依赖说明,降级到支持.NET 3.1的包版本后再尝试引用
内容的提问来源于stack exchange,提问作者user1867382
相关产品推荐
相关产品推荐

