最后,我通过使用 gacutil.exe 在 GAC 中注册所有程序集来使其工作。
我在我的应用程序中使用 .Net framework 4.6.1,并且我已执行以下步骤在 GAC 中安装 Npgsql 版本 5.0.0.0:
-
在新的 Visual Studio 项目中添加所需版本的 Npgsql .Net 数据提供程序的引用。
-
在文件夹中查找并复制此提供程序添加的所有程序集。
-
现在我们需要使用 gacutil.exe 在 GAC 中注册这些程序集。它可以与 Visual Studio 命令提示符一起使用,也可以与 Windows SDK 一起使用。
-
使用具有管理员权限的命令并为每个程序集运行以下命令。
gacutil.exe /i "AssemblyPath/assemblyName.dll"
对于 5.0.0.0 版本,完整的命令列表如下:
gacutil.exe /i Npgsql.dll
gacutil.exe /i Microsoft.Bcl.AsyncInterfaces.dll
gacutil.exe /i System.Buffers.dll
gacutil.exe /i System.Memory.dll
gacutil.exe /i System.Numerics.Vectors.dll
gacutil.exe /i System.Runtime.CompilerServices.Unsafe.dll
gacutil.exe /i System.Text.Encodings.Web.dll
gacutil.exe /i System.Text.Json.dll
gacutil.exe /i System.Threading.Channels.dll
gacutil.exe /i System.Threading.Tasks.Extensions.dll
gacutil.exe /i System.ValueTuple.dll
- 现在在 machine.config 文件中的两个位置添加以下条目
位置 1: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config
位置 2: C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config
配置条目:
<system.data>
<DbProviderFactories><add name="Npgsql Data Provider" invariant="Npgsql" description=".NET Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql, Version=5.0.0.0, Culture=neutral, PublicKeyToken=5D8B90D52F46FDA7"/></DbProviderFactories>
</system.data>
- 在当前版本 5.0.0.0 中,Npgsql 试图找出低版本的依赖程序集。我认为这是一个错误,他们可能会在以后的版本中解决这个问题,但现在我们需要在我们的应用程序 App.confg 文件中创建一些依赖程序集的条目来使用这些版本。
YourApplication.exe.config 可以在安装目录中找到,您需要在<assemblyBinding> 部分下添加以下行:
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.5.0" newVersion="4.0.5.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
<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>
现在我可以轻松地在我的应用程序中获取 DbProviderFactory 实例,而不会出现任何问题。