protected override void OnStop() { if (this.serviceHost != null) { this.EventLog.WriteEntry("Closing service"); this.serviceHost.Close(); this.serviceHost = null; this.EventLog.WriteEntry("Service closed"); } }
protected override void OnStart(string[] args) { // Sanity check: if we do OnStart -> OnStop -> OnStart, and we do not do set serviceHost to null in OnStop if (this.serviceHost != null) { this.serviceHost.Close(); } this.serviceHost = new ClousotServiceHost(); this.EventLog.WriteEntry(String.Format("Starting service at {0}", String.Join(", ", this.serviceHost.BaseAddresses))); this.serviceHost.AddLogger(new EventLogLineWriter(this.EventLog)); // We open the WCF service this.serviceHost.Open(); this.EventLog.WriteEntry("Service started"); // As it is a windows service, it remains alive }
// This Clousot as Service host started from the command line // Usage: run it, and wait for readline to kill everything // We need a service per box public static void Main() { CloudotLogging.WriteLine("Starting Cloudot service..."); CloudotLogging.WriteLine(); try { using (var host = new ClousotServiceHost()) { host.AddLogger(Console.Out.AsLineWriter()); // TODO: Replace the Console.Out with the equivalent in CloudotLogging host.Open(); CloudotLogging.WriteLine(string.Format("The service is ready and listening at {0}", String.Join(", ", host.BaseAddresses))); CloudotLogging.WriteLine("Press <ENTER> to terminate service."); Console.ReadLine(); host.Close(); } } catch (TimeoutException timeProblem) { CloudotLogging.WriteLine(timeProblem.Message); if (timeProblem.InnerException != null) { CloudotLogging.WriteLine(timeProblem.InnerException.Message); } Console.ReadLine(); } catch (CommunicationException commProblem) { CloudotLogging.WriteLine(commProblem.Message); if (commProblem.InnerException != null) { CloudotLogging.WriteLine(commProblem.InnerException.Message); } Console.ReadLine(); } }