MSMQ, microsoft Message Queue,微軟消息隊列。通過它,應用程式開發人員可以通過發送和接收消息,來與應用程式進行快速可靠的通信。
在WCF中,提供了MSMQ通信綁定:NetMsmqBinding和MsmqIntegrationBinding
這裡做個簡單的示範。(我的環境:windows2003 EE)
(1)安裝消息隊列服務
添加windows元件,在應用程式伺服器中找到消息隊列進行安裝。
(2)啟動消息隊列服務
(3)在計算機管理中檢視消息隊列,在專用消息隊列中添加專用隊列,起名為msmqdemo
(4)開始建立WCF服務
很簡單了,配置NetMsmqBinding綁定,設定綁定細節。exactlyOnce 設定為false,位址設定為:
net.msmq://localhost/private/MSMQDemo
專用隊列的位址是/private$/名稱,這裡的位址是這個樣子。然後釋出中繼資料,位址我設定的為:
http://localhost:8731/MSMQWCF/PrintService/
因為它不支援雙工通信(消息隊列是單向的),是以,操作屬性為單向。以添加客戶為例,
資料契約:
public class Customer
{
public int Unid { get; set; }
public string CustomerName { get; set; }
public DateTime CreateTime { get; set; }
}
服務契約為:
[OperationContract(IsOneWay = true)]
void AddCustomer(Customer customer);
實作:
public void AddCustomer(Customer customer)
Console.WriteLine("Customer order!");
Console.WriteLine(customer.Unid+":"
+customer.CustomerName+" 訂單時間:"
+customer.CreateTime.ToString());
(5)用戶端
添加wcf引用。測試
[Test]
public void TestmqmqCustomer()
PrintInstance.PrintServiceClient client =
new UNTest.PrintInstance.PrintServiceClient();
PrintInstance.Customer _customer =
new UNTest.PrintInstance.Customer {
Unid=1,
CustomerName="宋江",
CreateTime=Convert.ToDateTime("2010-2-2")
};
client.AddCustomer(_customer);
測試正常。
在服務不啟動的狀态下,單獨執行用戶端測試,會把資訊寫到消息隊列中,這個可以在消息隊列控制台看到。隊列的讀取與優先級有關。同優先級的,按先進先出處理。
部落格園大道至簡
<a href="http://www.cnblogs.com/jams742003/" target="_blank">http://www.cnblogs.com/jams742003/</a>
轉載請注明:部落格園