WCF NetTcp Hosted in Windows Service

 

Steps for a simple WCF Service with netTCPBinding hosted in a Windows Service

Microsoft – How to: Host WCF in a Windows Service Using TCP

Microsoft – NetTcpBinding Class

 

Create a new project with a WCF Service Library

image

 

Rename the default classes

image

 

Change ServiceContract

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfHelloWorldService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IWcfHelloWorld
{
[OperationContract]
string HelloWorld(string name);

}
}

Change Method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfHelloWorldService
{
// 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 WcfHelloWorld : IWcfHelloWorld
{
public string HelloWorld(string name)
{
return string.Format("Hello {0}", name);
}



}
}

 

Configure the config to use net

image

 

image

 

image

 

Name the Service Behavior to WcfHelloWorldService.WcfHelloWorldServiceBehavior

image

 

Set GetEnabled to false

image

 

Select the Behavior Configuration name

image

 

Build the project

 

Create a Windows Service to host the WCF Service

image

 

Delete Service.cs

Add a Windows Service called WcfHelloWorldService.cs

image

 

Make sure Program.cs is instantiating the Service just created

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace WcfHelloWorldServiceHost
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new WcfHelloWorldServiceHost.WcfHelloWorldService() //the windows service
};
ServiceBase.Run(ServicesToRun);
}
}
}

 

Add references to the newly created WCF Service and System.ServiceModel

Change the code in WcfHelloWorldService.cs to the code below

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.Threading.Tasks;
using System.ServiceModel;
using WcfHelloWorldService;

namespace WcfHelloWorldServiceHost
{
partial class WcfHelloWorldService : ServiceBase
{
internal static ServiceHost myServiceHost = null; //declare a service host
public WcfHelloWorldService()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
if (myServiceHost != null)
{
myServiceHost.Close();
}
myServiceHost = new ServiceHost(typeof(WcfHelloWorld));//service host of the scf service
myServiceHost.Open();
}
protected override void OnStop()
{
if (myServiceHost != null)
{
myServiceHost.Close(); //close the host
myServiceHost = null;
}
}

}
}

 

Right click on Service1.cs[design] and click “Add Installer”

ProjectInstaller.cs[design] is created

image

 

Change the Account to NetworkService

image

 

Change the StartType to Automatic

image

 

Set the description for the service in Project Installer

image

 

Install the Windows Service

Copy service .dll, the service application and the configuration file from the visual studio bin to the location where the service will be stored

image

 

From Visual Studio Command Prompt (Run as Administrator)

image

 

 

Open Computer Management and start the new service

WIndows Key + R then type compmgmt.msc

Right click on the new service and “Start”

image

 

 

Test the Service

Add a project to test the service and add a service reference

image

 

Add code to call the service

image

 

To debug the in Visual Studio stop the service in Computer Management.

image

 

Enable WCF Service Hosting in the service project properties

image

 

Microsoft WCF Service Host starts when tester application is run is Visual Studio

image