How to: Exchange Queued Messages with WCF Endpoints
Queues ensure that reliable messaging can occur between a client and a Windows Communication Foundation (WCF) service, even if the service is not available at the time of communication. The following procedures show how to ensure durable communication between a client and a service by using the standard queued binding when implementing the WCF service.
This section explains how to use NetMsmqBinding for queued communication between a WCF client and a WCF service.
To use queuing in a WCF service
Define a service contract using an interface marked with the ServiceContractAttribute. Mark the operations in the interface that are part of the service contract with the OperationContractAttribute and specify them as one-way because no response to the method is returned. The following code provides an example service contract and its operation definition.
[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")] public interface IOrderProcessor { [OperationContract(IsOneWay = true)] void SubmitPurchaseOrder(PurchaseOrder po); }
When the service contract passes user-defined types, you will need to define data contracts for those types. The following code shows two data contracts,
PurchaseOrder
andPurchaseOrderLineItem
these two types define data that will be sent to the service. (Note that the classes that defines this data contract also defines a number of methods. These methods are not considered part of the data contract. Only those members that are declared with the DataMember attribute are part of the data contract.)// Define the purchase order line item. [DataContract(Namespace = "http://Microsoft.ServiceModel.Samples")] public class PurchaseOrderLineItem { [DataMember] public string ProductId; [DataMember] public float UnitCost; [DataMember] public int Quantity; public override string ToString() { String displayString = "Order LineItem: " + Quantity + " of " + ProductId + " @unit price: $" + UnitCost + "\n"; return displayString; } public float TotalCost { get { return UnitCost * Quantity; } } } // Define the purchase order. [DataContract(Namespace = "http://Microsoft.ServiceModel.Samples")] public class PurchaseOrder { static readonly string[] OrderStates = { "Pending", "Processed", "Shipped" }; static Random statusIndexer = new Random(137); [DataMember] public string PONumber; [DataMember] public string CustomerId; [DataMember] public PurchaseOrderLineItem[] orderLineItems; public float TotalCost { get { float totalCost = 0; foreach (PurchaseOrderLineItem lineItem in orderLineItems) totalCost += lineItem.TotalCost; return totalCost; } } public string Status { get { return OrderStates[statusIndexer.Next(3)]; } } public override string ToString() { System.Text.StringBuilder strbuf = new System.Text.StringBuilder("Purchase Order: " + PONumber + "\n"); strbuf.Append("\tCustomer: " + CustomerId + "\n"); strbuf.Append("\tOrderDetails\n"); foreach (PurchaseOrderLineItem lineItem in orderLineItems) { strbuf.Append("\t\t" + lineItem.ToString()); } strbuf.Append("\tTotal cost of this order: $" + TotalCost + "\n"); strbuf.Append("\tOrder status: " + Status + "\n"); return strbuf.ToString(); } }
Implement the methods of the service contract defined in the interface in a class.
public class OrderProcessorService : IOrderProcessor { [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)] public void SubmitPurchaseOrder(PurchaseOrder po) { Orders.Add(po); Console.WriteLine("Processing {0} ", po); } }
Notice the OperationBehaviorAttribute placed on the
SubmitPurchaseOrder
method. This specifies that this operation must be called within a transaction and that the transaction will automatically complete when the method completes.Create a transactional queue using System.Messaging. You could choose to create the queue using Microsoft Message Queuing (MSMQ) Microsoft Management Console (MMC) instead. If so, make sure you create a transactional queue.
// Create the transacted MSMQ queue if necessary. if (!MessageQueue.Exists(queueName)) MessageQueue.Create(queueName, true);
Define a ServiceEndpoint in configuration that specifies the service address and uses the standard NetMsmqBinding binding. For more information about using WCF configuration, see Configuring Windows Communication Foundation Applications.
<services> <service name="Microsoft.ServiceModel.Samples.OrderProcessorService" behaviorConfiguration="CalculatorServiceBehavior"> <host> <baseAddresses> <add baseAddress="https://localhost:8000/ServiceModelSamples/service"/> </baseAddresses> </host> <!-- Define NetMsmqEndpoint --> <endpoint address="net.msmq://localhost/private/ServiceModelSamplesTransacted" binding="netMsmqBinding" contract="Microsoft.ServiceModel.Samples.IOrderProcessor" /> <!-- the mex endpoint is exposed at https://localhost:8000/ServiceModelSamples/service/mex --> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services>
Create a host for the OrderProcessing service using ServiceHost that reads messages from the queue and processes them. Open the service host to make the service available. Display a message telling the user to press any key to terminate the service. Call ReadLine to wait for the key to be pressed and then close the service.
// Create a ServiceHost for the OrderProcessorService type. using (ServiceHost serviceHost = new ServiceHost(typeof(OrderProcessorService))) { // Open the ServiceHost to create listeners and start listening for messages. serviceHost.Open(); // The service can now be accessed. Console.WriteLine("The service is ready."); Console.WriteLine("Press <ENTER> to terminate service."); Console.WriteLine(); Console.ReadLine(); // Close the ServiceHostB to shutdown the service. serviceHost.Close(); }
To create a client for the queued service
Run the hosting application and use the svcutil.exe tool to create the WCF client. The following command line shows how to do this.
svcutil https://localhost:8000/ServiceModelSamples/service
Define a ServiceEndpoint in configuration that specifies the address and uses the standard NetMsmqBinding binding.
<client> <!-- Define NetMsmqEndpoint --> <endpoint address="net.msmq://localhost/private/ServiceModelSamplesTransacted" binding="netMsmqBinding" contract="Microsoft.ServiceModel.Samples.IOrderProcessor" /> </client>
Create a transaction scope to write to the transactional queue, call the
SubmitPurchaseOrder
operation and close the WCF client.//Create a transaction scope. using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required)) { // Make a queued call to submit the purchase order. client.SubmitPurchaseOrder(po); // Complete the transaction. scope.Complete(); } //Closing the client gracefully closes the connection and cleans up resources. client.Close();
Example
// This is the service code
// Copyright (c) Microsoft Corporation. All Rights Reserved.
using System;
using System.ServiceModel.Channels;
using System.Configuration;
using System.Messaging;
using System.ServiceModel;
using System.Transactions;
using System.Runtime.Serialization;
using System.Collections.Generic;
namespace Microsoft.ServiceModel.Samples
{
// Define the purchase order line item.
[DataContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
public class PurchaseOrderLineItem
{
[DataMember]
public string ProductId;
[DataMember]
public float UnitCost;
[DataMember]
public int Quantity;
public override string ToString()
{
String displayString = "Order LineItem: " + Quantity + " of " + ProductId + " @unit price: $" + UnitCost + "\n";
return displayString;
}
public float TotalCost
{
get { return UnitCost * Quantity; }
}
}
// Define the purchase order.
[DataContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
public class PurchaseOrder
{
static readonly string[] OrderStates = { "Pending", "Processed", "Shipped" };
static Random statusIndexer = new Random(137);
[DataMember]
public string PONumber;
[DataMember]
public string CustomerId;
[DataMember]
public PurchaseOrderLineItem[] orderLineItems;
public float TotalCost
{
get
{
float totalCost = 0;
foreach (PurchaseOrderLineItem lineItem in orderLineItems)
totalCost += lineItem.TotalCost;
return totalCost;
}
}
public string Status
{
get
{
return OrderStates[statusIndexer.Next(3)];
}
}
public override string ToString()
{
System.Text.StringBuilder strbuf = new System.Text.StringBuilder("Purchase Order: " + PONumber + "\n");
strbuf.Append("\tCustomer: " + CustomerId + "\n");
strbuf.Append("\tOrderDetails\n");
foreach (PurchaseOrderLineItem lineItem in orderLineItems)
{
strbuf.Append("\t\t" + lineItem.ToString());
}
strbuf.Append("\tTotal cost of this order: $" + TotalCost + "\n");
strbuf.Append("\tOrder status: " + Status + "\n");
return strbuf.ToString();
}
}
// Order Processing Logic
// Can replace with transaction-aware resource such as SQL or transacted hashtable to hold the purchase orders.
// This example uses a non-transactional resource.
public class Orders
{
static Dictionary<string, PurchaseOrder> purchaseOrders = new Dictionary<string, PurchaseOrder>();
public static void Add(PurchaseOrder po)
{
purchaseOrders.Add(po.PONumber, po);
}
public static string GetOrderStatus(string poNumber)
{
PurchaseOrder po;
if (purchaseOrders.TryGetValue(poNumber, out po))
return po.Status;
else
return null;
}
public static void DeleteOrder(string poNumber)
{
if(purchaseOrders[poNumber] != null)
purchaseOrders.Remove(poNumber);
}
}
// Define a service contract.
[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public interface IOrderProcessor
{
[OperationContract(IsOneWay = true)]
void SubmitPurchaseOrder(PurchaseOrder po);
}
// Service class that implements the service contract.
// Added code to write output to the console window.
public class OrderProcessorService : IOrderProcessor
{
[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
public void SubmitPurchaseOrder(PurchaseOrder po)
{
Orders.Add(po);
Console.WriteLine("Processing {0} ", po);
}
}
}
// This is the hosting application.
using System;
using System.ServiceModel.Channels;
using System.Configuration;
using System.Messaging;
using System.ServiceModel;
using System.Transactions;
using System.Runtime.Serialization;
using System.Collections.Generic;
namespace Microsoft.ServiceModel.Samples
{
class hostApp
{
// Host the service within this EXE console application.
public static void Main()
{
// Get MSMQ queue name from appsettings in configuration.
string queueName = ConfigurationManager.AppSettings["queueName"];
// Create the transacted MSMQ queue if necessary.
if (!MessageQueue.Exists(queueName))
MessageQueue.Create(queueName, true);
// Create a ServiceHost for the OrderProcessorService type.
using (ServiceHost serviceHost = new ServiceHost(typeof(OrderProcessorService)))
{
// Open the ServiceHost to create listeners and start listening for messages.
serviceHost.Open();
// The service can now be accessed.
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
// Close the ServiceHostB to shutdown the service.
serviceHost.Close();
}
}
}
}
<!-- This is the app.config for the service -->
<configuration>
<appSettings>
<!-- use appSetting to configure MSMQ queue name -->
<add key="queueName" value=".\private$\ServiceModelSamplesTransacted" />
</appSettings>
<system.serviceModel>
<services>
<service
name="Microsoft.ServiceModel.Samples.OrderProcessorService"
behaviorConfiguration="CalculatorServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="https://localhost:8000/ServiceModelSamples/service"/>
</baseAddresses>
</host>
<!-- Define NetMsmqEndpoint -->
<endpoint address="net.msmq://localhost/private/ServiceModelSamplesTransacted"
binding="netMsmqBinding"
contract="Microsoft.ServiceModel.Samples.IOrderProcessor" />
<!-- the mex endpoint is exposed at https://localhost:8000/ServiceModelSamples/service/mex -->
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="CalculatorServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
// This is the client code.
// Copyright (c) Microsoft Corporation. All Rights Reserved.
using System;
using System.Configuration;
using System.Messaging;
using System.ServiceModel;
using System.Transactions;
namespace Microsoft.ServiceModel.Samples
{
//The service contract is defined in generatedClient.cs, generated from the service by the svcutil tool.
//Client implementation code.
class Client
{
static void Main()
{
// Create a client.
OrderProcessorClient client = new OrderProcessorClient();
// 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;
//Create a transaction scope.
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
{
// Make a queued call to submit the purchase order.
client.SubmitPurchaseOrder(po);
// Complete the transaction.
scope.Complete();
}
//Closing the client gracefully closes the connection and cleans up resources.
client.Close();
Console.WriteLine();
Console.WriteLine("Press <ENTER> to terminate client.");
Console.ReadLine();
}
}
}
<!-- This is the app.config for the client app -->
<configuration>
<system.serviceModel>
<client>
<!-- Define NetMsmqEndpoint -->
<endpoint address="net.msmq://localhost/private/ServiceModelSamplesTransacted"
binding="netMsmqBinding"
contract="Microsoft.ServiceModel.Samples.IOrderProcessor" />
</client>
</system.serviceModel>
</configuration>
See Also
Tasks
How to: Exchange Messages with WCF Endpoints and Message Queuing Applications
Reference
Concepts
Other Resources
© 2007 Microsoft Corporation. All rights reserved.
Build Date: 2009-08-07