Thursday, August 29, 2013

Self hosted windows Service .

Self hosted windows Service .

  • Open vs 2008 --> File--> New --> Project
  • Select Windows Service Template.
  • Name the service as "SelfHostedService".
  • Now Add Class and Interface and name them "Calculator" and "ICalculator" respectively.

  • Add the following code in ICalculator.cs
  • Add references : using System.ServiceModel and using System.ServiceModel.Web . 
// Code for ICalculator.cs


 namespace SelfHostedService

{
    [ServiceContract]
    public interface ICalculator 
    {
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "/Add?num1={num1}&num2{num2}",           ResponseFormat = WebMessageFormat.Xml)]
        string Add(string num1, string num2);
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "/Sub?num1={num1}&num2{num2}", ResponseFormat = WebMessageFormat.Xml)]
        string Sub(string num1, string num2);
    }
}



//

// Code for Calculator.cs


namespace SelfHostedService
{
    public class Calculator : ICalculator
    {
        public int Add(string num1, string num2)
        {
            int addition = Convert.ToInt32(num1) + Convert.ToInt32(num2);
            return addition;
        }

        public int Sub(string num1, string num2)
        {
            int addition = Convert.ToInt32(num1) - Convert.ToInt32(num2);
            return addition;
        }
    }
}

//
  • Now set the configuration for REST service in App Config file.
  • Add Web configuration file to the project and rename it to App.config
  • Remove everything inside in App.config and add the below code.
//Code for App.config


<system.serviceModel>
    <services>
      <!-- This section is optional with the new configuration model
           introduced in .NET Framework 4. -->
      <service behaviorConfiguration="CalculatorServiceBehavior" name="SelfHostedService.Calculator">
        <endpoint address=""  binding="webHttpBinding" behaviorConfiguration="web" contract="SelfHostedService.ICalculator" />

      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="CalculatorServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>

        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

//

  • Now open Service1.cs .
  • Add the following references : System.ServiceProcess ,System.ServiceModel ,System.Configuration.
// Code for Service1.cs

        ServiceHost serviceHost = null;
        protected override void OnStart(string[] args)
        {
           
            string serviceAddress = "http://localhost/2001";

            Uri baseAddress = new Uri(serviceAddress);
            if (serviceHost != null)
            {
                serviceHost.Close();
            }

            // Create a ServiceHost for the CalculatorService type and 
            // provide the base address.
            serviceHost = new ServiceHost(typeof(Calculator), baseAddress);

            // Open the ServiceHostBase to create listeners and start 
            // listening for messages.
            serviceHost.Open();
        }



        protected override void OnStop()
        {
            if (serviceHost != null)
            {
                serviceHost.Close();
            }
        }

//