[英]Azure Function can't connect to azure sql database. Package error
I'm trying to connect my azure function to my azure sql database
.我正在尝试将我的 azure function 连接到我的
azure sql database
。 When trying connection.open() it gives the following error.尝试 connection.open() 时,会出现以下错误。
Executed 'ClientID2' (Failed, Id=22ad8465-45f5-4fff-ba90-f0ff7d0ee465, Duration=5895ms) [2021-11-25T10:49:57.575Z] System.Private.CoreLib: Exception while executing function: ClientID2.
执行“ClientID2”(失败,Id=22ad8465-45f5-4fff-ba90-f0ff7d0ee465,持续时间=5895ms)[2021-11-25T10:49:57.575Z] System.Private.CoreLib:执行 ZC1C425268E68385D1AB5074C17A94F1 时出现异常。 Microsoft.Data.SqlClient: The type initializer for 'Microsoft.Data.SqlClient.TdsParser' threw an exception.
Microsoft.Data.SqlClient:“Microsoft.Data.SqlClient.TdsParser”的类型初始化程序引发了异常。 Microsoft.Data.SqlClient: Could not load file or assembly 'System.Text.Encoding.CodePages, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
Microsoft.Data.SqlClient:无法加载文件或程序集“System.Text.Encoding.CodePages,版本=5.0.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a”。
The connection itself runs on .net 5.0 framework, but not in .net core 3.1.连接本身在 .net 5.0 框架上运行,但不在 .net 核心 3.1 上运行。
[FunctionName("ClientID2")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req, ILogger log)
{
string requestBody = string.Empty;
try
{
log.LogInformation("Function process");
using (SqlConnection connection = new SqlConnection(Environment.GetEnvironmentVariable("Connection_Database")))
{
connection.Open();
Can anyone point me in the right direction?谁能指出我正确的方向?
Best regards此致
EDIT:编辑:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
<RootNamespace>Client_ID</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.30" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Http" Version="3.0.12" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="4.0.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
Local.Settings.Json Local.Settings.Json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"Connection_Database": Connection string from azure sql database
}
}
I figured it out, but not sure if it is the correct way.我想通了,但不确定它是否是正确的方法。
Had to put the System.Text.Encoding.CodePages.dll and System.Runtime.CompilerServices.Unsafe.dll directly in bin folder of project.必须将 System.Text.Encoding.CodePages.dll 和 System.Runtime.CompilerServices.Unsafe.dll 直接放在项目的 bin 文件夹中。
Next, according to this answer接下来,根据这个答案
I added to the.csproj the following我在 .csproj 中添加了以下内容
<ItemGroup>
<FunctionsPreservedDependencies Include="System.Text.Encoding.CodePages.dll" />
<FunctionsPreservedDependencies Include="System.Runtime.CompilerServices.Unsafe.dll" />
</ItemGroup>
And then it worked然后它起作用了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.