Essential C# Windows Communication Foundation  Suggest an edit

Getting started sample

The service describes the operations it performs in a service contract that it exposes publicly as metadata.

// Define a service contract.  
public interface ICalculator  
    double Add(double n1, double n2);

The service implementation calculates and returns the appropriate result, as shown in the following example code.

// Service class that implements the service contract.  
public class CalculatorService : ICalculator  
    public double Add(double n1, double n2)  
        return n1 + n2;  

The service exposes an endpoint for communicating with the service, defined using a configuration file (Web.config), as shown in the following sample configuration.

        <!-- ICalculator is exposed at the base address provided by  
         host: http://localhost/servicemodelsamples/service.svc.  -->  
       <endpoint address=""  
              contract="StackOverflow.ServiceModel.Samples.ICalculator" />  

The framework does not expose metadata by default. As such, the service turns on the ServiceMetadataBehavior and exposes a metadata exchange (MEX) endpoint at http://localhost/servicemodelsamples/service.svc/mex. The following configuration demonstrates this.

      <!-- the mex endpoint is explosed at  
       http://localhost/servicemodelsamples/service.svc/mex -->  
      <endpoint address="mex"  
                contract="IMetadataExchange" />  

  <!--For debugging purposes set the includeExceptionDetailInFaults  
   attribute to true-->  
      <behavior name="CalculatorServiceBehavior">  
        <serviceMetadata httpGetEnabled="True"/>  
        <serviceDebug includeExceptionDetailInFaults="False" />  

The client communicates using a given contract type by using a client class that is generated by the ServiceModel Metadata Utility Tool (Svcutil.exe).

Run the following command from the SDK command prompt in the client directory to generate the typed proxy:

svcutil.exe /n:"http://StackOverflow.ServiceModel.Samples,StackOverflow.ServiceModel.Samples" http://localhost/servicemodelsamples/service.svc/mex /out:generatedClient.cs

Like the service, the client uses a configuration file (App.config) to specify the endpoint with which it wants to communicate. The client endpoint configuration consists of an absolute address for the service endpoint, the binding, and the contract, as shown in the following example.

         contract="StackOverflow.ServiceModel.Samples.ICalculator" />  

The client implementation instantiates the client and uses the typed interface to begin communicating with the service, as shown in the following example code.

// Create a client.  
CalculatorClient client = new CalculatorClient();  

// Call the Add service operation.  
double value1 = 100.00D;  
double value2 = 15.99D;  
double result = client.Add(value1, value2);  
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); 

//Closing the client releases all communication resources.  

Table Of Contents
22 Enum
107 Stream
108 Timers
155 Windows Communication Foundation
  ↑ ↓ to navigate     ↵ to select     Esc to close