You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法打开NpgsqlConnection:System.Numerics.Vectors程序集加载失败求助

Npgsql连接PostgreSQL时System.Numerics.Vectors依赖问题

我正在开发一款C#应用,使用Npgsql从PostgreSQL数据库获取数据,但尝试打开数据库连接时遇到错误。

使用的代码

public static DataTable FetchMasterData()
{
    NpgsqlConnection npgsqlConn = new NpgsqlConnection(Connection.ConnectionStringPostgreSQL);

    DataTable classMasterTbl = new DataTable();
    string sqlQuery = string.Empty;
    NpgsqlCommand npgsqlCommand = null;

    try
    {
        sqlQuery = @"SELECT * FROM ClassMaster";
        npgsqlCommand = new NpgsqlCommand(sqlQuery, npgsqlConn);

        npgsqlConn.Open(); // Getting Error here

        NpgsqlDataAdapter dataAdapter = new NpgsqlDataAdapter(npgsqlCommand);
        dataAdapter.Fill(classMasterTbl);

        return classMasterTbl;
    }
    catch (Exception dalExce)
    {
        // Handle exception
    }
    finally
    {
        npgsqlCommand?.Dispose();
        npgsqlConn.Close();
    }
}

错误信息

尝试打开连接时,收到如下错误:

Could not load file or assembly 'System.Numerics.Vectors, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

相关配置文件

App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
      </dependentAssembly>     
    </assemblyBinding>
  </runtime>
</configuration>

Packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>      
  <package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net461" />
</packages>

.csproj引用

<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
  <HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>

环境信息

  • Npgsql版本:4.1.14.0
  • .NET版本:4.6.1
  • 已尝试操作:清理并重新生成解决方案

问题

  1. 为什么System.Numerics.Vectors程序集无法被找到?
  2. 如何解决这个依赖问题?

解决方案

问题原因

  1. 版本不匹配:Npgsql 4.1.14.0依赖的是System.Numerics.Vectors 4.1.3.0,但当前安装的是4.5.0版本,虽配置了绑定重定向,但规则可能未覆盖请求版本或未生效。
  2. 引用路径与输出问题:.csproj中HintPath指向net46目录的dll,而项目目标框架是net461;且可能该dll未被复制到应用输出目录,导致运行时找不到。
  3. 绑定重定向未生效:手动配置的重定向规则可能未被正确识别,或NuGet包缓存导致依赖版本混乱。

修复步骤

  1. 调整绑定重定向规则
    确保App.config中的重定向规则覆盖Npgsql请求的4.1.3.0版本,或开启自动生成重定向:

    <dependentAssembly>
      <assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
    </dependentAssembly>
    

    也可右键项目→属性→应用程序→勾选自动生成绑定重定向,让系统自动生成适配的规则。

  2. 确保程序集复制到输出目录
    右键项目中的System.Numerics.Vectors引用→属性,将复制本地设为True;检查输出目录(如bin/Debug)是否存在该dll,若缺失手动从packages/System.Numerics.Vectors.4.5.0/lib/net461(或net46)目录复制。

  3. 对齐依赖版本

    • 降级System.Numerics.Vectors到Npgsql依赖的4.1.3.0版本:
      Install-Package System.Numerics.Vectors -Version 4.1.3.0
      
    • 或升级Npgsql到更高版本(如5.x/6.x),新版本对System.Numerics.Vectors的依赖更兼容4.5.0版本(注意升级后需适配新API)。
  4. 清理NuGet缓存并还原
    执行命令清理缓存:

    nuget locals all -clear
    

    右键解决方案→还原NuGet包,确保所有依赖正确下载引用。

内容的提问来源于stack exchange,提问作者PPB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 01:21:02