Introduction
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 project.
- Client for testing (i.e. Client)
- Deploy as Windows service
- Testing
WCF Websocket Service (i.e. Server)
- Create new project -> WCF -> WCF Service Library
- Solution Name : TestWebsocket
- ProjectName: WebsocketServer
- After creating a project, delete the default classes created Service1.cs and IService1.cs.
- Add a new item (an interface) to the WebsocketServer project (name it IHelloService.cs).This interface will hold all server call methods.
- Add a reference "System.ServiceModel" to the WebsocketServer project.
- Declare another callback interface "IHelloCallback" below IHelloService. This interface will implement all the callback methods, to call the client.
- Add
a [ServiceContract(CallbackContract = typeof(IHelloCallback))] declaration for
the IHelloService interface:

- Define
an appropriate Tasks in both the interfaces. I have defined the Hello method
which accepts a string, and returns a string to the client:

- Add a class HelloService.cs; implement the interface IHelloService.
- In
the method implementation what I am doing is just returning the same string
that the client passed by prefixing "You Said:" to it.

- The last thing remaining is the App.config:
- Update Service name, baseAddress, endpoint contract (Replace IService1 with IHelloService and Service with HelloService)
- To
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
- Add
new project (Windows -> Windows Service) to the solution. (I have named it
as "WinServForWebSocketService")

- Right-click and Add Installer. This will add serviceProcessInstaller and serviceInstaller.
- Set
the Display Name for serviceInstaller, so that you can see the same name
service running under services.

- Now double-click on Services1.cs in the Solution Explorer and then switch to code view.
- Add the "System.ServiceModel" and "WebsocketServer" project references to this project.
- The
next step is the code for the OnStart and OnStop events for the service:

- Update
App.config of the WinServ project; copy the
<system.serviceModel>...</system.serviceModel> block from the WebsocketServer project and paste it here:

Client for testing (i.e. Client)
- Add a new project (Windows -> Console Application) to the solution. (I have named it "WebsocketClient").
- Add a Service
Reference to the client project.
- Right-click References -> Add Service Reference
- Click on Discover, and you will see WebsocketServer/HelloService.
- Click OK.

- As soon as you add the reference, you will see the change in the App.config file (updated section <system.serviceModel>) with bindings and endpoint.
- Now add a "using System.ServiceModel" to the Program.cs class.
- In
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
value:

- Now
complete the client code to invoke the service as shown below:

Deploy as a Windows service
- Build the solution.
- Open the VS 2012 command prompt as an Administrator.
- Go to the path where WinServForWebSocketService.exe is located (..TestWebsocket\WinServForWebSocketService\bin\Debug)
- Run
the installutil command as: installutil -I

- It
will prompt you for the account under which you need to run the service.

- Enter the appropriate user details with appropriate rights to run and click OK.
- If 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.).
- Go to Services and check "MyWebsocketTestService", if it's not running, click on Start to run.
Testing
Go to the TestWebsocket\WebsocketClient\bin\Debug folder, and run
WebsocketClient.exe to test your websocket Windows service.
Summary
In this article, I discussed how we can host a WCF Websocket service as a
Windows service.

Jaison ThomasPosted Apr 2, 2020, 1:19 AM
Hi Bhushan, I cannot add service reference when I create the client.
Bhushan BhurePosted Dec 31, 2012, 12:20 PM
Miryam: Just added source code, sorry forgot to add before.
Bhushan BhurePosted Dec 31, 2012, 12:16 PM
lamStalker:If you are not running on Websocket supported platform, you will get that error. Try running on Win8 (mentioned in Environment section).
IamStalkerPosted Dec 31, 2012, 11:09 AM
What no answer?! Can any one help please?
IamStalkerPosted Dec 28, 2012, 5:06 PM
I recive this error. System.PlatformNotSupportedException: This platform does not support server side WebSockets.
Manish SharmaPosted Jul 26, 2012, 11:02 AM
Thanks for providing such wonderful information about WCF Websocket Service. This is helpful to know about way to host a Websocket service as a Windows service.
Aman SinghalPosted Jul 26, 2012, 10:44 AM
Cong! Bhushan , Nice Starting Keep It Up
Bhushan BhurePosted Jul 25, 2012, 10:44 PM
Thanks Dinesh
Dinesh BeniwalPosted Jul 25, 2012, 9:21 PM
Good start bhushan