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
Rename the default classes
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
Name the Service Behavior to WcfHelloWorldService.WcfHelloWorldServiceBehavior
Set GetEnabled to false
Select the Behavior Configuration name
Build the project
Create a Windows Service to host the WCF Service
Delete Service.cs
Add a Windows Service called WcfHelloWorldService.cs
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
Change the Account to NetworkService
Change the StartType to Automatic
Set the description for the service in Project Installer
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
From Visual Studio Command Prompt (Run as Administrator)
Open Computer Management and start the new service
WIndows Key + R then type compmgmt.msc
Right click on the new service and “Start”
Test the Service
Add a project to test the service and add a service reference
Add code to call the service
To debug the in Visual Studio stop the service in Computer Management.
Enable WCF Service Hosting in the service project properties
Microsoft WCF Service Host starts when tester application is run is Visual Studio