作法:與 WCF 端點和訊息佇列應用程式交換訊息
您可以使用訊息佇列 (MSMQ) 整合繫結來轉換傳入與傳出 Windows Communication Foundation (WCF) 訊息的 MSMQ 訊息,藉此將現有的 MSMQ 應用程式與 WCF 應用程式進行整合。 這樣一來,您就可以從 WCF 用戶端對 MSMQ 接收者應用程式進行呼叫,並從 MSMQ 傳送者應用程式對 WCF 服務進行呼叫。
在本章節中,我們將說明如何在 (1) WCF 用戶端與使用 System.Messaging 撰寫而成的 MSMQ 應用程式服務之間,以及 (2) MSMQ 應用程式用戶端與 WCF 服務之間,使用已佇列通訊 MsmqIntegrationBinding。
如需示範如何從 WCF 用戶端呼叫 MSMQ 接收者應用程式的完整範例,請參閱從 Windows Communication Foundation 呼叫 Message Queuing 範例。
如需示範如何從 MSMQ 用戶端呼叫 WCF 服務的完整範例,請參閱從 Message Queuing 呼叫 Windows Communication Foundation 範例。
若要從 MSMQ 用戶端建立可接收訊息的 WCF 服務
定義可定義 WCF 服務 (用於接收來自 MSMQ 傳送者應用程式的佇列訊息) 之服務合約的介面,如下列範例程式碼所示。
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")] [ServiceKnownType(typeof(PurchaseOrder))] public interface IOrderProcessor { [OperationContract(IsOneWay = true, Action = "*")] void SubmitPurchaseOrder(MsmqMessage<PurchaseOrder> msg); }
<ServiceContract(Namespace:="http:'Microsoft.ServiceModel.Samples")> _ <ServiceKnownType(GetType(PurchaseOrder))> _ Public Interface IOrderProcessor <OperationContract(IsOneWay:=True, Action:="*")> _ Sub SubmitPurchaseOrder(ByVal msg As MsmqMessage(Of PurchaseOrder)) End Interface
實作介面並將 ServiceBehaviorAttribute 屬性套用至類別,如下列範例程式碼所示。
public class OrderProcessorService : IOrderProcessor { [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)] public void SubmitPurchaseOrder(MsmqMessage<PurchaseOrder> ordermsg) { PurchaseOrder po = (PurchaseOrder)ordermsg.Body; Random statusIndexer = new Random(); po.Status = (OrderStates)statusIndexer.Next(3); Console.WriteLine("Processing {0} ", po); } // Host the service within this EXE console application. public static void Main() { // Get base address from appsettings in configuration. Uri baseAddress = new Uri(ConfigurationManager.AppSettings["baseAddress"]); // Create a ServiceHost for the CalculatorService type and provide the base address. using (ServiceHost serviceHost = new ServiceHost(typeof(IOrderProcessor), baseAddress)) { // Open the ServiceHostBase to create listeners and start listening for messages. serviceHost.Open(); // The service can now be accessed. Console.WriteLine("The service is ready."); Console.WriteLine("The service is running in the following account: {0}", WindowsIdentity.GetCurrent().Name); Console.WriteLine("Press <ENTER> to terminate service."); Console.WriteLine(); Console.ReadLine(); // Close the ServiceHostBase to shutdown the service. serviceHost.Close(); } } }
Public Class OrderProcessorService Implements IOrderProcessor <OperationBehavior(TransactionScopeRequired:=True, TransactionAutoComplete:=True)> _ Public Sub SubmitPurchaseOrder(ByVal ordermsg As MsmqMessage(Of PurchaseOrder)) Implements IOrderProcessor.SubmitPurchaseOrder Dim po As PurchaseOrder = ordermsg.Body Dim statusIndexer As New Random() po.Status = statusIndexer.Next(3) Console.WriteLine("Processing {0} ", po) End Sub End Class
建立會指定 MsmqIntegrationBinding 的組態檔。
產生可使用設定繫結的 ServiceHost 物件。
若要建立可將訊息傳送至 MSMQ 接收者應用程式的 WCF 用戶端
定義可定義 WCF 用戶端 (用於將佇列訊息傳送至 MSMQ 接收者) 之服務合約的介面,如下列範例程式碼所示。
[System.ServiceModel.ServiceContractAttribute(Namespace = "http://Microsoft.ServiceModel.Samples")] public interface IOrderProcessor { [OperationContract(IsOneWay = true, Action = "*")] void SubmitPurchaseOrder(MsmqMessage<PurchaseOrder> msg); } public interface IOrderProcessorChannel : IOrderProcessor, System.ServiceModel.IClientChannel { }
<System.ServiceModel.ServiceContractAttribute(Namespace:="http:'Microsoft.ServiceModel.Samples")> _ Public Interface IOrderProcessor <OperationContract(IsOneWay:=True, Action:="*")> _ Sub SubmitPurchaseOrder(ByVal msg As MsmqMessage(Of PurchaseOrder)) end interface Public Interface IOrderProcessorChannel Inherits IOrderProcessor, System.ServiceModel.IClientChannel End Interface
定義 WCF 用戶端將用於呼叫 MSMQ 接收者的用戶端類別。
MsmqIntegrationBinding binding = new MsmqIntegrationBinding(); EndpointAddress address = new EndpointAddress("msmq.formatname:DIRECT=OS:.\\private$\\Orders"); ChannelFactory<IOrderProcessor> channelFactory = new ChannelFactory<IOrderProcessor>(binding, address); IOrderProcessor channel = channelFactory.CreateChannel(); 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; MsmqMessage<PurchaseOrder> ordermsg = new MsmqMessage<PurchaseOrder>(po); using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required)) { channel.SubmitPurchaseOrder(ordermsg); scope.Complete(); } Console.WriteLine("Order has been submitted:{0}", po);
Dim binding As New MsmqIntegrationBinding() Dim address As New EndpointAddress("msmq.formatname:DIRECT=OS:.\\private$\\Orders") Dim channelFactory As New ChannelFactory(Of IOrderProcessor)(binding, address) Dim channel As IOrderProcessor = channelFactory.CreateChannel() Dim po As New PurchaseOrder() po.customerId = "somecustomer.com" po.poNumber = Guid.NewGuid().ToString() Dim lineItem1 As New PurchaseOrderLineItem() lineItem1.productId = "Blue Widget" lineItem1.quantity = 54 lineItem1.unitCost = 29.99F Dim lineItem2 = New PurchaseOrderLineItem() lineItem2.productId = "Red Widget" lineItem2.quantity = 890 lineItem2.unitCost = 45.89F Dim lineItems(1) As PurchaseOrderLineItem lineItems(0) = lineItem1 lineItems(1) = lineItem2 po.orderLineItems = lineItems Dim ordermsg As MsmqMessage(Of PurchaseOrder) = New MsmqMessage(Of PurchaseOrder)(po) Using scope As New TransactionScope(TransactionScopeOption.Required) channel.SubmitPurchaseOrder(ordermsg) scope.Complete() End Using Console.WriteLine("Order has been submitted:{0}", po)
建立會指定 MsmqIntegrationBinding 繫結使用的組態。
MsmqIntegrationBinding binding = new MsmqIntegrationBinding("MyBindingConfig");
Dim binding As New MsmqIntegrationBinding("MyBindingConfig")
建立用戶端類別的執行個體,並呼叫訊息接收服務所定義的方法。
// 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; OrderProcessorClient client = new OrderProcessorClient("OrderResponseEndpoint"); MsmqMessage<PurchaseOrder> ordermsg = new MsmqMessage<PurchaseOrder>(po); using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required)) { client.SubmitPurchaseOrder(ordermsg); scope.Complete(); } Console.WriteLine("Order has been submitted:{0}", po); //Closing the client gracefully closes the connection and cleans up resources. client.Close(); Console.WriteLine(); Console.WriteLine("Press <ENTER> to terminate client."); Console.ReadLine();