英文:
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包”。
- 这将打开一个新窗口,您可以在包管理器中搜索。只需在窗口中搜索“System.Data.SqlClient”,然后选择项目名称并安装。
- 这里我的代码检查连接是否建立:
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,只需在解决方案资源管理器中右键单击项目名称,然后选择“发布”。
- 弹出一个窗口,您需要选择函数应用的类型,然后要么创建一个函数应用,要么选择现有的函数应用,然后部署。
- 另一种方法是上传一个名为“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 theNuGet Package Manager
option click onManage NuGet Pacakges for Solution
.
- 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 .
- 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
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.
- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论