menu

Sunday, June 19, 2016

Async Function in WCF Service

Below are the classes and interface to host a service

ICalc.cs
using System;
using System.ServiceModel;
namespace ConsoleService
{
    [ServiceContract]
    interface ICalc
    {
        [OperationContract(AsyncPattern = true)]
        IAsyncResult BeginAdd(int a, int b, AsyncCallback callback, object asyncState);
        int EndAdd(IAsyncResult result);

        [OperationContract]
        int Sub(int a, int b);
    }

}

Calc.cs
using System;
namespace ConsoleService
{
    class Calc : ICalc
    {
        Func<int, int, int> add = (n1, n2) => n1 + n2;
        public IAsyncResult BeginAdd(int a, int b, AsyncCallback callback, object asyncState)
        {
            return add.BeginInvoke(a, b, callback, asyncState);
        }

        public int EndAdd(IAsyncResult result)
        {
            return add.EndInvoke(result);
        }

        public int Sub(int a, int b)
        {
            return a - b;
        }
    }

}

Service.cs
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace ConsoleService
{
    class Service
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(Calc), new Uri("http://localhost:8888/async"));
            host.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = true });
            host.Open();
            Console.Title = "Async Service";
            Console.ReadKey();
        }
    }
}

Below is the class which have the code to access the service

Client.cs
using System;
namespace ConsoleClient
{
    class Client
    {
        static void Main(string[] args)
        {
            ServiceAsync.CalcClient client = new ServiceAsync.CalcClient();
            Console.WriteLine(client.Add(2, 5));
            Console.ReadKey();
        }
    }

}

Saturday, June 11, 2016

WCF Service Console Host and Console Client

Below are the classes and interface to host a service

ICalculator.cs
using System;
using System.ServiceModel;
namespace Interface
{
    [ServiceContract]
    public interface ICalculator
    {
        [OperationContract]
        double Add(double num1, double num2);
        [OperationContract]
        double Subtract(double num1, double num2);
        [OperationContract]
        double Multiply(double num1, double num2);
        [OperationContract]
        double Devide(double num1, double num2);
    }
}

CalculatorService.cs
using System;
namespace Service
{
    public class CalculatorService : Interface.ICalculator
    {
        public double Add(double num1, double num2)
        {
            return num1 + num2;
        }
        public double Subtract(double num1, double num2)
        {
            return num1 - num2;
        }
        public double Multiply(double num1, double num2)
        {
            return num1 * num2;
        }
        public double Devide(double num1, double num2)
        {
            return num1 / num2;
        }
    }

}

Host.cs
using Interface;
using Service;
using System;
using System.ServiceModel;
namespace ConsoleHost
{
    class Host
    {
        static void Main(string[] args)
        {
            //ServiceHost host = new ServiceHost(typeof(CalculatorService));
            ServiceHost host = new ServiceHost(typeof(CalculatorService), new Uri("http://localhost:8888/test"));
            host.AddServiceEndpoint(typeof(ICalculator), new NetTcpBinding(), "net.tcp://localhost:8889/test");
            //host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "http://localhost:8888/test1");
            host.AddDefaultEndpoints();
            host.Open();

            Console.WriteLine("Listening at ...");
            int i = 0;
            foreach (var ep in host.Description.Endpoints)
            {
                Console.WriteLine("({0}) A: {1}, \tB: {2}, \tC: {3}", i, ep.Address, ep.Binding.Name, ep.Contract.Name);
                i++;
            }
            Console.WriteLine("Service is running ... Press any key to stop service.");
            Console.ReadKey();
            host.Close();
        }
    }
}

Below are the classes and configuration to access the above hosted service.

App.config
<xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <system.serviceModel>
    <client>
      <endpoint name="basicEP" address="http://localhost:8888/test" binding="basicHttpBinding" contract="Interface.ICalculator" />
      <endpoint name="netTcpEP" address="net.tcp://localhost:8889/test" binding="netTcpBinding" contract="Interface.ICalculator" />
    </client>
  </system.serviceModel>
</configuration>

Client.cs
using Interface;
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace ConsoleClient
{
    class Client
    {
        static void Main(string[] args)
        {
            /*
            ChannelFactory factory = new ChannelFactory();
            factory.Endpoint.Address = new EndpointAddress("http://localhost:8888/test");
            factory.Endpoint.Binding = new BasicHttpBinding();
            factory.Endpoint.Contract = ContractDescription.GetContract(typeof(ICalculator));
            */

            ChannelFactory<ICalculator> factory = new ChannelFactory<ICalculator>("netTcpEP"); //provide endpoint name defined in App.config
           
            ICalculator proxy = factory.CreateChannel();
            Console.WriteLine(proxy.Add(12, 22));
            Console.ReadKey();
        }
    }
}