This sample demonstrates how to implement a typical service and a typical client using Windows Communication Foundation (WCF). This sample consists of a client console program (client.exe) and a service library hosted by Internet Information Services (IIS). The service implements a contract that defines a request-reply communication pattern. The contract is defined by the ICalculator interface, which exposes math operations (add, subtract, multiply, and divide). The client makes synchronous requests to a given math operation and the service replies with the result. Client activity is visible in the console window.


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


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.



The setup procedure and build instructions for this sample are located at the end of this topic.

This sample exposes the ICalculator contract using the <wsHttpBinding>. The configuration of this binding has been expanded in the Web.config file.

    <!--The following is the expanded configuration section for a-->  
    <!--WSHttpBinding. Each property is configured with the default-->   
    <!--value. See the ReliableSession, TransactionFlow, -->  
    <!--TransportSecurity, and MessageSecurity samples in the WS -->  
    <!--directory to learn how to configure these features. -->  
    <binding name="Binding1"  
      <reliableSession ordered="true"   
                       enabled="false" />  
      <security mode="Message">  
        <message clientCredentialType="Windows"   
                 establishSecurityContext="true" />  

On the base binding element, the maxReceivedMessageSize value lets you configure the maximum size of an incoming message (in bytes). The hostNameComparisonMode value lets you configure whether the hostname is considered when de-multiplexing messages to the service. The messageEncoding value lets you configure whether to use Text or MTOM encoding for messages. The textEncoding value lets you configure the character encoding for messages. The bypassProxyOnLocal value lets you configure whether to use an HTTP proxy for local communication. The transactionFlow value configures whether the current transaction is flowed (if an operation is configured for transaction flow).

On the <reliableSession> element, the enabled Boolean value configures whether reliable sessions are enabled. The ordered value configures whether message ordering is preserved. The inactivityTimeout value configures how long a session can be idle before being faulted.

On the <security>, the mode value configures which security mode should be used. In this sample, messages security is being used, which is why the <message> is specified inside the <security>.

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

  1. Install ASP.NET 4.0 using the following command.

    %windir%\Microsoft.NET\Framework\v4.0.XXXXX\aspnet_regiis.exe /i /enable  
  2. Ensure that you have performed the One-Time Setup Procedure for the Windows Communication Foundation Samples.

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

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