【发布时间】:2021-05-06 03:06:05
【问题描述】:
我正在尝试通过 Azure 函数应用从我的 sql 服务器运行 SELECT 查询,以便稍后处理从中提取的数据。我对 Azure 还很陌生,所以我可能会遗漏一些东西。我的运行时版本是 ~3。这是一个 C# HTTP 触发函数。
我的代码完全基于这篇博文https://randypaulo.com/2018/12/19/querying-azure-sql-database-using-azure-functions-2-0-to-return-json-data/
这是它中断的部分,在 conn.Open();
try
{
var str = Environment.GetEnvironmentVariable("sqldb_connection");
using(SqlConnection conn =new SqlConnection(str))
{
using(SqlCommand cmd =new SqlCommand())
{
SqlDataReader dataReader;
cmd.CommandText = "SELECT TOP 10 ID FROM CATALOG";
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
conn.Open();
dataReader = cmd.ExecuteReader();
var r = Serialize(dataReader);
json = JsonConvert.SerializeObject(r, Formatting.Indented);
}
}
}
函数编译成功,出现了大约 10 多个类似这样的警告。
2021-02-01T21:54:58.159 [Warning] warning CS1701: Assuming assembly reference 'System.Data.Common, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' used by 'System.Data.SqlClient' matches identity 'System.Data.Common, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' of 'System.Data.Common', you may need to supply runtime policy
但是,当涉及到实际运行查询时,在 conn.Open();线。我收到一条很长的错误消息。但它说“无法加载 DLL 'sni.dll' 或其依赖项之一:”
2021-02-01T21:57:29.802 [Information] The type initializer for 'System.Data.SqlClient.TdsParser' threw an exception.
2021-02-01T21:57:29.803 [Information] System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.TdsParser' threw an exception.---> System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SNILoadHandle' threw an exception.---> System.DllNotFoundException: Unable to load DLL 'sni.dll' or one of its dependencies: The specified module could not be found. (0x8007007E)at System.Data.SqlClient.SNINativeMethodWrapper.SNIInitialize(IntPtr pmo)at System.Data.SqlClient.SNILoadHandle..ctor()at System.Data.SqlClient.SNILoadHandle..cctor()--- End of inner exception stack trace
我遇到了一些限制,所以我会列出它们:
- 无法使用 Visual Studio/Visual Studio Code。
- 只能在门户上开发
- 无法切换到运行时 ~2
- 无法创建新资源
这是我目前尝试过的
- 改用 Microsoft.Data.SqlClient > 我无法安装软件包。 (包参考安装成功,但编译时显示为缺失)
- 创建了一个 function.proj 文件并列出了包引用 > 它运行和恢复包,但我的 run.csx 似乎没有使用它们。
- 使用 dotnet add package System.Data.SqlClient 删除和添加包
什么有效:
- 连接字符串 > 我在控制台中使用 sqlcmd 成功运行了查询。
我的问题:
-
有没有办法将 sni.dll 从 \shared\Microsoft.AspNetCore.App\2.2.14 文件夹复制到 \shared\Microsoft.AspNetCore.App\3.1.11 文件夹? (通过控制台不成功,禁止)
-
如何通过 Azure 门户引用/安装自定义或特定库? (如 #r "Newtonsoft.Json" 或 #load "file.csx")
-
是否需要提及我在 run.csx 文件中创建的 function.proj 文件才能正常工作?怎么样?
【问题讨论】:
-
您好,欢迎您。您能否复制一段您自己的代码并将其放入问题中。因为它基于链接中的代码,但它不一样,因此我们无法正确分析问题。至少编写您尝试运行的 SQL 部分。
-
我编辑了它!希望它有帮助我没有修改它很多。我还尝试打开 cmd.Connection.Open() 之类的通信以获得相同的结果。
-
请写下您的系统规格。视窗 10? 8?7?您需要检查 net core 3 安装是否可能重新安装?问题可能与 .net core 3 的先决条件有关
-
也可以看看这个讨论:github.com/Azure/app-service-announcements-discussions/issues/9 可能对你有用
-
非常感谢您的帮助!我最终将应用程序设置“FUNCTION_EXTENSION_VERSION”切换为 2 而不是 3。这将函数运行时设置更改为 ~2。现在功能正常工作了!
标签: c# .net azure system.data.sqlclient