Simple WCF Service Host

app.config

The configuration for a simple http WCF host is:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
  </appSettings>

  <system.serviceModel>
    <services>
      <service name="AssemblyNamespace.ServiceImplementation"
               behaviorConfiguration="ServiceBehavior">
        <endpoint address="ServiceName"
                  binding="basicHttpBinding"
                  contract="AssemblyNamespace.ServiceContractInterface" />

        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />

        <host>
          <baseAddresses>
            <add baseAddress="http://HostName:Port/"/>
          </baseAddresses>
        </host>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

  </system.serviceModel>
</configuration>

An example:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
  </appSettings>

  <system.serviceModel>
    <services>
      <service name="TestService.CalculatorService"
               behaviorConfiguration="CalculatorServiceBehavior">
        <endpoint address="CalculatorService"
                  binding="basicHttpBinding"
                  contract="TestService.ICalculator" />

        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />

        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000/"/>
          </baseAddresses>
        </host>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="CalculatorServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

  </system.serviceModel>
</configuration>

The Service

Define the service in an interface

using System.ServiceModel;

namespace TestService
{
    [ServiceContract]
    public interface ICalculator
    {
        #region Instance Methods

        [OperationContract]
        double Add(double n1, double n2);

        [OperationContract]
        double Divide(double n1, double n2);

        [OperationContract]
        double Multiply(double n1, double n2);

        [OperationContract]
        double Subtract(double n1, double n2);

        #endregion
    }
}

Then implement the interface

using System;

namespace TestService
{
    public class CalculatorService : ICalculator
    {
        #region ICalculator Members

        public double Add(double n1, double n2) {
            double result = n1 + n2;
            Console.WriteLine("Received Add({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Divide(double n1, double n2) {
            double result = n1/n2;
            Console.WriteLine("Received Divide({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Multiply(double n1, double n2) {
            double result = n1*n2;
            Console.WriteLine("Received Multiply({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Subtract(double n1, double n2) {
            double result = n1 - n2;
            Console.WriteLine("Received Subtract({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        #endregion
    }
}

The Host

Lastly create a host for the service

using System;
using System.ServiceModel;

namespace TestService
{
    internal class Program
    {
        #region Class Methods

        private static void Main() {
            using (ServiceHost serviceHost = new ServiceHost(typeof (CalculatorService))) {
                serviceHost.Open();

                Console.WriteLine("The calculator service is ready.");
                Console.WriteLine("Press <ENTER> to terminate service.");
                Console.WriteLine();
                Console.ReadLine();
            }
        }

        #endregion
    }
}

3 thoughts on “Simple WCF Service Host

  1. Hi,

    Thx alot for nice and simple example:-)
    Would be nice, if you could download the code.
    Copy/paste is a nightmare..:-(

    Cheers m8

  2. Hi Søren,
    If you hover over the code you should see a small tool-bar appear in the top right of the code frame. This has options to copy to the clipboard or open the code in your browsers default ‘view source’ viewer.

Leave a Reply