Validating a check using MSMQ in C #

I'm new to Microsoft Message Queue in Windows Server, I need to click if EmployeeID is 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 } } 

Once data is inserted into this exception queue for verification, it must be processed by a separate process. Every 1 hour, the process must be initiated, and it must call the following method

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

Please indicate to me how to create and process it.

+5
source share
1 answer

First step: Install MSMQ as a Windows function on the server / PC

Then:
- Create a queue if it does not exist
- asynchronously press the message in the queue
Useful guide

Sample code for pushing and receiving messages from 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"); } } 


Another widely used way to do this will also be to use from mailbox logs that already contain this functionality, such as Enterprise Library or NLog, which provide simple interfaces for this.

To receive messages, I would recommend a separate Windows service that periodically read messages and processed them. A good example of how to do this is given here: a Windows service with a timer

Update: Windows Service Example:

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

and MessageProcessor.cs

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

Source: https://habr.com/ru/post/1259985/


All Articles