无法从程序集“System.Data”加载类型“System.Data.SqlClient.SqlConnection”

问题描述 投票:0回答:1

我的 Azure 功能面向 .NET 4.8。当我部署函数并尝试运行它时,出现此错误:

执行函数时出现异常:通知程序无法从程序集“System.Data,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089”加载类型“System.Data.SqlClient.SqlConnection”。

经过调查,我发现运行时正在尝试从

System.Data.SqlClient.SqlConnection
而不是
System.Data
加载
System.Data.SqlClient

为了您的信息,我正在使用 v1 Azure 函数,当我检查我正在部署的包的工件时,它就在那里。你能帮我解答一下我的疑问吗?

我尝试将 Azure 函数代码升级到 v4,但遇到了许多其他错误

.net azure-functions
1个回答
0
投票

要解决此问题,请删除 bin 文件夹,清理并重建项目解决方案。

按照评论中提到的

@Rithwik
手动安装
System.Data.SqlClient
软件包版本4.*.*

NuGet\安装包 System.Data.SqlClient -版本 4.8.6

我在.NET 4.8中创建了一个Http触发的Azure Function v4,并使用以下代码建立了SQL连接。

代码片段:

 [Function("Function2")]
 public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
 {
     _logger.LogInformation("C# HTTP trigger function processed a request.");

     var response = req.CreateResponse(HttpStatusCode.OK);
     response.Headers.Add("Content-Type", "application/json; charset=utf-8");

     try
     {
         using (SqlConnection conn = new SqlConnection(_connectionString))
         {
             await conn.OpenAsync();
             string query = "SELECT * FROM table1";
             using (SqlCommand cmd = new SqlCommand(query, conn))
             {
                 using (SqlDataReader reader = await cmd.ExecuteReaderAsync())
                 {
                     var results = new List<string>();

                     while (await reader.ReadAsync())
                     {
                         var data = reader[1].ToString(); 
                         results.Add(data);
                     }
                     await response.WriteAsJsonAsync(results);
                 }
             }
         }
     }
     catch (SqlException ex)
     {
         _logger.LogError($"SQL Error: {ex.Message}");
         response = req.CreateResponse(HttpStatusCode.InternalServerError);
         await response.WriteStringAsync("An error occurred while accessing the database.");
     }
     catch (Exception ex)
     {
         _logger.LogError($"Error: {ex.Message}");
         response = req.CreateResponse(HttpStatusCode.InternalServerError);
         await response.WriteStringAsync("An unexpected error occurred.");
     }
     return response;
 }

.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net48</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.23.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.4.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.18.1" />
  </ItemGroup>

local.settings.json:

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
    "sqlConnection": "<sql_connection_string>"
  }
}

输出:

enter image description here

Functions:
        Function2: [GET,POST] http://localhost:7273/api/Function2

For detailed output, run func with --verbose flag.
[2024-11-04T13:54:08.045Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
[2024-11-04T13:55:03.191Z] Executing 'Functions.Function2' (Reason='This function was programmatically called via the host APIs.', Id=8623fa74-3XX8-8721-f25968fb7864)
[2024-11-04T13:55:03.401Z] C# HTTP trigger function processed a request.
[2024-11-04T13:55:07.874Z] Executed 'Functions.Function2' (Succeeded, Id=8623fa74-33f8-4608XX25968fb7864, Duration=4702ms)
© www.soinside.com 2019 - 2024. All rights reserved.