private static void Main() { Debug.Listeners.Clear(); Debug.Listeners.Add(new ConsoleTraceListener()); var tokenSource = new CancellationTokenSource(); var container = new HyperServiceHostContainer( () => { var host = new CancellableServiceHost(HyperNodeService.Instance); // When we abort, we want to cancel the service and wait for all child tasks to complete host.RegisterCancellationDelegate( args => { var cancelParams = (CancellationParams)args; HyperNodeService.Instance.Cancel(); HyperNodeService.Instance.WaitAllChildTasks(cancelParams.MillisecondsTimeout, cancelParams.Token); }, new CancellationParams { // TODO: Write about how these settings can be pulled from the app.config settings as a way to customize how long the service will wait after cancellation before proceeding to force a close. MillisecondsTimeout = 100, Timeout = TimeSpan.FromMilliseconds(2000), Token = tokenSource.Token } ); return(host); }, new DefaultServiceHostExceptionHandler() ); Console.WriteLine("Starting service..."); if (!container.Start()) { Console.WriteLine("Failed to start service. Press any key to continue..."); Console.ReadKey(); return; } Console.WriteLine("Service started and is listening on the following addresses:"); foreach (var endpoint in container.Endpoints) { Console.WriteLine(" " + endpoint.Address); } Console.WriteLine("Press any key to stop service..."); Console.ReadKey(); Console.WriteLine("Stopping service..."); container.Stop(); Console.WriteLine("Done."); Thread.Sleep(1000); }
public static void HostingAttempt5() { /* Final product*/ var container = new HyperServiceHostContainer( () => { var host = new CancellableServiceHost(HyperNodeService.Instance); // Inject the cancellation tasks as a registered cancellation delegate. This can be done at any time, even after the host has opened host.RegisterCancellationDelegate( () => { HyperNodeService.Instance.Cancel(); HyperNodeService.Instance.WaitAllChildTasks(); } ); return(host); }, new DefaultServiceHostExceptionHandler() // Inject the exception hanlders we want to use ); Console.WriteLine("Starting service..."); // Start() returns a boolean indicating whether the service was able to be started. This allows graceful recovery from failure to start properly. if (!container.Start()) { Console.WriteLine("Failed to start service. Press any key to continue..."); Console.ReadKey(); return; } Console.WriteLine("Service started and is listening on the following addresses:"); foreach (var endpoint in container.Endpoints) { Console.WriteLine(" " + endpoint.Address); } // Wait for user input to shutdown Console.ReadKey(); /* Simply call Stop() to handle all details of closing, including disposing the service and calling abort or close appropriately. * The container doesn't care what type of ServiceHost it is working with, but if it happens to be a CancellableServiceHost, * calling Abort() or Close() will automatically call Cancel() as discussed above. This ensures that our cancellation delegates * are executed as expected. */ container.Stop(); Console.ReadKey(); }
private static void Main() { Debug.Listeners.Clear(); Debug.Listeners.Add(new ConsoleTraceListener()); var container = new HyperServiceHostContainer( () => { var host = new CancellableServiceHost(HyperNodeService.Instance); host.RegisterCancellationDelegate(HyperNodeService.Instance.Cancel); return(host); }, new DefaultServiceHostExceptionHandler() ); Console.WriteLine("Starting service..."); if (!container.Start()) { Console.WriteLine("Failed to start service. Press any key to continue..."); Console.ReadKey(); return; } Console.WriteLine("Service started and is listening on the following addresses:"); foreach (var endpoint in container.Endpoints) { Console.WriteLine(" " + endpoint.Address); } Console.WriteLine("Press any key to stop service..."); Console.ReadKey(); Console.WriteLine("Stopping service..."); container.Stop(); Console.WriteLine("Done."); Thread.Sleep(1000); }
public static void HostingAttempt5() { /* Final product*/ var container = new HyperServiceHostContainer( () => { var host = new CancellableServiceHost(HyperNodeService.Instance); // Inject the cancellation tasks as a registered cancellation delegate. This can be done at any time, even after the host has opened host.RegisterCancellationDelegate( () => { HyperNodeService.Instance.Cancel(); HyperNodeService.Instance.WaitAllChildTasks(); } ); return host; }, new DefaultServiceHostExceptionHandler() // Inject the exception hanlders we want to use ); Console.WriteLine("Starting service..."); // Start() returns a boolean indicating whether the service was able to be started. This allows graceful recovery from failure to start properly. if (!container.Start()) { Console.WriteLine("Failed to start service. Press any key to continue..."); Console.ReadKey(); return; } Console.WriteLine("Service started and is listening on the following addresses:"); foreach (var endpoint in container.Endpoints) { Console.WriteLine(" " + endpoint.Address); } // Wait for user input to shutdown Console.ReadKey(); /* Simply call Stop() to handle all details of closing, including disposing the service and calling abort or close appropriately. * The container doesn't care what type of ServiceHost it is working with, but if it happens to be a CancellableServiceHost, * calling Abort() or Close() will automatically call Cancel() as discussed above. This ensures that our cancellation delegates * are executed as expected. */ container.Stop(); Console.ReadKey(); }