/// <summary>Starts the bus service.</summary> /// <remarks> /// Note: This is a blocking call for Console Applications running in interactive mode. This method will block /// until Ctrl+C is pressed and then initiate a clean shutdown. /// </remarks> /// <exception cref="ArgumentNullException">Thrown when <paramref name="busConfiguration"/> is <c>null</c>. /// </exception> /// <param name="busConfiguration">The bus configuration.</param> public static void Start(BusConfiguration busConfiguration) { if (busConfiguration == null) { throw new ArgumentNullException("busConfiguration"); } _service = new WindowsService(busConfiguration); var entryAssembly = Assembly.GetEntryAssembly(); if (entryAssembly == null) { // invoked by unit test _service.Start(); } else if (HostingEnvironment.IsHosted) { // Hosted in IIS _service.Start(); HostingEnvironment.RegisterObject(_service); } else if (NativeMethods.GetConsoleWindow() != IntPtr.Zero) { // Console application _service.Start(); _wait = new AutoResetEvent(initialState: false); _onCloseCallback = ConsoleCtrlCheck; // prevent callback handler from being GC'd NativeMethods.SetConsoleCtrlHandler(_onCloseCallback, add: true); _wait.WaitOne(); } else if (!Environment.UserInteractive) { // Windows service ServiceBase.Run(new ServiceBase[] { _service }); } else { // Windows application _service.Start(); AppDomain.CurrentDomain.ProcessExit += (s, e) => Stop(); } }
/// <summary>Starts the bus service.</summary> /// <remarks> /// Note: This is a blocking call for Console Applications running in interactive mode. This method will block /// until Ctrl+C is pressed and then initiate a clean shutdown. /// </remarks> /// <exception cref="ArgumentNullException">Thrown when <paramref name="busConfiguration"/> is <c>null</c>. /// </exception> /// <param name="busConfiguration">The bus configuration.</param> public static void Start(BusConfiguration busConfiguration) { if (busConfiguration == null) throw new ArgumentNullException("busConfiguration"); _service = new WindowsService(busConfiguration); var entryAssembly = Assembly.GetEntryAssembly(); if (entryAssembly == null) { // invoked by unit test _service.Start(); } else if (HostingEnvironment.IsHosted) { // Hosted in IIS _service.Start(); HostingEnvironment.RegisterObject(_service); } else if (NativeMethods.GetConsoleWindow() != IntPtr.Zero) { // Console application _service.Start(); _wait = new AutoResetEvent(initialState: false); _onCloseCallback = ConsoleCtrlCheck; // prevent callback handler from being GC'd NativeMethods.SetConsoleCtrlHandler(_onCloseCallback, add: true); _wait.WaitOne(); } else if (!Environment.UserInteractive) { // Windows service ServiceBase.Run(new ServiceBase[] { _service }); } else { // Windows application _service.Start(); AppDomain.CurrentDomain.ProcessExit += (s, e) => Stop(); } }
internal static extern bool SetConsoleCtrlHandler(HandlerRoutineCallback handler, bool add);
public static extern bool SetConsoleCtrlHandler(HandlerRoutineCallback handler, bool add);