Skip to content

Instantly share code, notes, and snippets.

@yallie
Last active October 26, 2016 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yallie/d9cab09680c98e8a224b9a302f581a8c to your computer and use it in GitHub Desktop.
Save yallie/d9cab09680c98e8a224b9a302f581a8c to your computer and use it in GitHub Desktop.
Zyan service example based on ServiceBase class
// Compile using: csc.exe ServiceExample.cs /r:Zyan.Communication.dll
// Install using: ServiceExample.exe install (run as Administrator)
// Uninstall using: ServiceExample.exe uninstall (run as Administrator)
// Start service: net start ServiceExample (run as Administrator)
// Stop service: net stop ServiceExample (run as Administrator)
// Run client app: ServiceExample.exe test
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
using System.Net.Sockets;
using Zyan.Communication;
namespace ZyanExperiments
{
// Server-side main service class
public class ServiceExample : ServiceBase, IServiceExample
{
public ServiceExample()
{
ServiceName = "ServiceExample";
}
private ZyanComponentHost Host { get; set; }
protected override void OnStart(string[] args)
{
// start the server
Host = new ZyanComponentHost("ServiceExample", 9876);
Host.RegisterComponent<IServiceExample, ServiceExample>(this);
base.OnStart(args);
}
protected override void OnStop()
{
// stop the server
Host.Dispose();
base.OnStop();
}
// implement the interface here
public string Reverse(string s)
{
return new string(s.Reverse().ToArray());
}
}
// Server-side installer class
[RunInstaller(true)]
public class ServiceExampleInstaller : Installer
{
public ServiceExampleInstaller()
{
var processInstaller = new ServiceProcessInstaller();
var serviceInstaller = new ServiceInstaller();
// set the privileges and service name
processInstaller.Account = ServiceAccount.LocalSystem;
serviceInstaller.DisplayName = "ServiceExample: Zyan service example";
serviceInstaller.StartType = ServiceStartMode.Automatic;
// must be the same as set in the service constructor
serviceInstaller.ServiceName = "ServiceExample";
Installers.Add(processInstaller);
Installers.Add(serviceInstaller);
}
}
// =================================
// Shared interface of your singleton service
public interface IServiceExample
{
string Reverse(string s);
}
// =================================
// Hybrid client/server console application
public class TestApplication
{
static void Main(string[] args)
{
// run as server
if (!Environment.UserInteractive)
{
ServiceBase.Run(new ServiceExample());
return;
}
if (!args.Any())
{
Console.WriteLine("Usage: test.exe install | uninstall | test");
return;
}
var action = args.First();
switch (action.ToLower().First())
{
case 'i':
ManagedInstallerClass.InstallHelper(new[] { Assembly.GetExecutingAssembly().Location });
break;
case 'u':
ManagedInstallerClass.InstallHelper(new[] { "/u", Assembly.GetExecutingAssembly().Location });
break;
default:
// run as client
TestService("ReverseMe");
break;
}
}
// client code example
static void TestService(string source)
{
try
{
// connect to server
using (var conn = new ZyanConnection("tcp://localhost:9876/ServiceExample"))
{
// create proxy and call the remote method
var proxy = conn.CreateProxy<IServiceExample>();
var result = proxy.Reverse(source);
Console.WriteLine("Called the remote method. " +
"Source: {0}, result: {1}", source, result);
}
}
catch (SocketException)
{
Console.WriteLine("Service is not running.");
Console.WriteLine("Please install the service using InstallUtil.exe.");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment