启动和停止窗口服务时出现System.Messaging.MessageQueueException(0x80004005)错误

时间:2017-01-03 10:52:19

标签: c# .net service msmq

实际上,我有一个窗口服务,每隔一段时间读取一次MSMQ queuw。我的MSMQ队列工作没有问题,但问题是当我停止或启动Windows服务然后它的错误。完整的错误如下:

**ERROR 65 DAL.MsmqImportListener - A MSMQ error occured
System.Messaging.MessageQueueException (0x80004005)
at System.Messaging.MessageQueue.AsynchronousRequest.End()
at DAL.MsmqImportListener.PeekCompleted(Object sender, PeekCompletedEventArgs e)**



private static void PeekCompleted(object sender, PeekCompletedEventArgs e)
    {
        var msmq = sender as MessageQueue;
        var messageProcessor = e.AsyncResult.AsyncState as MessageProcessorMethod;
        try
        {
            using (var scope = new TransactionScope())
            {
                msmq.EndPeek(e.AsyncResult);
                var message = msmq.ReceiveById(
                    e.Message.Id,
                    TimeSpan.FromSeconds(double.Parse(ConfigurationManager.AppSettings["MsmqReceiveTimeout"])),
                    MessageQueueTransactionType.Automatic);
                messageProcessor(message);
                scope.Complete();
            }
        }

        catch (MessageQueueException mqe)
        {
            // Check if timeout...no action if timeout, else log error
            if (mqe.MessageQueueErrorCode != MessageQueueErrorCode.IOTimeout)
            {
                Logger.Error("A MSMQ error occured", mqe);
                EmailDispatcher.SendInformationEmail("A MSMQ error occured" + Environment.NewLine + mqe);
            }
        }
        catch (Exception ex)
        {

            Logger.Error(
                string.Format(
                    CultureInfo.InvariantCulture,
                    "An unexpected error was encountered, message with id {0} was put in error queue: {1}",
                    e.Message.Id,
                    success),
                ex);

        }
        finally
        {
            msmq.BeginPeek(TimeSpan.FromSeconds(double.Parse(ConfigurationManager.AppSettings["MsmqPeekTimeout"])), messageProcessor);
        }
    } 

0 个答案:

没有答案