HOW TO:與 WCF 端點和訊息佇列應用程式交換訊息
您可以使用 MSMQ 整合繫結來轉換傳入與傳出 WCF 的 MSMQ 訊息,藉此將現有的訊息佇列 (MSMQ) 應用程式與 Windows Communication Foundation (WCF) 進行整合。這樣一來,您就可以從 WCF 用戶端對 MSMQ 接收者應用程式進行呼叫,並從 MSMQ 傳送者應用程式對 WCF 服務進行呼叫。
在本章節中,我們將說明如何在 (1) WCF 用戶端與使用 System.Messaging 撰寫而成的 MSMQ 應用程式服務之間,以及 (2) MSMQ 應用程式用戶端與 WCF 服務之間,使用已佇列通訊的 MsmqIntegrationBinding。
如需從 WCF 用戶端呼叫 MSMQ 接收者應用程式的完整範例,請參閱 Windows Communication Foundation 至訊息佇列範例。
如需從 MSMQ 用戶端呼叫 WCF 服務的完整範例,請參閱訊息佇列至 Windows Communication Foundation範例。
若要從 MSMQ 用戶端建立可接收訊息的 WCF 服務
定義可定義 WCF 服務 (用來接收來自 MSMQ 傳送者應用程式的佇列訊息) 之服務合約的介面,如下列範例程式碼所示。
<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
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")] [ServiceKnownType(typeof(PurchaseOrder))] public interface IOrderProcessor { [OperationContract(IsOneWay = true, Action = "*")] void SubmitPurchaseOrder(MsmqMessage<PurchaseOrder> msg); }
實作介面並將 ServiceBehaviorAttribute 屬性套用至類別,如下列範例程式碼所示。
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
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(); } } }
建立會指定 MsmqIntegrationBinding 的組態檔。
<configuration> <appSettings> <!-- use appSetting to configure MSMQ queue name --> <add key="orderQueueName" value=".\private$\Orders" /> <add key="baseAddress" value="https://localhost:8000/ServiceModelSamples/Service" /> </appSettings> <system.serviceModel> <services> <service name="Microsoft.ServiceModel.Samples.OrderProcessorService" > <endpoint address="msmq.formatname:DIRECT=OS:.\private$\Orders" binding="msmqIntegrationBinding" bindingConfiguration="OrderProcessorBinding" contract="Microsoft.ServiceModel.Samples.IOrderProcessor"> </endpoint> </service> </services> <bindings> <msmqIntegrationBinding> <binding name="OrderProcessorBinding" > <security mode="None" /> </binding> </msmqIntegrationBinding> </bindings> </system.serviceModel > </configuration>
產生可使用設定繫結的 ServiceHost 物件。
若要建立可將訊息傳送至 MSMQ 接收者應用程式的 WCF 用戶端
定義可定義 WCF 用戶端 (用來將佇列訊息傳送至 MSMQ 接收者) 之服務合約的介面,如下列範例程式碼所示。
<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
[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 { }
定義 WCF 用戶端將用來呼叫 MSMQ 接收者的用戶端類別。
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(2) 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 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);
建立會指定 MsmqIntegrationBinding 繫結使用的組態。
Dim binding As New MsmqIntegrationBinding("MyBindingConfig")
MsmqIntegrationBinding binding = new MsmqIntegrationBinding("MyBindingConfig");
<configuration> <system.serviceModel> <client> <endpoint name="OrderResponseEndpoint" address="msmq.formatname:DIRECT=OS:.\private$\Orders" binding="msmqIntegrationBinding" bindingConfiguration="OrderProcessorBinding" contract="Microsoft.ServiceModel.Samples.IOrderProcessor"> </endpoint> </client> <bindings> <msmqIntegrationBinding> <binding name="OrderProcessorBinding" > <security mode="None" /> </binding> </msmqIntegrationBinding> </bindings> </system.serviceModel> </configuration>
建立用戶端類別的執行個體,並呼叫訊息接收服務所定義的方法。
// 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();
另請參閱
工作
HOW TO:與 WCF 端點交換佇列訊息
Windows Communication Foundation 至訊息佇列
訊息佇列至 Windows Communication Foundation
訊息佇列上的訊息安全性