Host WCF Websocket Service as Windows Service
Here in this article we will learn how to host a WCF Websocket service as a Windows service.
There are various ways to host a service, but our requirement was to avoid IIS,
and host as a Windows service. I did not find a complete sample showing how to
host a websocket as a Windows service, so here is the sample and steps to host
a Websocket service as a Windows service.
Environment : VS Professional 2012 RC, Win 8 Server
I will break this down into 5 steps:
- WCF Websocket
Service (i.e. Server).
- Windows service
- Client for
testing (i.e. Client)
- Deploy as
Websocket Service (i.e. Server)
new project -> WCF -> WCF Service Library
Name : TestWebsocket
creating a project, delete the default classes created Service1.cs and
a new item (an interface) to the WebsocketServer project (name it
This interface will hold all server call methods.
a reference "System.ServiceModel" to the WebsocketServer project.
another callback interface "IHelloCallback" below IHelloService. This
interface will implement all the callback methods, to call the client.
a [ServiceContract(CallbackContract = typeof(IHelloCallback))] declaration for
the IHelloService interface:
an appropriate Tasks in both the interfaces. I have defined the Hello method
which accepts a string, and returns a string to the client:
a class HelloService.cs; implement the interface IHelloService.
the method implementation what I am doing is just returning the same string
that the client passed by prefixing "You Said:" to it.
last thing remaining is the App.config:
Service name, baseAddress, endpoint contract (Replace IService1 with
IHelloService and Service with HelloService)
enable Websocket, you need to have netHttpBinding for the contract (Replace
basicHttpBinding with netHttpBinding).
This is all you have to do for server.
Windows service project
new project (Windows -> Windows Service) to the solution. (I have named it
and Add Installer. This will add serviceProcessInstaller and serviceInstaller.
the Display Name for serviceInstaller, so that you can see the same name
service running under services.
double-click on Services1.cs in the Solution Explorer and then switch to code
the "System.ServiceModel" and "WebsocketServer" project
references to this project.
next step is the code for the OnStart and OnStop events for the service:
App.config of the WinServ project; copy the
block from the WebsocketServer project and paste it here:
for testing (i.e. Client)
a new project (Windows -> Console Application) to the solution. (I have
named it "WebsocketClient").
- Add a Service
Reference to the client project.
References -> Add Service Reference
- Click on
Discover, and you will see WebsocketServer/HelloService.
- Click OK.
soon as you add the reference, you will see the change in the App.config file
(updated section <system.serviceModel>) with bindings and endpoint.
add a "using System.ServiceModel" to the Program.cs class.
the client, you need to implement a callback handler for the IHelloCallback
interface, since the server will call the same method by passing a processed
complete the client code to invoke the service as shown below:
as a Windows service
the VS 2012 command prompt as an Administrator.
to the path where WinServForWebSocketService.exe is located
the installutil command as: installutil -I
will prompt you for the account under which you need to run the service.
the appropriate user details with appropriate rights to run and click OK.
you see a message like "The transacted install has completed" or
"The installation failed, and the rollback has been performed" then
please check the Windows application logs (Event Viewer (Local) -> Windows
Logs -> Application.).
to Services and check "MyWebsocketTestService", if it's not running,
click on Start to run.
Go to the TestWebsocket\WebsocketClient\bin\Debug folder, and run
WebsocketClient.exe to test your websocket Windows service.
In this article, I discussed how we can host a WCF Websocket service as a