共用方式為


消息佇列至 Windows Communication Foundation

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

服務合約為 IOrderProcessor,其定義適合用於佇列的單向服務。 MSMQ 訊息沒有 Action 標頭,因此無法自動將不同的 MSMQ 訊息對應至作業合約。 因此,只能有一個作業合約。 如果您想要為服務定義一個以上的作業合約,應用程式必須提供 MSMQ 訊息中哪個標頭的資訊(例如標籤或 correlationID)可用來決定要分派的作業合約。

MSMQ 訊息不包含標頭對應至不同操作契约參數的資訊。 參數的類型 MsmqMessage<T>為 (MsmqMessage<T>),其中包含基礎 MSMQ 訊息。 (MsmqMessage<T>) 類別中的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);
    …
}

服務會建立並開啟 ServiceHost 以供 OrderProcessorService 使用,如以下範例程式碼所示。

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 區段中指定,如下列範例組態所示。

備註

佇列名稱使用點(.)來表示本機電腦,並在路徑中使用反斜杠作為分隔符。 WCF 端點位址會指定 msmq.formatname 配置,並使用 localhost 作為本機計算機。 每個 MSMQ 格式名稱尋址指導方針的佇列位址都遵循 msmq.formatname 配置。

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

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

//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 範例 執行One-Time 安裝程式。

  2. 如果服務先執行,它會檢查以確定佇列存在。 如果佇列不存在,服務將會建立一個佇列。 您可以先執行服務來建立佇列,也可以透過 MSMQ 佇列管理員建立佇列。 請遵循下列步驟,在 Windows 2008 中建立佇列。

    1. 在 Visual Studio 2012 中開啟伺服器管理員。

    2. 展開 [功能] 索引標籤。

    3. 以滑鼠右鍵按下 私人消息佇列,然後選取 新增私人佇列

    4. 勾選 [交易式] 方塊。

    5. 輸入 ServiceModelSamplesTransacted 作為新佇列的名稱。

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

  4. 若要在單一計算機設定中執行範例,請遵循 執行 Windows Communication Foundation 範例中的指示。

在不同的電腦上運行範例

  1. 將服務程式檔案從語言專用資料夾中的 \service\bin\ 資料夾,複製到服務電腦。

  2. 將用戶端程式檔案從語言特定資料夾下的 \client\bin\ 資料夾,複製到用戶端電腦。

  3. 在 Client.exe.config 檔案中,變更 orderQueueName 以指定服務計算機名稱,而不是 “.”。

  4. 在伺服器上,從命令列啟動 Service.exe。

  5. 在用戶端電腦上,從命令提示符啟動 "Client.exe"。

另請參閱