2016-11-18 16 views
5

我是Windows Server中的Microsoft Message Queue中的新成員,如果EmployeeID爲NULL,則需要推送。在C#中使用MSMQ的驗證異常隊列

的僱員模型類是

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 
} 

請指導我如何創建和處理它。

回答

2

第一步: 安裝MSMQs作爲Windows的服務器/ PC功能

然後:
- 創建隊列如果不存在
它 - 在隊列中異步
有用的推送消息用於從MSMQ推動和檢索消息guide

代碼例如:

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 with timer

更新: Windows服務實例:

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 
      } 
     } 
    } 
+0

你能否用各自的代碼詳細說明你的答案。 –

+0

@ B.Balamanigandan在這裏你是交配。添加了一些代碼來幫助你。我希望事情現在更清晰 – Pan

+0

相當不錯...我需要通過Windows服務調用讀取方法。此服務應啓動調用以執行讀取方法。 - 請分享你的想法。 –