Service (via TopShelf)
You have been able to host XSockets as a windows service for many years. In the beginning there was a lot of hacking when hosting XSockets as a windows service. Then we put XSockets on Chocolatey which made it easier to get it up and running.
As of XSockets v5 we stopped using chocolatey and instead recomend TopShelf since that will provide full control and easy setup.
Hosting XSockets via TopShelf
This tutorial will show a basic installation of XSockets as a service via TopShelf. Look at the topshelf docs for more information about all the options.
1. Create a new Console Application
I created a new Console Application and named it "XSocketsService".
2. Install the needed packages
Install-Package Topshelf
Install-Package XSockets
3. Add the code
This is the complete sample code. This will take care of starting/stopping XSockets when it runs as a service.
using System;
using Topshelf;
using XSockets.Core.Common.Socket;
using XSockets.Plugin.Framework;
namespace XSocketsService
{
/// <summary>
/// The service class that will stop/start XSockets
/// </summary>
public class XSocketsService
{
private IXSocketServerContainer container;
public XSocketsService()
{
container = Composable.GetExport<IXSocketServerContainer>();
}
public void Start() { container.Start(); }
public void Stop() { container.Stop(); }
}
public class Program
{
public static void Main()
{
//TopShelf configuration
HostFactory.Run(x =>
{
x.Service<XSocketsService>(s =>
{
s.ConstructUsing(name => new XSocketsService());
s.WhenStarted(tc => tc.Start());
s.WhenStopped(tc => tc.Stop());
});
x.RunAsLocalSystem();
x.SetDescription("XSockets Topshelf Host");
x.SetDisplayName("XSocketsService");
x.SetServiceName("XSocketsService");
});
}
}
}
4. Install the service
As you can see in the TopShelf docs you can use the command line to do all the stuff needed.
So to install our XSockets/TopShelf hosted service we just open up a command prompt as administrator and navigate to the executable we just built. In my case I had the project on my desktop.
To install the service we just type nameofyourapplication.exe install
. In my case that would be XSocketsService.exe install
. Then hit enter to install the service.
NOTE: Add the start
parameter after install
to start the service instantly
nameofyourapplication.exe install start
That would output something like
5. You can now see the service under the "local services"
You can control the service from the topshelf command-line or use the UI for the service show below.