articles

Home / DeveloperSection / Articles / Host WCF in a Windows Service Using TCP

Host WCF in a Windows Service Using TCP

Host WCF in a Windows Service Using TCP

Anonymous User20300 18-Apr-2012

This article walks you through the process of hosting a WCF service within a Microsoft Windows service. i.e.  This article is going to explain how to host WCF in a Windows Service using TCP protocol.

To host WCF in a Windows Service using TCP, you’ve to follow up some steps;
  1.       Create a WCF Service
  2.       Configure the WCF Endpoints to use TCP and Set the Base Address
  3.       Create a Windows Service
  4.       Add the Service Installers to the Windows Service
  5.       Modify the Windows Service to host the WCF Service
  6.       Install the Windows Service
  7.       Create a Windows Forms Test Client Application
  8.       Add a WCF Service Reference to the Client
  9.       Test the Client and WCF Service

Let’s take a brief idea about these steps;

Step 1: Create a WCF Service

In this step, you create a WCF service to test hosting in a Windows service.

Getting Started:

1.       Open Visual Studio and go to File -> New -> Project

2.       In the Add New Project, click on WCF template section and select WCF Service Library Option

3.       Click on button ‘Ok’

Host WCF in a Windows Service Using TCP

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).

Getting Started:

1.       Right-click the App.config file of the WCF Service Library project and then click Edit WCF Configuration.

Host WCF in a Windows Service Using TCP

2.       In the Configuration Editor, in the configuration section, expand Services and then expand Endpoints.

Host WCF in a Windows Service Using TCP

3.       Select the first endpoint. Under Endpoint Properties, change the Binding from wsHttpBinding to netTcpBinding.

Host WCF in a Windows Service Using TCP

4.       Select the second endpoint. Under Endpoint Properties, change the Binding from mexHttpBinding to mexTcpBinding.

Host WCF in a Windows Service Using TCP

5.       Under Service, select the Host node, select the default address under the BaseAddress list, and then click Edit.

Host WCF in a Windows Service Using TCP

Here, set the default base address is net.tcp://localhost:8523/Service1

6.       Under Advanced, expand the tree under Service Behaviors. Select serviceMetadata and change httpGetEnabled from True to False.  

Host WCF in a Windows Service Using TCP

7.       Click File and then click ‘Save’ to save your configuration changes.

Step 3: Create a Windows Service

In this step, you add a Windows Service project to your solution.

Getting Started:

1.       Right-click your solution, clicks Add, and then clicks New Project.

Host WCF in a Windows Service Using TCP

2.       In the Add New Project dialog box, select Windows, and then select Windows Service.

Host WCF in a Windows Service Using TCP

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.

Host WCF in a Windows Service Using TCP

Step 4: Add the Service Installers to the Windows Service

In this step, you add service installers to your Windows service.

Getting Started:

1.       Right-click Service1.cs and then click View Designer.

2.       Right-click the designer view and then click Add Installer.

3.       This adds the ‘ProjectInstaller.cs’ file with two objects, serviceProcessInstaller1 and serviceInstaller1.

4.       In the Design view of ‘ProjectInstaller.cs’, right-click serviceProcessInstaller1 and then click Properties.

5.       In the Properties pane, set the Account attribute to ‘NetworkService’.

6.       Right-click serviceInstaller1 and then click Properties.

7.       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.

Code of Service1.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

 

namespace MyWCFServiceLibrary

{

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.

    public class Service1 : IService1

    {

        public string GetData(intvalue)

        {

            return string.Format("You entered: {0}"value);

        }

 

 

        public string GetPersonName(stringpName)

        {

            return "Arun Singh your service run perfectly";

        }

        public CompositeType GetDataUsingDataContract(CompositeTypecomposite)

        {

            if (composite== null)

            {

                throw new ArgumentNullException("composite");

            }

            if (composite.BoolValue)

            {

                composite.StringValue+="Suffix";

            }

            return composite;

        }

    }

} 


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.

Step 7: Create a Windows Forms Test Client Application

In this step, you create a Windows Forms application named TestClient that you will use to test the WCF service.

1.       Right-click your solution, clicks Add, and then clicks New Project.

2.       In the Add New Project dialog box, in the Templates section, select Windows Application.

3.       In the Name field, type TestClient 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 (This is the base address which you had added in step 3)

Step 9: Test the Client and WCF Service

Now simply create your windows form application and you can easily add this service in your application by using following code:

ServiceReference1.Service1Client myService = new ServiceReference1.Service1Client(); 

This is the complete description about how to host WCF in a Windows Service using TCP. I hope you will enjoy this article. I’m very thankful for using this article.


Updated 20-Jan-2021
I am a content writter !

Leave Comment

Comments

Liked By