訊息佇列至 Windows Communication Foundation

這個範例會示範訊息佇列 (MSMQ) 應用程式如何將 MSMQ 訊息傳送至 Windows Communication Foundation (WCF) 服務。這個服務是自我裝載的主控台應用程式,可讓您觀察接收佇列訊息的服務。

服務合約為 IOrderProcessor,這會定義適合與佇列搭配使用的單向服務。MSMQ 訊息沒有 Action 標頭,所以不可能自動將不同 MSMQ 訊息對應到作業合約。因此,這時只能有一個作業合約。如果您想要為服務定義一個以上的作業合約,應用程式就必須提供資訊,說明 MSMQ 訊息中的哪個標頭 (例如,標籤或 correlationID) 可以用來決定分派哪個作業合約。這會在自訂 Demux中示範。

MSMQ 訊息不會包含有關作業合約的不同參數各自對應到哪個標頭的資訊。參數的型別是 MsmqMessage(MsmqMessage<T>),其中包含基礎 MSMQ 訊息。MsmqMessage(MsmqMessage<T>) 類別中的型別 "T" 代表已序列化為 MSMQ 訊息本文的資料。在這個範例中,PurchaseOrder 型別會序列化為 MSMQ 訊息本文。

下列範例程式碼會示範訂單處理服務的服務合約。

// Define a service contract.
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
[ServiceKnownType(typeof(PurchaseOrder))]
public interface IOrderProcessor
{
    [OperationContract(IsOneWay = true, Action = "*")]
    void SubmitPurchaseOrder(MsmqMessage<PurchaseOrder> msg);
}

服務會自我裝載。使用 MSMQ 時,必須事先建立使用的佇列。這個動作可手動或透過程式碼完成。在這個範例中,服務會檢查佇列的存在,並在需要時建立佇列。佇列名稱會從組態檔中讀取。

public static void Main()
{
    // Get the MSMQ queue name from the application settings in 
    // configuration.
    string queueName = ConfigurationManager.AppSettings["queueName"];
    // Create the MSMQ queue if necessary.
    if (!MessageQueue.Exists(queueName))
        MessageQueue.Create(queueName, true);
    …
}

服務會建立並開啟 OrderProcessorServiceServiceHost,如下列範例程式碼所示。

using (ServiceHost serviceHost = new ServiceHost(typeof(OrderProcessorService)))
{
    serviceHost.Open();
    Console.WriteLine("The service is ready.");
    Console.WriteLine("Press <ENTER> to terminate service.");
    Console.ReadLine();
    serviceHost.Close();
}

MSMQ 佇列名稱是指定在組態檔的 appSettings 區段中,如下面的範例組態所示。

ms751490.note(zh-tw,VS.90).gif注意:
佇列名稱會使用點 (.)來代表本機電腦,並在其路徑中使用反斜線分隔符號。WCF 端點位址會指定 msmq.formatname 配置,並使用 localhost 表示本機電腦。每個 MSMQ 格式名稱定址方針的佇列位址會遵循 msmq.formatname 配置。

<appSettings>
    <add key="orderQueueName" value=".\private$\Orders" />
</appSettings>

用戶端應用程式是使用 Send 方法將永久和交易訊息傳送至佇列的 MSMQ 應用程式,如下列範例程式碼所示。

//Connect to the queue.
MessageQueue orderQueue = new MessageQueue(ConfigurationManager.AppSettings["orderQueueName"]);

// Create the purchase order.
PurchaseOrder po = new PurchaseOrder();
po.CustomerId = "somecustomer.com";
po.PONumber = Guid.NewGuid().ToString();

PurchaseOrderLineItem lineItem1 = new PurchaseOrderLineItem();
lineItem1.ProductId = "Blue Widget";
lineItem1.Quantity = 54;
lineItem1.UnitCost = 29.99F;

PurchaseOrderLineItem lineItem2 = new PurchaseOrderLineItem();
lineItem2.ProductId = "Red Widget";
lineItem2.Quantity = 890;
lineItem2.UnitCost = 45.89F;

po.orderLineItems = new PurchaseOrderLineItem[2];
po.orderLineItems[0] = lineItem1;
po.orderLineItems[1] = lineItem2;

// Submit the purchase order.
Message msg = new Message();
msg.Body = po;
//Create a transaction scope.
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
{
    
    orderQueue.Send(msg, MessageQueueTransactionType.Automatic);
    // Complete the transaction.
    scope.Complete();
   
}
Console.WriteLine("Placed the order:{0}", po);
Console.WriteLine("Press <ENTER> to terminate client.");
Console.ReadLine();

當您執行範例時,用戶端與服務活動都會顯示在服務與用戶端主控台視窗中。您可以查看來自用戶端的服務接收訊息。在每個主控台視窗中按下 ENTER 鍵,即可關閉服務與用戶端。請注意,因為佇列正在使用中,所以用戶端與服務不需要同時啟動與執行。例如,您可以執行用戶端,關閉用戶端,然後再啟動服務,服務還是會收到訊息。

若要設定、建置及執行範例

  1. 請確定您已執行 Windows Communication Foundation 範例的單次安裝程序

  2. 若要建置方案的 C# 或 Visual Basic .NET 版本,請遵循建置 Windows Communication Foundation 範例中的指示。

  3. 若要在單一機器組態中執行範例,請遵循執行 Windows Communication Foundation 範例中的指示。

若要跨機器執行範例

  1. 將語言特定資料夾下 \service\bin\ 資料夾中的服務程式檔複製到服務機器中。

  2. 將語言特定資料夾下 \client\bin\ 資料夾中的用戶端程式檔案複製到用戶端機器中。

  3. 在 Client.exe.config 檔案中,變更 orderQueueName 以取代 "." 指定服務機器名稱。

  4. 在服務機器上,從命令提示字元啟動 Service.exe。

  5. 在用戶端機器上,從命令提示字元啟動 Client.exe。

請參閱

其他資源

Queues in Windows Communication Foundation
How To: Exchange Messages with WCF Endpoints and MSMQ applications
訊息佇列

Send comments about this topic to Microsoft.
© 2007 Microsoft Corporation. All rights reserved.