private static void MainFunction(int numArgs, IntPtr argPtrPtr) { Options opt = new Options(); opt.TraceLevel = Options.LogLevel.Verbose; opt.TraceFile = "C:\\temp\\ttt.log"; opt.LogInformation("MainFunction: " + numArgs.ToString()); var startupArguments = ParseArguments(numArgs, argPtrPtr); serviceStatusHandle = Win32ServiceInterop.Wrapper.RegisterServiceCtrlHandlerExW(ServiceName, serviceControlHandlerDelegate, IntPtr.Zero); if (serviceStatusHandle.IsInvalid) { resultException = new Win32Exception(Marshal.GetLastWin32Error()); return; } ReportServiceStatus(ServiceState.StartPending, ServiceAcceptedControlCommandsFlags.None, win32ExitCode: 0, waitHint: 3000); try { OnStart(startupArguments, ReportServiceStatus); } catch { ReportServiceStatus(ServiceState.Stopped, ServiceAcceptedControlCommandsFlags.None, win32ExitCode: -1, waitHint: 0); } }
private void ServiceMainFunction(int numArgs, IntPtr argPtrPtr) { Log.Informational("Entering service main function."); serviceStatusHandle = Win32.RegisterServiceCtrlHandlerExW(this.serviceName, (ServiceControlHandler)this.HandleServiceControlCommand, IntPtr.Zero); if (serviceStatusHandle.IsInvalid) { stopTaskCompletionSource.SetException(new Win32Exception(Marshal.GetLastWin32Error())); return; } ReportServiceStatus(ServiceState.StartPending, ServiceAcceptedControlCommandsFlags.None, win32ExitCode: 0, waitHint: 3000); try { Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); Gateway.GetDatabaseProvider += Program.GetDatabase; Gateway.RegistrationSuccessful += Program.RegistrationSuccessful; if (!Gateway.Start(true).Result) { throw new Exception("Gateway being started in another process."); } ReportServiceStatus(ServiceState.Running, ServiceAcceptedControlCommandsFlags.Stop, win32ExitCode: 0, waitHint: 0); } catch (Exception ex) { Log.Critical(ex); ReportServiceStatus(ServiceState.Stopped, ServiceAcceptedControlCommandsFlags.None, win32ExitCode: -1, waitHint: 0); } }
private void GivenRegisteringServiceControlHandlerIsImpossible() { var statusHandle = new ServiceStatusHandle { NativeInterop = nativeInterop }; A.CallTo(() => nativeInterop.RegisterServiceCtrlHandlerExW(A <string> ._, A <ServiceControlHandler> ._, A <IntPtr> ._)) .Returns(statusHandle); }
internal static extern bool SetServiceStatus(ServiceStatusHandle statusHandle, ref ServiceStatus pServiceStatus);
bool Win32ServiceInterface.SetServiceStatus(ServiceStatusHandle statusHandle, ref ServiceStatus pServiceStatus) { return(SetServiceStatus(statusHandle, ref pServiceStatus)); }