我正在 .NET 中开发一个 Azure Function,它需要连接到共享托管提供商(不是 Azure SQL 数据库)上外部托管的 SQL Server 数据库。我已经遵循了几个教程,但在建立与数据库的连接时遇到了问题。
这是我到目前为止所做的:
使用 .NET 运行时堆栈在 Azure 门户中创建了 Azure Function App。
添加了一个名为 HttpTriggerWithSql 的新 HTTP 触发函数。
在 Function App 的设置中配置 SQL Server 连接字符串:
名称:SqlConnectionString
值:服务器=myserver.sharedhosting.com;数据库=mydatabase;用户ID=myusername;密码=mypassword;
string connectionString = Environment.GetEnvironmentVariable("SqlConnectionString", EnvironmentVariableTarget.Process);
Implemented the database connection logic:
using (SqlConnection conn = new SqlConnection(connectionString))
{
await conn.OpenAsync();
string query = "SELECT TOP 1 * FROM MyTable";
using (SqlCommand cmd = new SqlCommand(query, conn))
{
SqlDataReader reader = await cmd.ExecuteReaderAsync();
// Process data...
}
}
问题:
当我运行该函数时,出现以下错误:
2024-10-16T20:59:07Z [信息] 执行“Functions.HttpTrigger1”(原因=“此函数是通过主机 API 以编程方式调用的。”,Id=ba7ed57f-67b8-40d3-88d4-ba2567c74bea) 2024-10-16T20:59:07Z [Error] 函数编译错误 2024-10-16T20:59:07Z [错误] run.csx(7,42):错误CS0234:命名空间“Microsoft.Azure.WebJobs.Extensions”中不存在类型或命名空间名称“Http”(您是吗?缺少程序集引用?) 2024-10-16T20:59:07Z [Error] run.csx(15,10): error CS0246: 找不到类型或命名空间名称“HttpTriggerAttribute”(您是否缺少 using 指令或程序集引用?) 2024-10-16T20:59:07Z [Error] run.csx(15,10): error CS0246: 找不到类型或命名空间名称“HttpTrigger”(您是否缺少 using 指令或程序集引用?) 2024-10-16T20:59:07Z [错误] run.csx(15,22):错误CS0103:当前上下文中不存在名称“AuthorizationLevel” 2024-10-16T20:59:07Z [Error] run.csx(15,66): error CS0246: 找不到类型或命名空间名称“Route”(您是否缺少 using 指令或程序集引用?) 2024-10-16T20:59:07Z [错误] run.csx(25,20):错误CS1069:在命名空间“System.Data.SqlClient”中找不到类型名称“SqlConnection”。此类型已转发到程序集“System.Data.SqlClient,Version=0.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”考虑添加对该程序集的引用。 2024-10-16T20:59:07Z [错误] run.csx(25,45):错误CS1069:在命名空间“System.Data.SqlClient”中找不到类型名称“SqlConnection”。此类型已转发到程序集“System.Data.SqlClient,Version=0.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”考虑添加对该程序集的引用。 2024-10-16T20:59:07Z [错误] run.csx(31,24):错误CS1069:在命名空间“System.Data.SqlClient”中找不到类型名称“SqlCommand”。此类型已转发到程序集“System.Data.SqlClient,Version=0.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”考虑添加对该程序集的引用。 2024-10-16T20:59:07Z [错误] run.csx(31,45):错误CS1069:在命名空间“System.Data.SqlClient”中找不到类型名称“SqlCommand”。此类型已转发到程序集“System.Data.SqlClient,Version=0.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”考虑添加对该程序集的引用。 2024-10-16T20:59:07Z [错误] run.csx(33,21):错误CS1069:在命名空间“System.Data.SqlClient”中找不到类型名称“SqlDataReader”。此类型已转发到程序集“System.Data.SqlClient,Version=0.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”考虑添加对该程序集的引用。 2024-10-16T20:59:07Z [错误] 执行“Functions.HttpTrigger1”(失败,Id=ba7ed57f-67b8-40d3-88d4-ba2567c74bea,持续时间=4ms)
要解决这些错误,您必须在函数项目中安装以下 NuGet 包,如评论中提到的 @Kevin 。
.csproj:
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.5.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.6" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Sql" Version="3.0.534" />
</ItemGroup>
在
function.cs
中包含以下命名空间:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Data.SqlClient;
我能够运行您的代码,没有任何错误:
使用以下代码将 Azure Function 连接到 SQL Server 数据库并获取数据:
代码片段:
[FunctionName("GetToDoItem")]
public static IActionResult Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "gettodoitem")] HttpRequest req,
[Sql(commandText: "select [Name], [CreatedAt] from dbo.table1 where Id = @Id",
commandType: System.Data.CommandType.Text,
parameters: "@Id={Query.Id}",
connectionStringSetting: "SqlConnectionString")]
IEnumerable<ToDoItem> toDoItem)
{
var todoItem = toDoItem.FirstOrDefault();
if (todoItem == null)
{
return new NotFoundResult();
}
return new OkObjectResult(todoItem);
}
回复:
Functions:
GetToDoItem: [GET] http://localhost:7157/api/gettodoitem
For detailed output, run func with --verbose flag.
[2024-10-21T10:22:50.800Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
[2024-10-21T10:22:53.403Z] Executing 'Functions.GetToDoItem' (Reason='This function was programmatically called via the host APIs.', Id=4a6d4d60-bfdc-40e1-aff7-e760891184b6)
[2024-10-21T10:23:05.198Z] Executing OkObjectResult, writing value of type 'FunctionApp6.ToDoItem'.
[2024-10-21T10:23:05.275Z] Executed 'Functions.GetToDoItem' (Succeeded, Id=4a6d4d60-bfdc-40e1-aff7-e760891184b6, Duration=11894ms)