Untyped Request/Reply
This sample demonstrates how to define operation contracts that use the Message class.
Note: |
---|
The setup procedure and build instructions for this sample are located at the end of this topic. |
This sample is based on the Getting Started Sample. The service contract defines one operation that takes in a message type as argument and returns a message. The operation collects all required data to compute the sum from the message body and then sends the sum as the body in the return message:
[OperationContract(Action = CalculatorService.RequestAction, ReplyAction = CalculatorService.ReplyAction)]
Message ComputeSum(Message request);
On the service, the operation retrieves the array of integers passed in the input message and then computes the sum. To send a response message, the sample creates a new message with the appropriate MessageVersion and Action and adds the computed sum as its body. The following sample code demonstrates this:
public Message ComputeSum(Message request)
{
//The body of the message contains a list of numbers that is
//read as a int[] using GetBody<T>.
int result = 0;
int[] inputs = request.GetBody<int[]>();
foreach (int i in inputs)
{
result += i;
}
Message response = Message.CreateMessage(request.Version,
ReplyAction, result);
return response;
}
The client uses code that is generated by ServiceModel Metadata Utility Tool (Svcutil.exe) to create a proxy to the remote service. To send a request message, the client must have the MessageVersion, which depends on the underlying channel. Thus, it creates a new OperationContextScope scoped to the proxy channel it created, which creates an OperationContext with the correct MessageVersion populated in its OutgoingMessageHeaders.MessageVersion
property. The client passes an input array as the body to the request message and then invokes the ComputeSum
on the proxy. The client then retrieves the sum of the inputs it passed by accessing the GetBody<T>
method on the reply message. The following sample code demonstrates this:
using (new OperationContextScope(client.InnerChannel))
{
// Call the Sum service operation.
int[] values = { 1, 2, 3, 4, 5 };
Message request = Message.CreateMessage(
OperationContext.Current.OutgoingMessageHeaders.MessageVersion,
RequestAction, values);
Message reply = client.ComputeSum(request);
int response = reply.GetBody<int>();
Console.WriteLine("Sum of numbers passed (1,2,3,4,5) = {0}",
response);
}
This sample is a Web-hosted sample and so only the client executable must be run. The following is the sample output on the client:
Prompt>Client.exe
Sum of numbers passed (1,2,3,4,5) = 15
Press <ENTER> to terminate client.
This sample is a Web-hosted sample and so check the link provided in the following step 3 to see how to build and run the sample.
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 C# or Visual Basic .NET edition of 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.