Multiple Contracts

Download sample

The Multiple Contracts sample demonstrates how to implement more than one contract on a service and how to configure endpoints for communicating with each of the implemented contracts. This sample is based on the Getting Started Sample. The service has been modified to define two contracts, the ICalculator contract and the ICalculatorSession contract.

Note

The WCF samples may already be installed on your machine. Check this (default) directory before continuing: <InstallDrive>:\Samples\WCFWFCardspaceIf this directory doesn’t exist, click the download sample link at the top of this page. Note that this will download and install all of the WF, WCF, and CardSpace samples, you will only have to do this once. The sample is located in the following directory <InstallDrive>:\Samples\WCFWFCardSpace\WCF\Basic\Service\MultipleContracts

Note

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

The service class implements both the ICalculator and ICalculatorSession contracts. Because one of the contracts requires a session, the service uses the PerSession instance mode to maintain the state over the lifetime of the session.

The service configuration has been modified to define two endpoints to expose each contract. The ICalculator endpoint is exposed at the base address using a basicHttpBinding. The ICalculatorSession endpoint is exposed at the baseaddress/session using a wsHttpBinding with the bindingConfiguration attribute set to BindingWithSession, as shown in the following sample configuration.

<service 
    name="Microsoft.ServiceModel.Samples.CalculatorService"
    behaviorConfiguration="CalculatorServiceBehavior">
  <!-- ICalculator endpoint is exposed using BasicBinding at the base
       address provided by host: 
       https://localhost/servicemodelsamples/service.svc  -->
  <endpoint address=""
            binding="basicHttpBinding"
            contract="Microsoft.ServiceModel.Samples.ICalculator" />
  <!-- ICalculatorSession endpoint is exposed using BindingWithSession
       at {baseaddress}/session:
       https://localhost/servicemodelsamples/service.svc/session -->
  <endpoint address="session"
            binding="wsHttpBinding"
            bindingConfiguration="BindingWithSession" 
           contract="Microsoft.ServiceModel.Samples.ICalculatorSession" />
  ...
</service>

The generated client code now includes a client class for both the original ICalculator contract and the new ICalculatorSession contract. The client configuration and code have been modified to communicate with each contract at the appropriate service endpoint.

The client is a console windows application (.exe). The service is hosted by Internet Information Services (IIS).

The client console window displays the operations sent to each of the endpoints, first the basic endpoint, followed by the secure endpoint.

To set up, build, and run the sample

  1. Ensure that you have performed the One-Time Set Up 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.

© 2007 Microsoft Corporation. All rights reserved.