Route by Body
This sample demonstrates how to implement a service that accepts message objects with any SOAP action. This sample is based on the Getting Started Sample that implements a calculator service. The service implements a single Calculate
operation that accepts a Message request parameter and returns a Message response.
In this sample, the client is a console application (.exe) and the service is hosted in IIS.
Note: |
---|
The setup procedure and build instructions for this sample are located at the end of this topic. |
The sample demonstrates message dispatch based on the body content. The built-in Windows Communication Foundation (WCF) service model message dispatch mechanism is based on message Actions. However, there are many existing Web services that define all of their operations with Action="". It is impossible to build a service based on WSDL that keeps dispatching request messages based on Action information. This sample demonstrates a service contract that is based on WSDL (the WSDL is contained in Service.wsdl that is included with the sample). The service contract is Calculator, similar to the one used in Getting Started Sample. However the [OperationContract]
specifies Action=""
for all operations.
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples"),
XmlSerializerFormat, DispatchByBodyBehavior]
public interface ICalculator
{
[OperationContract(Action="")]
double Add(double n1, double n2);
[OperationContract(Action = "")]
double Subtract(double n1, double n2);
[OperationContract(Action = "")]
double Multiply(double n1, double n2);
[OperationContract(Action = "")]
double Divide(double n1, double n2);
}
Given a contract, a service requires custom dispatch behavior DispatchByBodyBehavior
to allow messages to be dispatched between operations. This dispatch behavior initializes the DispatchByBodyElementOperationSelector
custom operation selector with a table of the operation names keyed by QName of respective wrapper elements. DispatchByBodyElementOperationSelector
looks at the start tag of the first child of the Body and selects the operation using the table previously mentioned.
The client uses a proxy auto-generated from the WSDL exported by the service using Service Metadata Utility Tool (Svcutil.exe).
svcutil.exe /n:http://Microsoft.ServiceModel.Samples,Microsoft.ServiceModel.Samples /uxs https://localhost/servicemodelsamples/service.svc?wsdl /out:generatedProxy.cs
The fact that actions of all operations are empty has no impact on the client code, except for the Action parameters in the auto-generated proxy.
The client code performs several calculations. When you run the sample, the operation requests and responses are displayed in the client console window. Press ENTER in the client window to shut down the client.
Add(100, 15.99) = 115.99
Subtract(145, 76.54) = 68.46
Multiply(9, 81.25) = 731.25
Divide(22, 7) = 3.14285714285714
Press <ENTER> to terminate client.
To set up, build, and run the sample
Ensure that you have performed the One-Time Setup Procedure for the Windows Communication Foundation Samples.
To build the solution, follow the instructions in Building the Windows Communication Foundation Samples.
To run the sample in a single- or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.
Send comments about this topic to Microsoft.
© Microsoft Corporation. All rights reserved.