Export (0) Print
Expand All

How to: Create a Service Endpoint in Code

In this example, an ICalculator contract is defined for a calculator service, the service is implemented in the CalculatorService class, and then its endpoint is defined in code, where it is specified that the service must use the BasicHttpBinding class.

It is usually the best practice to specify the binding and address information declaratively in configuration rather than imperatively in code. Defining endpoints in code is usually not practical because the bindings and addresses for a deployed service are typically different from those used while the service is being developed. More generally, keeping the binding and addressing information out of the code allows them to change without having to recompile or redeploy the application.

To create a service endpoint in code

  1. Create the interface that defines the service contract.

    
      [ServiceContract]
      public interface ICalculator
      {
         [OperationContract]
         double Add(double n1, double n2);
         [OperationContract]
         double Subtract(double n1, double n2);
         [OperationContract]
         double Multiply(double n1, double n2);
         [OperationContract]
         double Divide(double n1, double n2);
      }
    
    
    
    
  2. Implement the service contract defined in step 1.

    
      public class CalculatorService : ICalculator
      {
         public double Add(double n1, double n2)
         {
            return n1 + n2;
         }
         public double Subtract(double n1, double n2)
         {
            return n1 - n2;
         }
         public double Multiply(double n1, double n2)
         {
            return n1 * n2;
         }
         public double Divide(double n1, double n2)
         {
            return n1 / n2;
         }
      } 
    
    
    
    
  3. In the hosting application, create the base address for the service and the binding to be used with the service.

    
    
    // Specify a base address for the service
    
    String baseAddress = "http://localhost/CalculatorService";
    // Create the binding to be used by the service.
    
    BasicHttpBinding binding1 = new BasicHttpBinding();
    
    
    
  4. Create the host and call AddServiceEndpoint(Type, Binding, String) or one of the other overloads to add the service endpoint for the host.

    
    
                using(ServiceHost host = new ServiceHost(typeof(CalculatorService)))
                {
                    host.AddServiceEndpoint(typeof(ICalculator),binding1, baseAddress);
    
    
    
    

    To specify the binding in code, but to use the default endpoints provided by the runtime, pass the bass address into constructor when creating the ServiceHost, and do not call AddServiceEndpoint.

    
    ServiceHost host = new ServiceHost(typeof(CalculatorService), new Uri(baseAddress));
    
    
    

    For more information about default endpoints, see Simplified Configuration and Simplified Configuration for WCF Services.

Show:
© 2014 Microsoft