我的计算机上安装了ODAC 12.1。源代码使用的是版本12.2(更新)。
在运行时我得到InvalidCastException
:
System.InvalidCastException: [A] Oracle.DataAccess.Client.OracleCommand无法强制转换为 [B] Oracle.DataAccess.Client.OracleCommand。类型A源自 'Oracle.DataAccess,版本= 4.121.2.0,区域性=中性, 位置“默认”上下文中的PublicKeyToken = 89b483f429c47342” 'C:\ WINDOWS \ Microsoft.Net \ assembly \ GAC_64 \ Oracle.DataAccess \ v4.0_4.121.2.0__89b483f429c47342 \ Oracle.DataAccess.dll'。 类型B源自'Oracle.DataAccess,Version = 4.122.1.0, 文化=中性,PublicKeyToken = 89b483f429c47342'在上下文中 位置为“默认” 'C:\ code \ Windows Application \ bin \ Debug \ Oracle.DataAccess.dll”。
如何指定应用程序应使用哪个Oracle版本?
答案 0 :(得分:1)
但是,我认为问题可能是其他原因。检查您的GAC,我的样子如下:
c:\Temp>gacutil /l | findstr /i Oracle.DataAccess, | findstr Version=4.12 | sort
Oracle.DataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342, processorArchitecture=AMD64
Oracle.DataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342, processorArchitecture=x86
Policy.4.112.Oracle.DataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342, processorArchitecture=AMD64
Policy.4.112.Oracle.DataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342, processorArchitecture=x86
Policy.4.121.Oracle.DataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342, processorArchitecture=AMD64
Policy.4.121.Oracle.DataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342, processorArchitecture=x86
发布者政策文件Policy.4.121.Oracle.DataAccess.config
是这个
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Oracle.DataAccess" publicKeyToken="89B483F429C47342"/>
<bindingRedirect oldVersion="4.121.0.0-4.121.2.0" newVersion="4.121.2.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
如果您有一个要求Oracle.DataAccess, Version=4.122.1.0
的应用程序,则Windows不知道要从GAC加载哪个版本(因为它最多覆盖4.121.2.0),因此很可能它将加载您复制到的DLL。应用程序目录。然后您会遇到版本冲突。
解决方案1:
获取Oracle.DataAccess.dll
的“旧”版本(甚至可以早于4.121.2.0,但必须为4.xxx)并将其复制到您的开发机器上-仅使用单个DLL,不要安装任何DLL。其他Oracle客户端。
请参考此旧DLL。发布者策略将自动重定向到您计算机上安装的版本-除非您强制使用特定版本,请参见How to load specific version of assembly from GAC
解决方案2:
上面我写了“发布者策略将自动重定向...”,但这意味着发布者策略已正确安装!
当我上次安装Oracle 12.2客户端时,Oracle.DataAccess
没有放入GAC,也没有添加发布者策略。这似乎是Oracle Universal Installer中的错误。您可以使用此脚本手动添加它们,选择所需的版本和体系结构:
SETLOCAL
set Oracle_x64=c:\oracle\product\12.1\Client_x64\ODP.NET
set Oracle_x86=c:\oracle\product\12.1\Client_x86\ODP.NET
set OraProvCfg_x64=%Oracle_x64%\bin\4\OraProvCfg.exe
set OraProvCfg_x86=%Oracle_x86%\bin\4\OraProvCfg.exe
IF EXIST "%Oracle_x64%\managed\common\Oracle.ManagedDataAccess.dll" "%OraProvCfg_x64%" /action:gac /providerpath:%Oracle_x64%\managed\common\Oracle.ManagedDataAccess.dll
IF EXIST "%Oracle_x64%\managed\PublisherPolicy\4\Policy.4.121.Oracle.ManagedDataAccess.dll" "%OraProvCfg_x64%" /action:gac /providerpath:%Oracle_x64%\managed\PublisherPolicy\4\Policy.4.121.Oracle.ManagedDataAccess.dll
IF EXIST "%Oracle_x64%\managed\PublisherPolicy\4\Policy.4.122.Oracle.ManagedDataAccess.dll" "%OraProvCfg_x64%" /action:gac /providerpath:%Oracle_x64%\managed\PublisherPolicy\4\Policy.4.122.Oracle.ManagedDataAccess.dll
IF EXIST "%Oracle_x64%\bin\2.x\Oracle.DataAccess.dll" "%OraProvCfg_x64%" /action:gac /providerpath:%Oracle_x64%\bin\2.x\Oracle.DataAccess.dll
IF EXIST "%Oracle_x64%\PublisherPolicy\2.x\Policy.2.102.Oracle.DataAccess.dll" "%OraProvCfg_x64%" /action:gac /providerpath:%Oracle_x64%\PublisherPolicy\2.x\Policy.2.102.Oracle.DataAccess.dll
IF EXIST "%Oracle_x64%\PublisherPolicy\2.x\Policy.2.111.Oracle.DataAccess.dll" "%OraProvCfg_x64%" /action:gac /providerpath:%Oracle_x64%\PublisherPolicy\2.x\Policy.2.111.Oracle.DataAccess.dll
IF EXIST "%Oracle_x64%\PublisherPolicy\2.x\Policy.2.112.Oracle.DataAccess.dll" "%OraProvCfg_x64%" /action:gac /providerpath:%Oracle_x64%\PublisherPolicy\2.x\Policy.2.112.Oracle.DataAccess.dll
IF EXIST "%Oracle_x64%\PublisherPolicy\2.x\Policy.2.121.Oracle.DataAccess.dll" "%OraProvCfg_x64%" /action:gac /providerpath:%Oracle_x64%\PublisherPolicy\2.x\Policy.2.121.Oracle.DataAccess.dll
IF EXIST "%Oracle_x64%\PublisherPolicy\2.x\Policy.2.122.Oracle.DataAccess.dll" "%OraProvCfg_x64%" /action:gac /providerpath:%Oracle_x64%\PublisherPolicy\2.x\Policy.2.122.Oracle.DataAccess.dll
IF EXIST "%Oracle_x64%\bin\4\Oracle.DataAccess.dll" "%OraProvCfg_x64%" /action:gac /providerpath:%Oracle_x64%\bin\4\Oracle.DataAccess.dll
IF EXIST "%Oracle_x64%\PublisherPolicy\4\Policy.4.112.Oracle.DataAccess.dll" "%OraProvCfg_x64%" /action:gac /providerpath:%Oracle_x64%\PublisherPolicy\4\Policy.4.112.Oracle.DataAccess.dll
IF EXIST "%Oracle_x64%\PublisherPolicy\4\Policy.4.121.Oracle.DataAccess.dll" "%OraProvCfg_x64%" /action:gac /providerpath:%Oracle_x64%\PublisherPolicy\4\Policy.4.121.Oracle.DataAccess.dll
IF EXIST "%Oracle_x64%\PublisherPolicy\4\Policy.4.122.Oracle.DataAccess.dll" "%OraProvCfg_x64%" /action:gac /providerpath:%Oracle_x64%\PublisherPolicy\4\Policy.4.122.Oracle.DataAccess.dll
IF EXIST "%Oracle_x86%\bin\2.x\Oracle.DataAccess.dll" "%OraProvCfg_x86%" /action:gac /providerpath:%Oracle_x86%\bin\2.x\Oracle.DataAccess.dll
IF EXIST "%Oracle_x86%\PublisherPolicy\2.x\Policy.2.102.Oracle.DataAccess.dll" "%OraProvCfg_x86%" /action:gac /providerpath:%Oracle_x86%\PublisherPolicy\2.x\Policy.2.102.Oracle.DataAccess.dll
IF EXIST "%Oracle_x86%\PublisherPolicy\2.x\Policy.2.111.Oracle.DataAccess.dll" "%OraProvCfg_x86%" /action:gac /providerpath:%Oracle_x86%\PublisherPolicy\2.x\Policy.2.111.Oracle.DataAccess.dll
IF EXIST "%Oracle_x86%\PublisherPolicy\2.x\Policy.2.112.Oracle.DataAccess.dll" "%OraProvCfg_x86%" /action:gac /providerpath:%Oracle_x86%\PublisherPolicy\2.x\Policy.2.112.Oracle.DataAccess.dll
IF EXIST "%Oracle_x86%\PublisherPolicy\2.x\Policy.2.121.Oracle.DataAccess.dll" "%OraProvCfg_x86%" /action:gac /providerpath:%Oracle_x86%\PublisherPolicy\2.x\Policy.2.121.Oracle.DataAccess.dll
IF EXIST "%Oracle_x86%\PublisherPolicy\2.x\Policy.2.122.Oracle.DataAccess.dll" "%OraProvCfg_x86%" /action:gac /providerpath:%Oracle_x86%\PublisherPolicy\2.x\Policy.2.122.Oracle.DataAccess.dll
IF EXIST "%Oracle_x86%\bin\4\Oracle.DataAccess.dll" "%OraProvCfg_x86%" /action:gac /providerpath:%Oracle_x86%\bin\4\Oracle.DataAccess.dll
IF EXIST "%Oracle_x86%\PublisherPolicy\4\Policy.4.112.Oracle.DataAccess.dll" "%OraProvCfg_x86%" /action:gac /providerpath:%Oracle_x86%\PublisherPolicy\4\Policy.4.112.Oracle.DataAccess.dll
IF EXIST "%Oracle_x86%\PublisherPolicy\4\Policy.4.121.Oracle.DataAccess.dll" "%OraProvCfg_x86%" /action:gac /providerpath:%Oracle_x86%\PublisherPolicy\4\Policy.4.121.Oracle.DataAccess.dll
IF EXIST "%Oracle_x86%\PublisherPolicy\4\Policy.4.122.Oracle.DataAccess.dll" "%OraProvCfg_x86%" /action:gac /providerpath:%Oracle_x86%\PublisherPolicy\4\Policy.4.122.Oracle.DataAccess.dll
当然,您也可以根据需要使用gacutil.exe
。