Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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();
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        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();
        }
Ejemplo n.º 5
0
        public static void HostingAttempt4()
        {
            /* Now using the CancellableServiceHost, we get this*/
            IDisposable disposableService = null;
            try
            {
                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();
                    }
                );

                Console.WriteLine("Starting service...");
                host.Open();

                Console.WriteLine("Service started and is listening on the following addresses:");
                foreach (var endpoint in host.Description.Endpoints)
                {
                    Console.WriteLine("    " + endpoint.Address);
                }

                // Wait for user input to shutdown
                Console.ReadKey();

                // Save a reference to the singleton instance of the service and safe cast to IDisposable
                disposableService = host.SingletonInstance as IDisposable;

                /* Check for Faulted state and make the appropriate call to Abort() or Close().
                 * You can call Cancel() yourself if you prefer, but both Abort() and Close() call
                 * the Cancel() method if cancellation hasn't already been reuested.
                 */
                if (host.State == CommunicationState.Faulted)
                    host.Abort();
                else
                    host.Close();
            }
            catch (TimeoutException exTimeout)
            {
                // Handle TimeoutException
            }
            catch (CommunicationException exCommunication)
            {
                // Handle CommunicationException
            }
            catch (Exception ex)
            {
                // Handle other generic exceptions
                Console.WriteLine(ex);
            }
            finally
            {
                // Dispose of our service if applicable
                if (disposableService != null)
                    disposableService.Dispose();
            }

            Console.ReadKey();

            /* Problem summary:
             *
             * 1) Just one more thing to do: wrap in the HyperServiceHostContainer
             */
        }
Ejemplo n.º 6
0
        public static void HostingAttempt4()
        {
            /* Now using the CancellableServiceHost, we get this*/
            IDisposable disposableService = null;

            try
            {
                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();
                }
                    );

                Console.WriteLine("Starting service...");
                host.Open();

                Console.WriteLine("Service started and is listening on the following addresses:");
                foreach (var endpoint in host.Description.Endpoints)
                {
                    Console.WriteLine("    " + endpoint.Address);
                }

                // Wait for user input to shutdown
                Console.ReadKey();

                // Save a reference to the singleton instance of the service and safe cast to IDisposable
                disposableService = host.SingletonInstance as IDisposable;

                /* Check for Faulted state and make the appropriate call to Abort() or Close().
                 * You can call Cancel() yourself if you prefer, but both Abort() and Close() call
                 * the Cancel() method if cancellation hasn't already been reuested.
                 */
                if (host.State == CommunicationState.Faulted)
                {
                    host.Abort();
                }
                else
                {
                    host.Close();
                }
            }
            catch (TimeoutException exTimeout)
            {
                // Handle TimeoutException
            }
            catch (CommunicationException exCommunication)
            {
                // Handle CommunicationException
            }
            catch (Exception ex)
            {
                // Handle other generic exceptions
                Console.WriteLine(ex);
            }
            finally
            {
                // Dispose of our service if applicable
                if (disposableService != null)
                {
                    disposableService.Dispose();
                }
            }

            Console.ReadKey();

            /* Problem summary:
             *
             * 1) Just one more thing to do: wrap in the HyperServiceHostContainer
             */
        }