static public void Init(string[] messageWriteAccess) { if (instance != null) { return; } instance = new PSMessageAgent(); instance.pipeSec = new PipeSecurity(); foreach (string accessor in messageWriteAccess) { instance.pipeSec.AddAccessRule( new PipeAccessRule( new NTAccount(accessor), PipeAccessRights.ReadWrite | PipeAccessRights.Synchronize, AccessControlType.Allow ) ); } asyncThread = new Thread(new ThreadStart(instance.RunAsync)); asyncThread.Start(); while (!asyncThread.IsAlive) { ; } }
protected override void OnStart(string[] args) { WriteToServiceLog("Starting."); ServiceStatus.dwServiceType = ServiceType.SERVICE_WIN32_OWN_PROCESS; ServiceStatus.dwCurrentState = ServiceState.SERVICE_START_PENDING; ServiceStatus.dwWin32ExitCode = 0; ServiceStatus.dwWaitHint = 2000; SetServiceStatus(ServiceHandle, ref ServiceStatus); try { WriteToServiceLog("Loading service definition from embedded resources."); string svcCfgCmd = GetResourceAsString("PSServiceManager.ServiceDefinition.ConfigurationCommand.ps1"); string svcDefEmbedded = GetResourceAsString("PSServiceManager.ServiceDefinition.ps1"); if (_SourceIsAvailable) { string svcDefFromFile = File.ReadAllText( Path.Combine( _InstallPath, _ServiceName + ".ServiceDefinition.ps1" ), Encoding.UTF8 ); if (svcDefEmbedded != svcDefFromFile) { throw new Exception("The embedded service definition did not match the content of the exposed definition file."); } } using (Runspace defRs = RunspaceFactory.CreateRunspace()) { defRs.Open(); defRs.CreatePipeline(svcCfgCmd).Invoke(); ServiceDefinition = defRs.CreatePipeline(svcDefEmbedded).Invoke()[0]; ServiceScript_Process = (string)ServiceDefinition.Properties["Process"].Value; } WriteToServiceLog("Initializing runspace."); ServiceRunspace = RunspaceFactory.CreateRunspace(); ServiceRunspace.Open(); ServiceRunspace.CreatePipeline("$ErrorActionPreference = \"Stop\"").Invoke(); ServiceRunspace.CreatePipeline("function Write-Log ($message) {[PSServiceManager.PSService]::Instance.WriteToServiceLogFromScript($message)}").Invoke(); Pipeline pl = ServiceRunspace.CreatePipeline(); Command svcCmd = new Command("param($service)", true); svcCmd.Parameters.Add(new CommandParameter("service", new { DataPath = _DataPath })); pl.Commands.Add(svcCmd); pl.Invoke(); string begin = (string)ServiceDefinition.Properties["Begin"].Value; if (begin.Length > 0) { WriteToServiceLog("Invoking 'Begin'. ["); ServiceRunspace.CreatePipeline(begin).Invoke(); WriteToServiceLog("] Finished 'Begin'."); } else { WriteToServiceLog("Would invoke 'Begin' if defined."); } WriteToServiceLog("Initializing process trigger(s)."); if ((bool)ServiceDefinition.Properties["ProcessOnTimer"].Value) { System.Timers.Timer timer = new System.Timers.Timer(); timer.Interval = (double)ServiceDefinition.Properties["TimerInterval"].Value; timer.Elapsed += new System.Timers.ElapsedEventHandler(proc_OnInterval); timer.Start(); } if ((bool)ServiceDefinition.Properties["ProcessOnMessage"].Value) { PSMessageAgent.Init((string[])ServiceDefinition.Properties["MessageWriteAccess"].Value); PSMessageAgent.GetInstance().HasMessage += proc_OnMessage; while (!PSMessageAgent.GetInstance().IsReady) { continue; } } WriteToServiceLog("Started."); ServiceStatus.dwCurrentState = ServiceState.SERVICE_RUNNING; } catch (ActionPreferenceStopException e) { WriteToServiceLog("Exception: " + e.ErrorRecord.Exception.Message); ServiceStatus.dwWin32ExitCode = (int)(Win32Error.ERROR_APP_INIT_FAILURE); throw e; } catch (Exception e) { WriteToServiceLog("Exception: " + e.Message); ServiceStatus.dwWin32ExitCode = (int)(Win32Error.ERROR_APP_INIT_FAILURE); throw e; } finally { ServiceStatus.dwWaitHint = 0; SetServiceStatus(ServiceHandle, ref ServiceStatus); } }