Azure函数应用连接到数据库无法工作

huangapple go评论65阅读模式
英文:

Azure Function App connected to database not working

问题

我是相对新手,已经按照几个教程的步骤来实现我的目标,但是我无法让它工作。例如,我按照这个教程操作:
https://www.sqlshack.com/azure-functions-for-azure-sql-database/

在我看到的所有教程中,事情都正常运行,但在我的情况下,却不行。

我遇到的所有错误都与对 "System.Data.SqlClient" 的引用有关:

错误 CS1069:在命名空间 "System.Data.SqlClient" 中找不到类型名称 "SqlConnection"。此类型已被转发到程序集 "System.Data.SqlClient,Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"。考虑添加对该程序集的引用。

根据我理解的情况,这是因为缺少 NuGet 包的原因,但我不知道如何将其集成到 Azure 门户中。此外,根据我了解,它应该自动集成。

我该怎么办?谢谢。

英文:

I am relatively new to all this and I have followed several tutorials to be able to carry out the objective of my title, but I can't get it to work. For example, I followed this tutorial:
https://www.sqlshack.com/azure-functions-for-azure-sql-database/

and in all the tutorials that I see, things work, but in my case it doesn't.

All the errors I get have to do with the reference to "System.Data.SqlClient"

error CS1069: The type name 'SqlConnection' could not be found in the namespace 'System.Data.SqlClient'. This type has been forwarded to assembly 'System.Data.SqlClient, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Consider adding a reference to that assembly.

From what I understand, it's because of the missing NuGet package, but I have no idea how to integrate it into the Azure portal, also, from what I understand, it should be integrated automatically.
What can I do? Thank you.

答案1

得分: 1

  • Azure Function并不真正提供对NuGet包的无缝支持,如果使用门户开发函数。

  • 因此,我建议您使用Visual Studio,您可以通过Visual Studio内置的包管理器支持轻松安装NuGet包。

  • 要执行此操作,一旦您在Visual Studio中创建了基本函数,请在“工具”选项卡下,然后在“NuGet包管理器”选项下,单击“管理解决方案的NuGet包”。

Azure函数应用连接到数据库无法工作

  • 这将打开一个新窗口,您可以在包管理器中搜索。只需在窗口中搜索“System.Data.SqlClient”,然后选择项目名称并安装。

Azure函数应用连接到数据库无法工作

  • 这里我的代码检查连接是否建立:
string connstring = "";
SqlConnection conn = new SqlConnection(connstring);
conn.Open();
if (conn.State != System.Data.ConnectionState.Closed)
{
	log.LogInformation("C# HTTP触发函数已处理请求。");
	return new OkObjectResult("已连接");
}            
conn.Close();
return new OkObjectResult("等待中");
  • 现在,我们可以从Visual Studio将函数部署到Azure,只需在解决方案资源管理器中右键单击项目名称,然后选择“发布”。

Azure函数应用连接到数据库无法工作

  • 弹出一个窗口,您需要选择函数应用的类型,然后要么创建一个函数应用,要么选择现有的函数应用,然后部署。

Azure函数应用连接到数据库无法工作

  • 另一种方法是上传一个名为“function.proj”的文件,其中包含对NuGet包的引用。可以参考Hasan Savran编写的教程
英文:
  • Azure function doesn't really provide seamless support for installation of the Nuget packages if the function is developed using portal

  • So I would suggest you use visual Studio where you can easily install nuget packages through the inbuild support for the package manager in the visual studio

  • To do this once you have created the basic function in visual studio go under tools tab then under the NuGet Package Manager option click on Manage NuGet Pacakges for Solution.

Azure函数应用连接到数据库无法工作

  • This will open a new window where you can search on the package manager . Just search for System.Data.SqlClient in the window select the name of the project and install .

Azure函数应用连接到数据库无法工作

  • Here my code checks whether the connection is established or not
string connstring = "";
SqlConnection conn = new SqlConnection(connstring);
conn.Open();
if (conn.State != System.Data.ConnectionState.Closed)
{
	log.LogInformation("C# HTTP trigger function processed a request.");
	return new OkObjectResult("Connected");
}            
conn.Close();
return new OkObjectResult("Waiting");
  • Now we can deploy the function from visual studio to azure by right clicking on the name of the project in the solution explore and select publish

Azure函数应用连接到数据库无法工作

The a pop will appear where you have to select the type of function app and then either create a function app or select the existing ones then deploy.

Azure函数应用连接到数据库无法工作

  • Another approach would be to upload a function.proj file which will have reference to the NuGet package . Refer this tutorial for it which is written by Hasan Savran

huangapple
  • 本文由 发表于 2023年2月24日 06:23:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定