Created
October 20, 2015 14:35
-
-
Save randyburden/f845a306c29bc72c4c7b to your computer and use it in GitHub Desktop.
This is a working example of using the Nano micro web framework hosted as a Windows Service using the System.ServiceProcess .NET dependency to create a low dependency, low memory footprint website or web API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Threading; | |
using Nano.Web.Core; | |
using Nano.Web.Core.Host.HttpListener; | |
namespace NanoWindowsServiceExample | |
{ | |
public class Program | |
{ | |
public class WindowsService : System.ServiceProcess.ServiceBase | |
{ | |
private readonly Action _onStart; | |
private readonly Action _onStop; | |
public WindowsService(string serviceName, Action onStart, Action onStop ) | |
{ | |
_onStart = onStart; | |
_onStop = onStop; | |
ServiceName = serviceName; | |
EventLog.Log = "Application"; | |
CanStop = true; | |
} | |
protected override void OnStart(string[] args) | |
{ | |
_onStart(); | |
} | |
protected override void OnStop() | |
{ | |
_onStop(); | |
} | |
public static void Start(string serviceName, Action onStart, Action onStop) | |
{ | |
if (!Environment.UserInteractive) | |
{ | |
// Running as a Windows Service | |
using (var service = new WindowsService(serviceName, onStart, onStop)) | |
{ | |
System.ServiceProcess.ServiceBase.Run(service); | |
} | |
} | |
else | |
{ | |
var exitEvent = new ManualResetEvent(false); | |
// Running as Console application | |
Console.CancelKeyPress += (sender, eventArgs) => | |
{ | |
eventArgs.Cancel = true; | |
exitEvent.Set(); | |
}; | |
onStart(); | |
Console.WriteLine("Press Ctrl+C to exit."); | |
exitEvent.WaitOne(); | |
onStop(); | |
} | |
} | |
} | |
/* | |
Windows Service Install, Start, Stop, Query, Delete Commands: | |
============================================================= | |
sc create "NanoWindowsServiceExample" binPath= "C:\AAA\NanoWindowsServiceExample.exe ApplicationName NanoWindowsServiceExample Uri http://localhost:8686" start= auto | |
sc start "NanoWindowsServiceExample" | |
sc stop "NanoWindowsServiceExample" | |
sc query "NanoWindowsServiceExample" | |
sc delete "NanoWindowsServiceExample" | |
*/ | |
private static void Main( string[] args ) | |
{ | |
var applicationName = GetArgumentValue( args, "ServiceName" ) ?? GetArgumentValue(args, "ApplicationName") ?? AppDomain.CurrentDomain.FriendlyName; | |
var url = GetArgumentValue(args, "Url") ?? GetArgumentValue(args, "Uri") ?? "http://localhost:7474"; | |
WindowsService.Start(applicationName, () => Startup.Start(url, applicationName), Startup.Stop ); | |
} | |
private static string GetArgumentValue( string[] arguments, string argumentName ) | |
{ | |
for ( int i = 0; i < arguments.Length; i++ ) | |
{ | |
var argument = arguments[ i ].ToLower(); | |
if ( argument.ToLower().Contains( argumentName.ToLower() ) ) | |
{ | |
if ( argument.Length >= i + 1 ) | |
return arguments[ i + 1 ]; | |
} | |
} | |
return null; | |
} | |
} | |
public static class Startup | |
{ | |
private static HttpListenerNanoServer _server; | |
public static void Start( string url, string applicationName ) | |
{ | |
var nanoConfiguration = new NanoConfiguration(); | |
nanoConfiguration.AddMethods<Clock>(); | |
nanoConfiguration.AddDirectory( "/", "www", returnHttp404WhenFileWasNotFound: true ); | |
nanoConfiguration.ApplicationName = applicationName; | |
_server = HttpListenerNanoServer.Start(nanoConfiguration, url); | |
if (Debugger.IsAttached) | |
Process.Start(_server.HttpListenerConfiguration.GetFirstUrlBeingListenedOn().TrimEnd('/') + "/ApiExplorer/"); | |
Console.WriteLine("Nano Server is running on: " + _server.HttpListenerConfiguration.GetFirstUrlBeingListenedOn()); | |
} | |
public static void Stop() | |
{ | |
if (_server.HttpListenerConfiguration.HttpListener.IsListening) | |
{ | |
_server.HttpListenerConfiguration.HttpListener.Stop(); | |
} | |
} | |
} | |
/// <summary>Provides clock-related functionality.</summary> | |
public class Clock | |
{ | |
/// <summary>Gets the current date and time.</summary> | |
/// <returns>Current date and time.</returns> | |
public static DateTime GetCurrentDateTime() | |
{ | |
return DateTime.Now; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment