using System; using System.Threading; using System.Diagnostics; using System.ServiceModel; using System.ServiceModel.Description; namespace DaoMqPump2 { class PumpService : System.ServiceProcess.ServiceBase { const int INTERVAL = 5; TimeSpan m_delay = new TimeSpan(0, 0, 0, INTERVAL, 0); private Thread m_thread = null; private static ManualResetEvent m_shutdownEvent = new ManualResetEvent(false); TransportController transportController = null; ServiceHost selfHost; protected void InitializeComponent() { // Initialize the operating properties for the service. this.CanPauseAndContinue = false; this.CanShutdown = true; this.CanHandleSessionChangeEvent = true; this.ServiceName = "DaoMqPump2"; } // Start the service. protected override void OnStart(string[] args) { try { transportController = TransportController.getInstance(); } catch (Exception e) { EventLog.WriteEntry("DaoMqPump2", "Error starting DaoMqPump2: " + e.Message, EventLogEntryType.Error); throw e; } // create our threadstart object to wrap our delegate method ThreadStart ts = new ThreadStart(this.ServiceWorkerMethod); // create the manual reset event and // set it to an initial state of unsignaled m_shutdownEvent = new ManualResetEvent(false); // create the worker thread m_thread = new Thread(ts); // go ahead and start the worker thread m_thread.Start(); startRemoteControl(); } // Stop this service. protected override void OnStop() { // New in .NET Framework version 2.0. this.RequestAdditionalTime(10000); // signal the event to shutdown m_shutdownEvent.Set(); // wait for the thread to stop giving it 10 seconds m_thread.Join(10000); // call the base class base.OnStop(); stopRemoteControl(); this.ExitCode = 0; } void startRemoteControl() { // Step 1 Create a URI to serve as the base address. Uri baseAddress = new Uri("http://localhost:8000/RemoteControl/"); // Step 2 Create a ServiceHost instance selfHost = new ServiceHost(typeof(RemoteControl), baseAddress); try { /*BasicHttpBinding WSHttpBinding binding = new WSHttpBinding(); binding.Security.Mode = SecurityMode.None;//no security*/ // Step 3 Add a service endpoint. selfHost.AddServiceEndpoint(typeof(IRemoteControl), new BasicHttpBinding(), "RemoteControl"); // Step 4 Enable metadata exchange. ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; selfHost.Description.Behaviors.Add(smb); //selfHost.Authorization.ServiceAuthorizationManager = new GrantningAuthorizationManager(); // Step 5 Start the service. selfHost.Open(); Console.WriteLine("The service is ready."); } catch (CommunicationException ce) { Console.WriteLine("An exception occurred: {0}", ce.Message); EventLog.WriteEntry("DaoMqPump2", "startRemoteControl(): " + ce.Message, EventLogEntryType.Warning); selfHost.Abort(); } } void stopRemoteControl() { try { // Close the ServiceHostBase to shutdown the service. selfHost.Close(); } catch (CommunicationException ce) { Console.WriteLine("An exception occurred: {0}", ce.Message); EventLog.WriteEntry("DaoMqPump2", "stopRemoteControl(): " + ce.Message, EventLogEntryType.Warning); } } public void ServiceWorkerMethod() { bool bSignaled = false; int count = 20; //count starts high so we get to pump during the first iteration try { do { // Block if the service is paused or is shutting down. bSignaled = m_shutdownEvent.WaitOne(m_delay, true); // if we were signaled to shutdow, exit the loop if (bSignaled == true) break; count ++; int elapsed = INTERVAL * count; if (elapsed >= 30) // only run every 30th second { transportController.transportAllMessages(); count = 0;//reset counter } } while (true); } catch (ThreadAbortException) { // Another thread has signalled that this worker // thread must terminate. Typically, this occurs when // the main service thread receives a service stop // command. // Write a trace line indicating that the worker thread // is exiting. Notice that this simple thread does // not have any local objects or data to clean up. } } } public class GrantningAuthorizationManager : ServiceAuthorizationManager { protected override bool CheckAccessCore(OperationContext operationContext) { return true; } } }