简体   繁体   English

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

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

I receive some errors regarding MSMQ, and can't find anything online to help me out. 我收到有关MSMQ的一些错误,找不到任何在线帮助我的东西。 Here is the stacktrace: 这是堆栈跟踪:

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)

The queue is initialized using this code: 使用以下代码初始化队列:

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);
}

This is the code that executes the peek on msmq 这是在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 One thing I should note is this piece of code runs in a multi-threading environment, using BackgroundWorkers and there are high changes for two or more threads to try and create a queue at a time, leading to concurrent create. PS我应该注意的一件事是,这段代码使用BackgroundWorkers在多线程环境中运行,并且两个或多个线程尝试一次创建队列的情况发生了很大变化,从而导致并发创建。 This issue was solved in the catch branch. 在catch分支中解决了此问题。

Is there something that I'm doing wrong ? 我做错了什么吗? Thanks ! 谢谢 !

There appears to be an issue in a very old .NET framework version that almost perfectly matches this problem. 在非常老的.NET Framework版本中似乎存在一个与该问题几乎完全匹配的问题。 I found it, and your post, because we're experiencing the same issue sometimes. 我找到了它,也找到了您的帖子,因为有时我们遇到同样的问题。

KB 826297 describes this issue occurring when calling ReceiveById in a multithreaded environment. KB 826297描述了在多线程环境中调用ReceiveById时发生的此问题。

Edit: I thought a lock statement would be sufficient to fix it - but I misread their help article. 编辑:我认为锁定语句就足以解决它-但我看错了他们的帮助文章。 It seems you might need to use the hack in the KB article 看来您可能需要使用知识库文章中的技巧

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

相关问题 C#MySql 0x80004005):SQL语法错误; - C# MySql 0x80004005): an error in SQL syntax; C# MySql.Data.MySqlClient.MySqlException (0x80004005) - C# MySql.Data.MySqlClient.MySqlException (0x80004005) WPF System.ComponentModel.Win32Exception(0x80004005):无效的窗口句柄 - WPF System.ComponentModel.Win32Exception (0x80004005): Invalid window handle OleDbException(0x80004005):未指定错误? - OleDbException (0x80004005): Not specified error? TCPClient套接字错误0x80004005的原因 - Causes of TCPClient socket error 0x80004005 C# System.ComponentModel.Win32Exception (0x80004005): 没有足够的存储空间来处理此命令 - C# System.ComponentModel.Win32Exception (0x80004005): Not enough storage is available to process this command IErrorInfo.GetDescription在asp.net c#中的E_FAIL(0x80004005)失败了? - IErrorInfo.GetDescription failed with E_FAIL(0x80004005) in asp.net c#? IErrorInfo.GetDescription失败,显示E_FAIL(0x80004005)C# - IErrorInfo.GetDescription failed with E_FAIL(0x80004005) C# System.Data.OleDb.OleDbException(0x80004005):未指定的错误 - System.Data.OleDb.OleDbException(0x80004005): Unspecified Error ExternalException (0x80004005) GDI+ 中发生一般错误 - ExternalException (0x80004005) A generic error occurred in GDI+
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM