Oracle 托管数据访问在使用别名时似乎忽略端口

问题描述

我收到以下错误

ExceptionMessage: "ORA-12541: TNS: No listener"
ExceptionType: "Oracle.ManagedDataAccess.Client.OracleException"
InnerException: {Message: "An error has occurred.",ExceptionMessage: "ORA-12541: TNS: No listener",…}
ExceptionMessage: "ORA-12541: TNS: No listener"
ExceptionType: "OracleInternal.Network.NetworkException"
InnerException: {Message: "An error has occurred.",…}
ExceptionMessage: "No connection Could be made because the target machine actively refused it {internal_ip}:1521"
ExceptionType: "System.Net.sockets.socketException"
Message: "An error has occurred."
StackTrace: "   at System.Net.sockets.socket.InternalEndConnect(IAsyncResult asyncResult)
↵   at System.Net.sockets.socket.EndConnect(IAsyncResult asyncResult)
↵   at OracleInternal.Network.TcpTransportAdapter.ConnectIterate()"
Message: "An error has occurred."
StackTrace: "   at OracleInternal.Network.OracleCommunication.DoConnect(String tnsDescriptor)
↵   at OracleInternal.ServiceObjects.OracleConnectionImpl.Connect(ConnectionString cs,Boolean bOpenEndUserSession,OracleConnection connRefForCriteria,String instanceName)"
Message: "An error has occurred."
StackTrace: "   at OracleInternal.ConnectionPool.PoolManager`3.Get(ConnectionString csWithDiffOrNewPwd,Boolean bGetForApp,String affinityInstanceName,Boolean bForceMatch)
↵   at OracleInternal.ConnectionPool.OraclePoolManager.Get(ConnectionString csWithNewPassword,Boolean bForceMatch)
↵   at OracleInternal.ConnectionPool.OracleConnectiondispenser`3.Get(ConnectionString cs,PM conPM,ConnectionString pmCS,securestring securedPassword,securestring securedProxyPassword,OracleConnection connRefForCriteria)
↵   at Oracle.ManagedDataAccess.Client.OracleConnection.open()

在我的配置文件中使用以下别名。请注意,配置文件中的端口是 1522,但错误引用了端口 1521。

<oracle.manageddataaccess.client>
  <version number="*">
    <dataSources>
      <dataSource alias="ConnectionOne" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST={host})(PORT=1522))(CONNECT_DATA=(SERVICE_NAME={service_name}))) " />
    </dataSources>
  </version>
</oracle.manageddataaccess.client>

更改连接字符串

<connectionStrings>
    <add name="ConnectionOne"
       connectionString="user id = ; password = ; data source = ConnectionOne"/>
</connectionStrings>

<connectionStrings>
    <add name="ConnectionOne"
       connectionString="user id = ; password = ; data source=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST={host})(PORT=1522))(CONNECT_DATA=(SERVICE_NAME={service_name})))"/>
</connectionStrings>

解决问题。

来自 Oracle 文档 (https://docs.oracle.com/cd/E63277_01/win.121/e63268/InstallManagedConfig.htm#ODPNT8161)

按照以下优先顺序解析数据源 在连接字符串的数据源属性中指定的别名。

  1. .NET 配置文件 部分下的 dataSources 部分中的数据源别名。
  2. tnsnames.ora 文件中的数据源别名,位于 .NET 配置文件中 TNS_ADMIN 指定的位置。位置可以包括 绝对或相对目录路径
  3. tnsnames.ora 文件中的数据源别名与 .exe 位于同一目录中。

所以,我不认为它是从其他位置读取的。

有人知道是什么原因造成的吗?

解决方法

我发现 TNS_ADMIN 系统变量引起了我所有与您类似的头痛。根据情况添加或删除解决问题。