繁体   English   中英

C#Msmq错误:(0x80004005):无效的句柄传递给该函数

[英]C# Msmq error: (0x80004005): An invalid handle was passed to the function

我收到有关MSMQ的一些错误,找不到任何在线帮助我的东西。 这是堆栈跟踪:

System.Messaging.MessageQueueException (0x80004005): An invalid handle was passed to the function.
at System.Messaging.MessageQueue.ReceiveCurrent(TimeSpan timeout, Int32 action, CursorHandle cursor, MessagePropertyFilter filter, MessageQueueTransaction internalTransaction, MessageQueueTransactionType transactionType)
at System.Messaging.MessageQueue.Peek(TimeSpan timeout, Cursor cursor, PeekAction action)
at Utility.Msmq.MsmqManager.PeekWithoutTimeout(Cursor cursor, PeekAction action)
at Utility.Msmq.MsmqManager.SearchMessages(List`1 labels)

使用以下代码初始化队列:

var queuePath = isIp
    ? (machineName == "127.0.0.1"
            ? string.Format(@".\Private$\{0}", queueName)
            : string.Format("FormatName:Direct=OS:{0}\\private$\\{1}", machineName,
                            queueName))
    : string.Format("FormatName:Direct=TCP:{0}\\private$\\{1}", machineName, queueName);

try
{
    _queue = MessageQueue.Exists(queuePath)
                    ? new MessageQueue(queuePath)
                    : MessageQueue.Create(queuePath, true);
    _queue.MessageReadPropertyFilter.Priority = true;
    _queue.SetPermissions("Everyone", MessageQueueAccessRights.FullControl);
}
catch(MessageQueueException)
{
    // Concurrent threads on a new company will try to create the queue at once, which throws an error
    // If this happens, wait 100ms, then check if queue was created by some other thread, and return it.
    while (!MessageQueue.Exists(queuePath))
    {
        Thread.Sleep(100);
    }
    _queue = new MessageQueue(queuePath);
}

这是在msmq上执行偷看的代码

private Message PeekWithoutTimeout(Cursor cursor, PeekAction action)
{
    Message ret = null;
    try
    {
        ret = _queue.Peek(new TimeSpan(1), cursor, action);
    }
    catch (MessageQueueException mqe)
    {
        if (mqe.MessageQueueErrorCode != MessageQueueErrorCode.IOTimeout)
        {
            throw;
        }
    }
    catch (Exception generalException)
    {
        Loggers.CreateBillLogger.Error(generalException.Message, generalException);
        throw generalException;
    }

    return ret;
}

public List<Message> SearchMessages(List<string> labels)
{
    var count = 0;
    var cursor = _queue.CreateCursor();
    var messages = new List<Message>();

    var message = PeekWithoutTimeout(cursor, PeekAction.Current);
    if (message != null)
    {
        if (labels.Contains(message.Label))
        {
            messages.Add(message);
        }

        do
        {
            message = PeekWithoutTimeout(cursor, PeekAction.Next);
            if (message != null && labels.Contains(message.Label))
            {
                messages.Add(message);
            }
        } while (message != null);
    }

    return messages;
}

PS我应该注意的一件事是,这段代码使用BackgroundWorkers在多线程环境中运行,并且两个或多个线程尝试一次创建队列的情况发生了很大变化,从而导致并发创建。 在catch分支中解决了此问题。

我做错了什么吗? 谢谢 !

在非常老的.NET Framework版本中似乎存在一个与该问题几乎完全匹配的问题。 我找到了它,也找到了您的帖子,因为有时我们遇到同样的问题。

KB 826297描述了在多线程环境中调用ReceiveById时发生的此问题。

编辑:我认为锁定语句就足以解决它-但我看错了他们的帮助文章。 看来您可能需要使用知识库文章中的技巧

暂无
暂无

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

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