问题描述
尝试在Azure门户中运行此Azure功能,但失败,出现上述标题错误:
Trying to run this Azure Function in Azure portal but fails with above title error:
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.Data.SqlClient;
public static string Run(HttpRequest req, ILogger log)
{
string name="dbconn";
string conStr = System.Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process);
if (string.IsNullOrEmpty(conStr)) // Azure Functions App Service naming convention
conStr = System.Environment.GetEnvironmentVariable($"SQLCONNSTR_{name}", EnvironmentVariableTarget.Process);
using (SqlConnection conn = new SqlConnection(conStr))
{
conn.Open();
}
return conStr;
我已经在AzureSQL数据库中添加了ADO.NET ConnectionString:Google搜索显示此问题主要是针对System.Data.SqlClient本地发生的.但是我遇到的问题是azure门户中的主机,我不是从VS发布的,所以不确定如何解决此问题.非常感谢您的帮助.
I've added in the AzureSQL database ADO.NET ConnectionString:Google search shows this issue mostly happening for local in regards to System.Data.SqlClient. But the issue I have is at host in azure's portal, i'm not publishing from VS, so not sure how to fix this. Help really appreciated.
如果我还尝试将System.Data.SqlClient更改为Microsoft.Data.SqlClient,但无法编译:类型或名称空间名称"Data"在名称空间"Microsoft"中不存在(您是否缺少程序集)参考?)
In case I also tried to change System.Data.SqlClient for Microsoft.Data.SqlClient but can't compile: The type or namespace name 'Data' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
在Azure cli中,我可以看到.net核心为v.2.2.402,不确定是否更新到3.1可能是问题所在?我可以在Windows 10 pc上更新到.Net Core 3.1,但是Azure cli继续显示.net core 2.2.402.我在 stackoverflow 中发布了问题,同时询问如何更新天青的环境.
In Azure cli I can see .net core is v 2.2.402 and not sure if updating to 3.1 could this be the issue? I can update to .Net Core 3.1 on windows 10 pc but Azure cli continues to show .net core 2.2.402. I posted question in stackoverflow question how to also update the azure environment.
非常感谢您的帮助,欢呼!
Thanks a bunch for your help, cheers!
推荐答案
连接字符串似乎有问题.请检查您是否正在使用与此类似的东西:
It looks like something is wrong with the connection string. Please check that you are you using something similar to this:
Server=tcp:{your_server}.database.windows.net,1433;Initial Catalog={your_database};Persist Security Info=False;User ID={your_user};Password={your_password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
对于使用Microsoft.Data.SqlClient(推荐的库)的问题,您需要先将其添加到解决方案中:
For what concern using Microsoft.Data.SqlClient, which is the recommended library, you need to add it to the solution first:
dotnet add package Microsoft.Net.SqlClient
这篇关于Azure函数:无法加载DLL'sni.dll'或其依赖项之一:找不到指定的模块.(0x8007007E)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!