c#中使用MSMQ的验证异常队列

本文关键字:验证 异常 队列 MSMQ | 更新日期: 2023-09-27 18:18:34

我是Windows Server中Microsoft Message Queue的新成员,如果EmployeeID为NULL,我需要推送。

Employee Model Class是

public class Employee
{
    public string EmployeeID { get; set; }
    public string EmployeeName { get; set; }
}
public void ValidationProcess(Employee emp)
{
    if((emp != null) || (emp.EmployeeID == null))
    {
       // Push into Validation Exception Queue using MSMQ
    }
}

一旦数据被推入验证异常队列,它应该由单独的进程处理。流程需要每隔1小时启动一次,它应该调用以下方法

public void ValidationExceptionProcess(object obj)
{
    // Some Inner Process
    // Log the Error
}

请指导我如何创建和处理它

c#中使用MSMQ的验证异常队列

在服务器/pc

上安装MSMQs作为windows功能

:
—如果队列不存在,则创建该队列
-异步地将消息推送到队列中


有用的指南

从msmq推送和检索消息的代码示例:

public class ExceptionMSMQ
    {
        private static readonly string description = "Example description";
        private static readonly string path = @".'Private$'myqueue";
        private static MessageQueue exceptionQueue;
        public static MessageQueue ExceptionQueue
        {
            get
            {
                if (exceptionQueue == null)
                {
                    try
                    {
                        if (MessageQueue.Exists(path))
                        {
                            exceptionQueue = new MessageQueue(path);
                            exceptionQueue.Label = description;
                        }
                        else
                        {
                            MessageQueue.Create(path);
                            exceptionQueue = new MessageQueue(path);
                            exceptionQueue.Label = description;
                        }
                    }
                    catch
                    {
                        throw;
                    }
                    finally
                    {
                        exceptionQueue.Dispose();
                    }
                }
                return exceptionQueue;
            }
        }
        public static void PushMessage(string message)
        {
            ExceptionQueue.Send(message);
        }
        private static List<string> RetrieveMessages()
        {
            List<string> messages = new List<string>();
            using (ExceptionQueue)
            {
                System.Messaging.Message[] queueMessages = ExceptionQueue.GetAllMessages();
                foreach (System.Messaging.Message message in queueMessages)
                {
                    message.Formatter = new XmlMessageFormatter(
                    new String[] { "System.String, mscorlib" });
                    string msg = message.Body.ToString();
                    messages.Add(msg);
                }
            }
            return messages;
        }
        public static void Main(string[] args)
        {
            ExceptionMSMQ.PushMessage("my exception string");
        }
    }


另一种广泛使用的方法是使用已经包含此功能的开箱即用的日志记录器,如Enterprise Library或NLog,它们提供了简单的接口来完成此操作。

对于检索消息,我建议使用一个单独的windows服务,它将定期读取消息并处理它们。这里给出了一个很好的示例:

更新: Windows Service示例:

MSMQConsumerService.cs

public partial class MSMQConsumerService : ServiceBase
{
    private System.Timers.Timer timer;
    public MSMQConsumerService()
    {
        InitializeComponent();
    }
    protected override void OnStart(string[] args)
    {
        this.timer = new System.Timers.Timer(30000D);  // 30000 milliseconds = 30 seconds
        this.timer.AutoReset = true;
        this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.ProcessQueueMessages);
        this.timer.Start();
    }
    protected override void OnStop()
    {
        this.timer.Stop();
        this.timer = null;
    }
    private void ProcessQueueMessages(object sender, System.Timers.ElapsedEventArgs e)
    {
        MessageProcessor.StartProcessing();
    }
}

和MessageProcessor.cs

public class MessageProcessor
    {
        public static void StartProcessing()
        {
            List<string> messages = ExceptionMSMQ.RetrieveMessages();
            foreach(string message in messages)
            {
                //write message in database
            }
        }
    }