简体   繁体   English

Azure Function 无法连接到 azure ZAC5C74B64B4B8352AF2F181AFFB5AC 数据库。 Package 错误

[英]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();

Packages installed安装的软件包

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.

相关问题 Azure WCF服务无法连接到SQL Azure - Azure WCF service can't connect to SQL Azure 使用 SQL 客户端将时间触发的 Azure Function 与数据库连接 - Connect time-triggered Azure Function with Database using SQL Client 尽管连接字符串正确,但无法连接到服务器上的 Azure 数据库。 1042 错误 - Can't connect to Azure database on Sever although the connection String is right. 1042 error 无法在Linux上使用SQLConnection与Azure SQL连接 - Can't connect with Azure SQL using SQLConnection on Linux Azure数据工厂- Azure function连接ZAC5C74B64B4B835AZ2EF2F181数据库不工作 - Azure data factory- Azure function to connect sql database does not work 无法在C#Azure函数中使用Nuget程序包 - Can't use Nuget package in C# Azure function 无法向 Azure Function 提供 NuGet 包源凭据 - Can't provide NuGet package source credentials to Azure Function 我们可以将 Microsoft Azure Bot 连接到 Azure SQL 数据库并从 DB 读取数据并将其用作机器人的回复吗? - Can we connect Microsoft Azure Bot to Azure SQL Database and read data from the DB and use it as a reply for the Bot? 尝试连接到SQL Azure数据库时发生异常 - Exception when trying to connect to SQL Azure Database 无法从1和1 Windows服务器访问Azure SQL数据库 - Can't access Azure SQL Database from 1&1 Windows server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM