简体   繁体   English

使用C#.Net WCF Rest服务访问IBM MQ消息

[英]Accessing IBM MQ messages using C#.Net WCF Rest service

Unable to retrieve the first response message from IBM MQ. 无法从IBM MQ检索第一条响应消息。

I have designed the IBM MQ connection, message passing and message retrieval in a ASP.NET web application which is working perfectly fine. 我已经在运行良好的ASP.NET Web应用程序中设计了IBM MQ连接,消息传递和消息检索。 When I try to implement the same logic in a WCF REST service, the very first request will always return error in .net as 'MQRC_NO_MSG_AVAIL'. 当我尝试在WCF REST服务中实现相同的逻辑时,第一个请求将始终在.net中以“ MQRC_NO_MSG_AVAIL”形式返回错误。 However when I will pass the second request I will receive the response for first request The error in IBM MQ for first request is 但是,当我传递第二个请求时,我将收到第一个请求的响应。IBM MQ中第一个请求的错误是

Message . 信息 。 . . . : Error on receive from host 10 (XXX.XXX.XX). :从主机10(XXX.XXX.XX)接收时出错。
Cause . 原因。 . . . . : An error occurred receiving data from 10 (XXX.XXX.XX) over TCP/IP. :通过TCP / IP从10(XXX.XXX.XX)接收数据时发生错误。 This may be due to a communications failure. 这可能是由于通信故障。 Recovery . 恢复。 . . : The return code from the TCP/IP (read) call was 3426 (X'X'00000D62''). :TCP / IP(读取)调用的返回代码为3426(X'X'00000D62'')。 Record these values and tell the systems administrator. 记录这些值并告知系统管理员。

The request openOptions : 请求openOptions:

MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_OUTPUT The response openOptions : MQC.MQOO_INQUIRE + MQC.MQOO_BROWSE + MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_INPUT_SHARED MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_OUTPUT响应openOptions:MQC.MQOO_INQUIRE + MQC.MQOO_BROWSE + MQC.MQOO_FAIL_IF_QUIESCING + MQC.MSHAQOO_INP

Note : When I tried to check in IBM MQ Explorer the first request response always goes into 'Current QueueDepth' .Current QueueDepth doesnot get blank when I have read the response the second time. 注:当我尝试检入IBM MQ Explorer时,第一个请求响应始终进入“ Current QueueDepth”。当我第二次读取响应时,Current QueueDepth不会空白。 We have separate queues for Input and Output We tried restarting the IBM MQ ,clearing messages,creating new queue and new channel but nothing works. 我们有用于输入和输出的单独队列我们尝试重新启动IBM MQ,清除消息,创建新队列和新通道,但是没有任何效果。

ReadMsg method : ReadMsg方法:

queue = queueManager.AccessQueue(queueName, MQC.MQOO_INQUIRE + MQC.MQOO_BROWSE + MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_INPUT_SHARED );
queueMessage = new MQMessage();
queueMessage.Format = MQC.MQFMT_STRING;
queueGetMessageOptions = new MQGetMessageOptions();
queue.Get(queueMessage, queueGetMessageOptions);
strReturn = queueMessage.ReadString(queueMessage.MessageLength);

The issue has been resolved. 该问题已解决。

Reason : When the first request was sent the connection was already existing for that message and read operation could not read the output as the response was already held by previous(write) object. 原因:发送第一个请求时,该消息的连接已经存在,并且读取操作无法读取输出,因为先前的(写入)对象已经保留了响应。 When I was sending second request, the first connection was disconnected so I was able to read the response of first request. 当我发送第二个请求时,第一个连接已断开,因此我能够读取第一个请求的响应。

Solution : After completing the write operation i used 'queueManager.Disconnect()'. 解决方案:完成写入操作后,我使用了“ queueManager.Disconnect()”。 However I had to reestablish the connection when reading the message from queue. 但是,当从队列中读取消息时,我必须重新建立连接。

The connection code:
System.Collections.Hashtable queueProperties = new System.Collections.Hashtable();
queueProperties[MQC.HOST_NAME_PROPERTY] = host;
queueProperties[MQC.PORT_PROPERTY] = port;
queueProperties[MQC.CHANNEL_PROPERTY] = channel;            
queueManager = new MQQueueManager(queueManagerName, queueProperties);                

The write code:
queue = queueManager.AccessQueue(queueName, MQC.MQOO_INPUT_SHARED + MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_OUTPUT + MQC.MQOO_BROWSE);                
queueMessage = new MQMessage();
queueMessage.WriteString(message);
queueMessage.Persistence = 0;
queueMessage.Format = MQC.MQFMT_STRING;
queuePutMessageOptions = new MQPutMessageOptions();
queue.Put(queueMessage, queuePutMessageOptions);
queueManager.Disconnect();
queue.Close(); 

The read code :
queue = queueManager.AccessQueue(queueName, MQC.MQOO_INQUIRE + MQC.MQOO_BROWSE + MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_INPUT_SHARED + MQC.MQCMD_CREATE_LISTENER);
queueMessage.Format = MQC.MQFMT_STRING;
queueGetMessageOptions = new MQGetMessageOptions();
queue.Get(queueMessage, queueGetMessageOptions);
strReturn = queueMessage.ReadString(queueMessage.MessageLength);
queueManager.Disconnect();
queue.Close();

Method calling:

Write:
connectResp = operation.ConnectMQ(CustomMQ.queueManager, CustomMQ.host, CustomMQ.port, CustomMQ.channel);
sendResp = operation.WriteQMsg(CustomMQ.inputQueueName, request);

Read:
connectResp = operation.ConnectMQ(CustomMQ.queueManager, CustomMQ.host, CustomMQ.port, CustomMQ.channel);
response = operation.ReadQMsg(CustomMQ.outputQueueName);

Provide rating as per your experience. 根据您的经验提供评分。 In case of negative rating, kindly highlight the reason. 如果是负面评价,请指出原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM