Thursday, March 29, 2012

Design Pattern : Generic Singleton Class

Generic Class Object

public class GetInstance1<T> where T : class
    {
        private static object SyncRoot = new object();
        private static T instance = default(T);
        public static T Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (SyncRoot)
                    {
                        if (instance == null)
                        {
                            ConstructorInfo ci = typeof(T).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null);
                            if (ci == null) { throw new InvalidOperationException("Class must contain a private constructor"); }
                            instance = (T)ci.Invoke(null);
                        }
                    }
                }
                return instance;
            }
        }

Test Class
 public class MyTestClass
    {
        private MyTestClass() { }

        public string GetName()
        {

            return "Hi";
        }

    }

Consuming Class
 string strGetInstance1 = GetInstance1<MyTestClass>.Instance.GetName();

Monday, March 19, 2012

WCF: Self Hosting using Window Service

Objectives

  • Create a simple WCF service.
  • Host your WCF service in a Windows service using Transmission Control Protocol (TCP).
  • Create a simple client to consume your service.

Overview

WCF services can be self-hosted in an application (such as a console or a Windows Forms application), in a Windows service, in Internet Information Services (IIS) 6.0, or in IIS 7.0 with Windows Activation Services (WAS).
The advantages of hosting in a Windows service are:
  • Start on boot. The service will automatically be started when the hosting computer is rebooted.
  • Recovery. The service will be restarted by the Windows Service Control Manager if there is a failure.
  • Administration. Administrators already know how to manage Windows services.
  • Security Identity. Windows Service Control Manager allows you to choose an identity under which the process will run.
  • Binding flexibility. Hosting in a Windows service allows you to choose any binding protocol. IIS 6.0 only allows HTTP bindings.
The disadvantages of hosting in a Windows service are:
  • Installation. You must use a custom installer action or the .NET utility Installutil.exe.
  • Lack of enterprise features. Windows services do not have the security, manageability, scalability, and administrative features that are included in IIS.
To host WCF in a Windows service, you need to create the WCF service, create a Windows service to host the WCF service, and then install and run the Windows service. For the purposes of this How To article, you will use installutil.exe on the command line to install the service. In a production environment, you can use a setup program to install the service.

Summary of Steps

  • Step 1: Create a WCF Service
  • Step 2: Configure the WCF Endpoints to Use TCP and Set the Base Address
  • Step 3: Create a Windows Service
  • Step 4: Add the Service Installers to the Windows Service
  • Step 5: Modify the Windows Service to Host the WCF Service
  • Step 6: Install the Windows Service
  • Step 7: Create a Windows Forms Test Client Application
  • Step 8: Add a WCF Service Reference to the Client
  • Step 9: Test the Client and WCF Service

Step 1: Create a WCF Service

In this step, you create a WCF service to test hosting in a Windows service.
  1. In Visual Studio, click File, click New, and then click Project.
  2. In the Add New Project dialog box, in the Templates section, select WCF Service Library.
  3. In the Add New Project dialog box, click OK to create the WCF Service Library project WcfServiceLibrary1.

Step 2: Configure the WCF Endpoints to Use TCP and Set the Base Address

In this step, you modify the WCF configuration so that the endpoints use TCP instead of the default Hypertext Transfer Protocol (HTTP). You then set the base address for your service. Finally, you set httpGetEnabled to false, since you will be running under TCP.
  1. Right-click the App.config file of the WCF Service Library project and then click Edit WCF Configuration. If you do not see the Edit WCF Configuration option, on the Tools menu, click WCF Service Configuration Editor. Close the WCF Service Configuration Editor tool that appears. The option should now appear on the App.config context menu.
  2. In the Configuration Editor, in the configuration section, expand Services and then expand Endpoints.
  3. Select the first endpoint. Under Endpoint Properties, change the Binding from wsHttpBinding to netTcpBinding.
  4. Select the second endpoint. Under Endpoint Properties, change the Binding from mexHttpBinding to mexTcpBinding.
  5. Under Service, select the Host node, select the default address under the BaseAddress list, and then click Edit.
  6. Set the base address to the following and then click OK:
    net.tcp://localhost:8523/Service1 
    
    
  7. Under Advanced, expand the tree under Service Behaviors. Select serviceMetadata and change httpGetEnabled from True to False.
  8. Click File and then click Save to save your configuration changes.
  9. In Visual Studio, verify your configuration, which should look as follows:
      <system.serviceModel>
        <services>
          <service behaviorConfiguration="WcfServiceLibrary1.Service1Behavior"
            name="WcfServiceLibrary1.Service1">
            <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
              contract="WcfServiceLibrary1.IService1">
              <identity>
                <dns value="localhost" />
              </identity>
            </endpoint>
            <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
              contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                <add baseAddress="net.tcp://localhost:8523/Service1" />
              </baseAddresses>
            </host>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="WcfServiceLibrary1.Service1Behavior">
              <serviceMetadata httpGetEnabled="false" />
              <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    
    
Ff649818.note(en-us,PandP.10).gifNote:
Notes:
The port 8523 and Service1 are arbitrary for this example. If you run into a conflict where the port is in use, change it.
If you do not set httpGetEnabled to False, you will get an exception in the Event Log when the service tries to start.

Step 3: Create a Windows Service

In this step, you add a Windows Service project to your solution.
  1. Right-click your solution, cllick Add, and then click New Project.
  2. In the Add New Project dialog box, select Windows, and then select Windows Service.
  3. In the Name field, leave the default name WindowsService1 and then click OK to create a Windows service application.
  4. Copy App.config from your WCF Service Library project to your Windows service project. In the WCF Service Library project, right-click the App.config file, click Copy, and then right-click your Windows service project and click Paste.

Step 4: Add the Service Installers to the Windows Service

In this step, you add service installers to your Windows service.
  1. Right-click Service1.cs and then click View Designer.
  2. Right-click the designer view and then click Add Installer. This adds the ProjectInstaller.cs file with two objects, serviceProcessInstaller1 and serviceInstaller1.
  3. In the Design view of ProjectInstaller.cs, right-click serviceProcessInstaller1 and then click Properties.
  4. In the Properties pane, set the Account attribute to NetworkService.
  5. Right-click serviceInstaller1 and then click Properties.
  6. In the Properties pane, set the StartType attribute to Automatic.

Step 5: Modify the Windows Service to Host the WCF Service

In this step, you override the OnStart() and OnStop() methods to start and stop the WCF service inside the Windows service process.
  1. Add a reference to System.ServiceModel to your Windows Service project. To do so, in your Windows service project, right-click the References node and then click Add References. In the Add Reference dialog box, select System.ServiceModel and then click OK.
  2. Add a reference to your WCF Service Library project from your Windows service. To do so, in your Windows service project, right-click the References node and then click Add References. In the Add Reference dialog box, select the Projects tab. Select the WCF Service Library project, WcfServiceLibrary1, and then click OK.
  3. Add the following using statements to the Service1.cs file in your Windows service project.
    using System.ServiceModel;
    using WcfServiceLibrary1;
    
    
  4. Select Service1.cs and switch to code view.
  5. Declare an internal static member of ServiceHost type, as follows:
    internal static ServiceHost myServiceHost = null; 
    
    
  6. Override the OnStart method of the Windows service, to open the service host as follows:
    protected override void OnStart(string[] args)
    {
       if (myServiceHost != null)
       {
           myServiceHost.Close();
       }
       myServiceHost = new ServiceHost(typeof(Service1));
       myServiceHost.Open();
    }
    
    
  7. Override the OnStop method of the Windows service, to close the service host as follows:
    protected override void OnStop()
    {
       if (myServiceHost != null)
       {
          myServiceHost.Close();
          myServiceHost = null;
       }
    }
    
    
  8. Verify that your Service1.cs resembles the following:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Linq;
    using System.ServiceProcess;
    using System.Text;
    using System.ServiceModel;
    using WcfServiceLibrary1;
    
    namespace WindowsService1
    {
        public partial class Service1: ServiceBase
        {
            internal static ServiceHost myServiceHost = null; 
    
            public WCFServiceHost1()
            {
                InitializeComponent();
            }
            protected override void OnStart(string[] args)
            {
                if (myServiceHost != null)
                {
                    myServiceHost.Close();
                }
                myServiceHost = new ServiceHost(typeof(Service1));
                myServiceHost.Open();
            }
            protected override void OnStop()
            {
                if (myServiceHost != null)
                {
                    myServiceHost.Close();
                    myServiceHost = null;
                }
            }
        }
    }
    
    
  9. In the Solution Explorer, copy the App.config file from the WCF service project to the Windows service project so that the config file will be in both service binary folders after compiling.
  10. Build your solution and verify that your project produces WindowsService1.exe in your project \bin\debug directory of your WindowsService1 project.

Step 6: Install the Windows Service

In this step, you install the Windows service and run it from the Services console.
  1. Rebuild the solution and open a Visual Studio command prompt.
  2. Browse to the bin directory of the project where WindowsService1.exe is located.
  3. Run the following command to install the service:
    Installutil WindowsService1.exe 
    
    
  4. Start your service. To do so, click Start, click Run, type services.msc and then click OK. Right-click your service and then click Start.
Ff649818.note(en-us,PandP.10).gifNote:
If you have modified the service that is already installed, you can uninstall it by using following command:
Installutil /u WindowsService1.exe

Step 7: Create a Windows Forms Test Client Application

In this step, you create a Windows Forms application named Test Client that you will use to test the WCF service.
  1. Right-click your solution, click Add, and then click New Project.
  2. In the Add New Project dialog box, in the Templates section, select Windows Application.
  3. In the Name field, type Test Client and then click OK to create a Windows Forms application.

Step 8: Add a WCF Service Reference to the Client

In this step, you add a reference from your test client to your WCF service
  1. Right-click your Test client project and select Add Service Reference.
  2. In the Add Service Reference dialog box, set the Address to the following and then click OK
    net.tcp://localhost:8523/Service1
    
    
    Ff649818.note(en-us,PandP.10).gifNote:
    net.tcp://localhost:8523/Service1 is the base address that you set in Step 3 above.

Step 9: Test the Client and WCF Service

In this step, you use the test client to ensure that the WCF service is running properly.
  1. In your Client project, drag a button control onto your form.
  2. Double-click the button control to show the underlying code. In the code behind the button click, create an instance of the proxy, and call GetData of your WCF service. When you call the service, your current user security context will automatically be passed to your WCF service. The code should look as follows:
            private void button1_Click(object sender, EventArgs e)
            {
                ServiceReference1.Service1Client myService = new ServiceReference1.Service1Client();
                MessageBox.Show(myService.GetData(123), “My Service”);
                myService.Close();
            }
    
    
  3. Right-click your client project and then click Set as Startup Project.
  4. Run the client application by pressing F5 or Ctrl+F5. When you click the button on the form, the message “You entered: 123” should appear.

Another Example:

Windows Service Hosting

In this tutorial we are going to see the hosting WCF service in Windows service. We will use same set of code used for hosting the WCF service in Console application to this. This is same as hosting the service in IIS without message activated. There is some advantage of hosting service in Windows service.
  • The service will be hosted, when system starts
  • Process life time of the service can be controlled by Service Control Manager for windows service
  • All versions of Windows will support hosting WCF service.
Step 1: Now let start create the WCF service, Open the Visual Studio 2008 and click New->Project and select Class Library from the template.

Step 2: Add reference System.ServiceModel to the project. This is the core assembly used for creating the WCF service.
Step 3: Next we can create the ISimpleCalulator interface as shown below. Add the Service and Operation Contract attribute as shown below.
ISimpleCalculator.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace WindowsServiceHostedContract
{
    [ServiceContract]
    public interface ISimpleCalculator
    {
        [OperationContract]
        int Add(int num1, int num2);

        [OperationContract]
        int Subtract(int num1, int num2);

        [OperationContract]
        int Multiply(int num1,int num2);

        [OperationContract]
        double Divide(int num1, int num2);

    }
}
Step 4: Implement the ISimpleCalculator interface as shown below.
SimpleCalulator.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsServiceHostedService
{
    class SimpleCalculator
        : ISimpleCalculator
    {
 
        public int Add(int num1, int num2)
        {
            return num1+num2;
        }

        public int Subtract(int num1, int num2)
        {
             return num1-num2;
        }

        public int Multiply(int num1, int num2)
        {
             return num1*num2;
        }

        public double Divide(int num1, int num2)
        {
            if (num2 != 0)
                return num1 / num2;
            else
                return 0;
        }


    }
}
Step 5: Build the Project and get the dll. Now we are ready with WCF service, now we are going to see how to host the WCF Service in Windows service. Note: In this project, I have mention that we are creating both Contract and Service(implementation) are in same project. It is always good practice if you have both in different project.
Step 6: Open Visual Studio 2008 and Click New->Project and select Windows Service.
Step 7: Add the 'WindowsServiceHostedService.dll' as reference to the project. This assembly will going to act as service.


Step 8: OnStart method of the service, we can write the hosting code for WCF. We have to make sure that we are using only one service host object. On stop method you need to close the Service Host. Following code show how to host WCF service in Windows service.
WCFHostedWindowsService.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace WCFHostedWindowsService
{
    partial class WCFHostedWindowsService : ServiceBase
    {
        ServiceHost m_Host;
        
        public WCFHostedWindowsService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            if (m_Host != null)
            {
                m_Host.Close();
            }
            //Create a URI to serve as the base address
            Uri httpUrl = new Uri("http://localhost:8090/MyService/SimpleCalculator");
            //Create ServiceHost
            m_Host = new ServiceHost
            (typeof(WindowsServiceHostedService.SimpleCalculator), httpUrl);
            //Add a service endpoint
            m_Host.AddServiceEndpoint
            (typeof(WindowsServiceHostedService.ISimpleCalculator), new WSHttpBinding(), "");
            //Enable metadata exchange
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            m_Host.Description.Behaviors.Add(smb);
            //Start the Service
            m_Host.Open();


        }

        protected override void OnStop()
        {
            if (m_Host != null)
            {
                m_Host.Close();
                m_Host = null;
            }
        }
        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
   { 
    new WCFHostedWindowsService() 
   };
            ServiceBase.Run(ServicesToRun);

        }
    }
}
Step 9: In order to install the service we need to have the Installer class for the Windows service. So add new Installer class to the project, which is inherited from the Installer class. Please find the below code for mentioning the Service name, StartUp type etc of the service.
ServiceInstaller.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceProcess;
using System.Configuration.Install;
using System.ComponentModel;
using System.Configuration;


namespace WCFHostedWindowsService
{
    [RunInstaller(true)]
    public class WinServiceInstaller : Installer
    {
        private ServiceProcessInstaller process;
        private ServiceInstaller service;

        public WinServiceInstaller()
        {
            process = new ServiceProcessInstaller();
            process.Account = ServiceAccount.NetworkService;
            service = new ServiceInstaller();
            service.ServiceName = "WCFHostedWindowsService";
            service.DisplayName = "WCFHostedWindowsService";
            service.Description = "WCF Service Hosted";
            service.StartType = ServiceStartMode.Automatic;
            Installers.Add(process);
            Installers.Add(service);
        }
    }
}
Step 10: Build the project, we will get the WCFHostedWindowsService.exe. Next we need to install the service using Visual Studio Command Prompt. So open the command prompt by clicking Start->All Programs-> Microsoft Visual Studio 2008-> Visual Studio Tools-> Visual Studio Command Prompt Using installutil utility application, you can install the service as shown below.
Step 11: Now service is Hosted sucessfully and we can create the proxy class for the service and start using in the client applcaiton.

Another Example:

1>Create Service Library(Interface and Service classes dll)
2>Create a Windows Consol Application/Windows Service
3>Add required App.config file
4>Add System.ServiceModel and System.Runtime.Serilization namespace
5>Add new Window Service class
6>update the constructore, on start, on Stop method
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.ServiceModel;

namespace WCFWinsowServiceHost
{
    partial class TestWS : ServiceBase
    {
        public ServiceHost serviceHost = null;
        public TestWS()
        {
            //InitializeComponent();
            // Name the Windows Service
            ServiceName = "TestWS";
        }

        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
            if (serviceHost != null)
            {
                serviceHost.Close();
            }

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

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

        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.
            if (serviceHost != null)
            {
                serviceHost.Close();
                serviceHost = null;
            }
        }
    }
}

7>Add installer class
8>add required code
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;


namespace WCFWinsowServiceHost
{
    [RunInstaller(true)]
    public partial class TestWSInstaller : System.Configuration.Install.Installer
    {

        private ServiceProcessInstaller process;
        private ServiceInstaller service;
        public TestWSInstaller()
        {
            //InitializeComponent();
            process = new ServiceProcessInstaller();
            process.Account = ServiceAccount.LocalSystem;
            service = new ServiceInstaller();
            service.ServiceName = "WCFTestWS";
            Installers.Add(process);
            Installers.Add(service);

        }
    }
}

9>update program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
namespace WCFWinsowServiceHost
{
    class Program
    {
        public static void Main()
        {
            try
            {
                ServiceBase.Run(new TestWS());

                Console.WriteLine("The service is ready.");
                Console.WriteLine("Press <ENTER> to terminate service.");
                Console.WriteLine();
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error:" + ex.Message.ToString());
                Console.WriteLine();
                Console.ReadLine();
            }
        }
    }
}

10 installutil <exe name>
11 installutil /u <exe name> //to uninstall