How to: Exchange Messages Within a Reliable Session
Article
This topic outlines the steps required to enable a reliable session using one of the system-provided bindings that support such a session, but not by default. You enable a reliable session imperatively using code or declaratively in your configuration file. This procedure uses the client and service configuration files to enable the reliable session and to stipulate that the messages arrive in the same order in which they were sent.
The key part of this procedure is that the endpoint configuration element contain a bindingConfiguration attribute that references a binding configuration named Binding1. The <binding> configuration element references this name to enable reliable sessions by setting the enabled attribute of the <reliableSession> element to true. You specify the ordered delivery assurances for the reliable session by setting the ordered attribute to true.
Implement the service contract in a service class. Note that the address or binding information isn't specified inside the implementation of the service. You aren't required to write code to retrieve the address or binding information from the configuration file.
Create a Web.config file to configure an endpoint for the CalculatorService that uses the WSHttpBinding with reliable session enabled and ordered delivery of messages required.
XML
<?xml version="1.0" encoding="utf-8" ?><configuration><system.serviceModel><services><servicename="Microsoft.ServiceModel.Samples.CalculatorService"><!--
This endpoint is exposed at the base address provided by
host: http://localhost/servicemodelsamples/service.svc
Specify wsHttpBinding binding and a binding configuration
to use
--><endpointaddress=""binding="wsHttpBinding"bindingConfiguration="Binding1"contract="Microsoft.ServiceModel.Samples.ICalculator" /><!--
The mex endpoint is exposed at
http://localhost/servicemodelsamples/service.svc/mex
--><endpointaddress="mex"binding="mexHttpBinding"contract="IMetadataExchange" /></service></services><!--
Configures WSHttpBinding for reliable sessions with ordered
delivery.
--><bindings><wsHttpBinding><bindingname="Binding1"><reliableSessionenabled="true"ordered="true" /></binding></wsHttpBinding></bindings></system.serviceModel></configuration>
The generated client application also contains the implementation of the ClientCalculator. Note that the address and binding information isn't specified anywhere inside the implementation of the service. You aren't required to write code to retrieve the address or binding information from the configuration file.
Svcutil.exe also generates the configuration for the client that uses the WSHttpBinding class. Name the configuration file App.config when using Visual Studio.
XML
<?xml version="1.0" encoding="utf-8" ?><configuration><system.serviceModel><client><!--
Specify wsHttpBinding binding and a binding configuration
to use
--><endpointaddress="http://localhost/servicemodelsamples/service.svc"binding="wsHttpBinding"bindingConfiguration="Binding1"contract="Microsoft.ServiceModel.Samples.ICalculator" /></client><!--
Configures WSHttpBinding for reliable sessions with ordered
delivery
--><bindings><wsHttpBinding><bindingname="Binding1"><reliableSessionenabled="true"ordered="true" /></binding></wsHttpBinding></bindings></system.serviceModel></configuration>
Create an instance of the ClientCalculator in an application and call the service operations.
C#
//Client implementation code.classClient
{
staticvoidMain()
{
// Create a client with given client endpoint configuration
CalculatorClient client = new CalculatorClient();
// Call the Add service operation.double value1 = 100.00D;
double value2 = 15.99D;
double result = client.Add(value1, value2);
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
// Call the Subtract service operation.
value1 = 145.00D;
value2 = 76.54D;
result = client.Subtract(value1, value2);
Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);
// Call the Multiply service operation.
value1 = 9.00D;
value2 = 81.25D;
result = client.Multiply(value1, value2);
Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);
// Call the Divide service operation.
value1 = 22.00D;
value2 = 7.00D;
result = client.Divide(value1, value2);
Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
//Closing the client gracefully closes the connection and cleans up resources
client.Close();
Console.WriteLine();
Console.WriteLine("Press <ENTER> to terminate client.");
Console.ReadLine();
}
}
Compile and run the client.
Example
Several of the system-provided bindings support reliable sessions by default. These include:
In this module, you'll learn about the RabbitMQ message broker and how you can use it to decouple microservices while ensuring that they can communicate reliably. You'll also see how .NET Aspire makes it easy to integrate with RabbitMQ.