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:
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:
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.
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).
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")
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.
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:
7.
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)
1.
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.
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:
6.
Now
complete the client code to invoke the service as shown below:
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
5.
It
will prompt you for the account under which you need to run the service.
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.
Summary
In this article, I discussed how we can host a WCF Websocket service as a
Windows service.