Export (0) Print
Expand All

Untyped Request/Reply

This sample demonstrates how to define operation contracts that use the Message class.

Note 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 an 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 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 message version 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 which will be 
    //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 message version, 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 message version 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 step 3 to see how to build and run the sample.

To set up, build, and run the sample

  1. Ensure that you have performed the One-Time Setup Procedure for the Windows Communication Foundation Samples.

  2. To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  3. To run the sample in a single- or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.

Important note Important

The samples may already be installed on your machine. Check for the following (default) directory before continuing.

<InstallDrive>:\WF_WCF_Samples

If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.

<InstallDrive>:\WF_WCF_Samples\WCF\Basic\Contract\Message\Untyped

Show:
© 2014 Microsoft