Host WCF Websocket Service as 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 WebsocketService (i.e. Server).
  • Windows serviceproject.
  • Client fortesting (i.e. Client)
  • Deploy asWindows service
  • Testing

WCF Websocket Service (i.e. Server)

  1. Createnew project -> WCF -> WCF Service Library
    • SolutionName : TestWebsocket
    • ProjectName:WebsocketServer
  2. Aftercreating a project, delete the default classes created Service1.cs andIService1.cs.
  3. Adda new item (an interface) to the WebsocketServer project (name itIHelloService.cs).This interface will hold all server call methods.
  4. Adda reference "System.ServiceModel" to the WebsocketServer project.
  5. Declareanother callback interface "IHelloCallback" below IHelloService. Thisinterface will implement all the callback methods, to call the client.
  6. Adda [ServiceContract(CallbackContract = typeof(IHelloCallback))] declaration forthe IHelloService interface:


  7. Definean appropriate Tasks in both the interfaces. I have defined the Hello methodwhich accepts a string, and returns a string to the client:


  8. Adda class HelloService.cs; implement the interface IHelloService.
  9. Inthe method implementation what I am doing is just returning the same stringthat the client passed by prefixing "You Said:" to it.


  10. Thelast thing remaining is the App.config:
    • UpdateService name, baseAddress, endpoint contract (Replace IService1 withIHelloService and Service with HelloService)
    • Toenable Websocket, you need to have netHttpBinding for the contract (ReplacebasicHttpBinding with netHttpBinding).

This is all you have to do for server.

Windows service project

  1. Addnew project (Windows -> Windows Service) to the solution. (I have named itas "WinServForWebSocketService")


  2. Right-clickand Add Installer. This will add serviceProcessInstaller and serviceInstaller.
  3. Setthe Display Name for serviceInstaller, so that you can see the same nameservice running under services.


  4. Nowdouble-click on Services1.cs in the Solution Explorer and then switch to codeview.
  5. Addthe "System.ServiceModel" and "WebsocketServer" projectreferences to this project.
  6. Thenext step is the code for the OnStart and OnStop events for the service:


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

  1. Adda new project (Windows -> Console Application) to the solution. (I havenamed it "WebsocketClient").
  2. Add a ServiceReference to the client project.
    • Right-clickReferences -> Add Service Reference
    • Click onDiscover, and you will see WebsocketServer/HelloService.
    • Click OK.

  3. Assoon as you add the reference, you will see the change in the App.config file(updated section <system.serviceModel>) with bindings and endpoint.
  4. Nowadd a "using System.ServiceModel" to the Program.cs class.
  5. Inthe client, you need to implement a callback handler for the IHelloCallbackinterface, since the server will call the same method by passing a processedvalue:


  6. Nowcomplete the client code to invoke the service as shown below:


Deploy as a Windows service

  1. Buildthe solution.
  2. Openthe VS 2012 command prompt as an Administrator.
  3. Goto the path where WinServForWebSocketService.exe is located(..TestWebsocket\WinServForWebSocketService\bin\Debug)
  4. Runthe installutil command as: installutil -I


  5. Itwill prompt you for the account under which you need to run the service.

  6. Enterthe appropriate user details with appropriate rights to run and click OK.
  7. Ifyou see a message like "The transacted install has completed" or"The installation failed, and the rollback has been performed" thenplease check the Windows application logs (Event Viewer (Local) -> WindowsLogs -> Application.).
  8. Goto 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 Windows service.