Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从异步方法读取/写入websphere队列_C#_Asynchronous_Ibm Mq - Fatal编程技术网

C# 从异步方法读取/写入websphere队列

C# 从异步方法读取/写入websphere队列,c#,asynchronous,ibm-mq,C#,Asynchronous,Ibm Mq,我有一个异步方法,它只负责连接到队列并从队列中读取数据。在尝试读取时,我从WebSphere dll“amqmdnet.dll”中得到一个错误,该错误表示“线程正在中止”。每次尝试以任何方式修改队列时,以及仅当尝试从异步方法修改队列时,都会出现此错误。我还尝试实现了IBM.XMS.net dll,因为据说它用于异步消息传递,尽管我得到了相同的错误。是否可以从异步方法内部读取队列?如果是这样,那么在修改队列本身时,同步和异步的读/写实现是否有所不同?我可以很好地连接到队列管理器,它正在修改队列,这

我有一个异步方法,它只负责连接到队列并从队列中读取数据。在尝试读取时,我从WebSphere dll“amqmdnet.dll”中得到一个错误,该错误表示“线程正在中止”。每次尝试以任何方式修改队列时,以及仅当尝试从异步方法修改队列时,都会出现此错误。我还尝试实现了IBM.XMS.net dll,因为据说它用于异步消息传递,尽管我得到了相同的错误。是否可以从异步方法内部读取队列?如果是这样,那么在修改队列本身时,同步和异步的读/写实现是否有所不同?我可以很好地连接到队列管理器,它正在修改队列,这给我带来了问题

主要内容:

MqMessanger:(IBM.XMS.net dll)

MqMessanger:(amqmdnet dll)


我认为IBM MQ的异步消息传递模式和.NET Framework的异步编程概念之间存在一些混淆

IBM MQ通过队列以异步方式实现应用程序到应用程序的连接。在典型的客户机-服务器模式中,客户机和服务器应用程序都需要始终启动并运行以进行数据交换。这是同步模式。在异步模式中,客户机应用程序和服务器应用程序不需要总是运行。客户机可以向MQ发送消息并离开。消息将驻留在MQ队列中。如果服务器应用程序正在运行,则该消息将传递给服务器应用程序。服务器将处理该消息,并将回复消息放入MQ队列中,然后离开。客户端应用程序可以在一段时间后返回并读取回复消息。正如您所看到的,客户机和服务器应用程序不是同时进行的,但它们仍然能够通过MQ以异步方式进行通信

我不是较新的.NET框架概念方面的专家。NET Framework中的
async
编程概念用于卸载短任务,这些任务可以由CPU独立完成,而不会阻塞线程。例如,当主线程忙于显示网页内容时,连接到数据库的任务可以转移到.NET任务,以便用户能够与网页交互


IBM MQ.NET(amqmdnet和XMS)使用多个线程与队列管理器通信。我不确定当涉及多个线程时,“异步”编程技术是否合适。这可能是“线程被中止”错误的原因。

异步/等待模式提供了对异步代码(如网络I/O)的抽象,因此任务类非常适合IBM MQ。不幸的是,amqmdnet似乎不支持这一点?每个
Put()
Get()
方法都返回
void
,而不是
Task
,因此都是异步代码上的同步,这是一种反模式。
private async Task ReadAsync()
{
   await MqMessanger.ConnectAsync(); // connects fine
   await MqMessanger.StartReadingAsync(); // errors out
}
private XMSFactoryFactory factoryFactory; //used for connection
private IConnectionFactory cf; //used for connection
private ISession sessionWMQ;
private IDestination destination;
private IMessageConsumer consumerAsync;
private MessageListener messageListener;
public IConnection QueueManager { get; set; }

//QueueManager has been connected prior to this
private void StartReadingAsync()
{
    try
    {
        //Creates a session where an Ack is sent to the server to delete the message as soon the message is received. 
        sessionWMQ = QueueManager.CreateSession(false, AcknowledgeMode.AutoAcknowledge);
        destination = sessionWMQ.CreateQueue(queueName);

        // Create consumer object to read messages from the queue
        consumerAsync = sessionWMQ.CreateConsumer(destination);

        // Create a message listener to fire when a message is put on the queue and assign it to consumer
        messageListener = new MessageListener(OnMessageCallback);
        consumerAsync.MessageListener = messageListener;                    
    }
    catch (Exception ex)
    {
        throw new Exception($"Error reading from '{destination.Name}'.", ex);
    }
}
//QueueManager has been connected prior to this
private void StartReadingAsync()
{
    try
    {
       queue = queueManager.AccessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING);
        queueMessage = new MQMessage();
        queueMessage.Format = MQC.MQFMT_STRING;
        queueGetMessageOptions = new MQGetMessageOptions();
        queue.Get(queueMessage, queueGetMessageOptions);
        string message = queueMessage.ReadString(queueMessage.MessageLength);
        //Do something with this message
    }
    catch (Exception ex)
    {
       throw new Exception($"Error reading from '{destination.Name}'.", ex);
    }