环境: .NET 4.0, MQ .NET客户端 IBM.XMS(v2.0.0.3)
测试代码如下:
var factoryFactory = XMSFactoryFactory.GetInstance(XMSC.CT_WMQ); var _connFactory = factoryFactory.CreateConnectionFactory(); _connFactory.SetStringProperty(XMSC.WMQ_HOST_NAME, "192.168.0.65"); _connFactory.SetIntProperty(XMSC.WMQ_PORT, 1414); _connFactory.SetStringProperty(XMSC.WMQ_QUEUE_MANAGER, "XPP_QM"); _connFactory.SetIntProperty(XMSC.WMQ_CONNECTION_MODE, XMSC.WMQ_CM_CLIENT); _connFactory.SetIntProperty(XMSC.WMQ_BROKER_VERSION, XMSC.WMQ_BROKER_V1); _connFactory.SetStringProperty(XMSC.CLIENT_ID, string.Empty); var conn = _connFactory.CreateConnection(); conn.Start();
错误为:
IBM.XMS.XMSException: CWSMQ0006E: 调用方法 ConnectionFactory.CreateConnection 期间接收到了异常:CompCode: 2, Reason: 2538
。 在执行指定的方法期间,另一个组件抛出了异常。 有关更多信息,请参阅链接异常。
在 IBM.XMS.Client.WMQ.WmqConnectionFactory.CreateProviderConnection(XmsPropertyContext connectionProps)
在 IBM.XMS.Client.Impl.XmsConnectionFactoryImpl.CreateConnection(String userID, String password)
在 IBM.XMS.Client.Impl.XmsConnectionFactoryImpl.CreateConnection()
在 XRisk.MQ.MQConnection.Open(Boolean needLog) 位置 e:xRisk4-srcXRisk.MQMQConnection.cs:行号 314
Linked Exception : CompCode: 2, Reason: 2538
真正的异常是下面的代码中报的。
IBM.WMQ.MQTCPConnection ---- < ParseLocalAddress(String) exit [o] rc=OK IBM.WMQ.MQTCPConnection --- d Exception in method ConnectSocket(string,string,MQLONG) IBM.WMQ.MQTCPConnection --- X System.Net.Sockets.SocketException (0x80004005): The requested name is valid and was found in the database, but it does not have the correct associated data being resolved for at System.Net.Dns.InternalGetHostByAddress(IPAddress address, Boolean includeIPv6) at System.Net.Dns.GetHostEntry(String hostNameOrAddress) at IBM.WMQ.MQTCPConnection.ConnectSocket(String localAddr, String connectionName, Int32 options) MQException CompCode: 2 Reason: 2538
真实的原因是在.NET 4.0中,微软改变了接口 Dns.GetHostEntry 的行为。当直接使用IP作为参数时就会变报,如果使用机器名或域名就不会报告。
解决方法如下
1. 使用 IPAddress.TryParse()或Dns.Resolve(string) 来代替Dns.GetHostEntry 。
2. 如果使用了第三方的库,不能修改代码,则可以把IP用机器名或域名代码。如上面的代码可以改成。
var factoryFactory = XMSFactoryFactory.GetInstance(XMSC.CT_WMQ); var _connFactory = factoryFactory.CreateConnectionFactory(); _connFactory.SetStringProperty(XMSC.WMQ_HOST_NAME, "hostname"); _connFactory.SetIntProperty(XMSC.WMQ_PORT, 1414); _connFactory.SetStringProperty(XMSC.WMQ_QUEUE_MANAGER, "XPP_QM"); _connFactory.SetIntProperty(XMSC.WMQ_CONNECTION_MODE, XMSC.WMQ_CM_CLIENT); _connFactory.SetIntProperty(XMSC.WMQ_BROKER_VERSION, XMSC.WMQ_BROKER_V1); _connFactory.SetStringProperty(XMSC.CLIENT_ID, string.Empty); var conn = _connFactory.CreateConnection(); conn.Start();
参考: