ARTICLE

Host WCF Websocket Service as Windows Service

Posted by Bhushan Bhure Articles | WCF with C# July 25, 2012
Here in this article we will learn how to host a WCF Websocket service as a Windows service.
Reader Level:
Download Files:
 

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)

1.     Create new project -> WCF -> WCF Service Library

o   Solution Name : TestWebsocket

o   ProjectName: WebsocketServer

2.     After creating a project, delete the default classes created Service1.cs and IService1.cs.

3.     Add a new item (an interface) to the WebsocketServer project (name it IHelloService.cs).

This interface will hold all server call methods.

4.     Add a reference "System.ServiceModel" to the WebsocketServer project.

5.     Declare another callback interface "IHelloCallback" below IHelloService. This interface will implement all the callback methods, to call the client.

6.     Add a [ServiceContract(CallbackContract = typeof(IHelloCallback))] declaration for the IHelloService interface:

webscoket1.gif

7.     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:
webscoket2.gif

8.     Add a class HelloService.cs; implement the interface IHelloService.

9.     In the method implementation what I am doing is just returning the same string that the client passed by prefixing "You Said:" to it.

webscoket3.gif

10.   The last thing remaining is the App.config:

o   Update Service name, baseAddress, endpoint contract (Replace IService1 with IHelloService and Service with HelloService)

o   To enable Websocket, you need to have netHttpBinding for the contract (Replace basicHttpBinding with netHttpBinding).

webscoket4a.gif

This is all you have to do for server.

Windows service project

1.     Add new project (Windows -> Windows Service) to the solution. (I have named it as "WinServForWebSocketService")

webscoket5.gif

2.     Right-click and Add Installer. This will add serviceProcessInstaller and serviceInstaller.

3.     Set the Display Name for serviceInstaller, so that you can see the same name service running under services.

webscoket6.gif

4.     Now double-click on Services1.cs in the Solution Explorer and then switch to code view.

5.     Add the "System.ServiceModel" and "WebsocketServer" project references to this project.

6.     The next step is the code for the OnStart and OnStop events for the service:

webscoket7.gif

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

webscoket8.gif

Client for testing (i.e. Client)

1.     Add a new project (Windows -> Console Application) to the solution. (I have named it "WebsocketClient").

  1. 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.

webscoket9.gif

3.     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.

4.     Now add a "using System.ServiceModel" to the Program.cs class.

5.     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:

webscoket10.gif

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

webscoket11.gif

Deploy as a Windows service

1.     Build the solution.

2.     Open the VS 2012 command prompt as an Administrator.

3.     Go to the path where WinServForWebSocketService.exe is located (..TestWebsocket\WinServForWebSocketService\bin\Debug)

4.     Run the installutil command as: installutil -I

websocket12.gif

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

webscoket13.gif

6.     Enter the appropriate user details with appropriate rights to run and click OK.

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

8.     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.

webscoket14.gif

Summary

In this article, I discussed how we can host a WCF Websocket service as a Windows service.

 

Login to add your contents and source code to this article
post comment
     

Miryam: Just added source code, sorry forgot to add before.

Posted by Bhushan Bhure Dec 31, 2012

lamStalker:If you are not running on Websocket supported platform, you will get that error. Try running on Win8 (mentioned in Environment section).

Posted by Bhushan Bhure Dec 31, 2012

What no answer?! Can any one help please?

Posted by IamStalker Dec 31, 2012

I recive this error. System.PlatformNotSupportedException: This platform does not support server side WebSockets.

Posted by IamStalker Dec 28, 2012

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.

Posted by Manish Sharma Jul 26, 2012
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter