Monthly Archives: June 2015

Bare-Bones WCF Service

MathService.Server

IMathService.cs:

using System.ServiceModel;

namespace MathService.Server
{
    [ServiceContract]
    public interface IMathService
    {
        [OperationContract]
        double Add(double x, double y);

        [OperationContract]
        double Subtract(double x, double y);

        [OperationContract]
        double Multiply(double x, double y);

        [OperationContract]
        double Divide(double x, double y);
    }
}

MathService.cs:

namespace MathService.Server
{
    internal class MathService : IMathService
    {
        public double Add(double x, double y)
        {
            return x + y;
        }

        public double Subtract(double x, double y)
        {
            return x - y;
        }

        public double Multiply(double x, double y)
        {
            return x * y;
        }

        public double Divide(double x, double y)
        {
            return x / y;
        }
    }
}

App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
  </startup>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceMetadata />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="MathService.Server.MathService">
        <host>
          <baseAddresses>
            <add baseAddress="net.pipe://localhost/MathService" />
          </baseAddresses>
        </host>
        <endpoint binding="netNamedPipeBinding" contract="MathService.Server.IMathService" />
        <endpoint address="mex" binding="mexNamedPipeBinding" contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>
</configuration>

Program.cs:

using System;
using System.ServiceModel;

namespace MathService.Server
{
    internal static class Program
    {
        public static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(MathService)))
            {
                host.Open();
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey(true);
                host.Close();
            }
        }
    }
}

MathService.Client

App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
  </startup>
  <system.serviceModel>
    <client>
      <endpoint name="IMathService" address="net.pipe://localhost/MathService" binding="netNamedPipeBinding" contract="MathService.Server.IMathService" />
    </client>
  </system.serviceModel>
</configuration>

Program.cs:

using MathService.Server;
using System;
using System.ServiceModel;

namespace MathService.Client
{
    internal static class Program
    {
        public static void Main(string[] args)
        {
            ChannelFactory<IMathService> factory = new ChannelFactory<IMathService>("IMathService");
            IMathService service = factory.CreateChannel();
            double x = 1.0;
            double y = 2.0;
            Console.WriteLine(service.Add(x, y));
            Console.WriteLine(service.Subtract(x, y));
            Console.WriteLine(service.Multiply(x, y));
            Console.WriteLine(service.Divide(x, y));
        }
    }
}