/[projects]/dao/DaoMqPump2/DaoMqPump2/PumpService.cs
ViewVC logotype

Annotation of /dao/DaoMqPump2/DaoMqPump2/PumpService.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1988 - (hide annotations) (download)
Wed Jul 3 09:46:23 2013 UTC (10 years, 11 months ago) by torben
File size: 6236 byte(s)
Force visual studio to treat Service* as code only files
1 torben 1986 using System;
2     using System.Threading;
3    
4     using System.Diagnostics;
5    
6     using System.ServiceModel;
7     using System.ServiceModel.Description;
8    
9    
10     namespace DaoMqPump2
11     {
12    
13 torben 1988 [System.ComponentModel.DesignerCategory("Code")]
14 torben 1986 class PumpService : System.ServiceProcess.ServiceBase
15     {
16     const int INTERVAL = 5;
17     TimeSpan m_delay = new TimeSpan(0, 0, 0, INTERVAL, 0);
18    
19     private Thread m_thread = null;
20     private static ManualResetEvent m_shutdownEvent = new ManualResetEvent(false);
21    
22    
23    
24    
25    
26     TransportController transportController = null;
27    
28    
29     ServiceHost selfHost;
30    
31    
32     protected void InitializeComponent()
33     {
34    
35     // Initialize the operating properties for the service.
36     this.CanPauseAndContinue = false;
37     this.CanShutdown = true;
38     this.CanHandleSessionChangeEvent = true;
39     this.ServiceName = "DaoMqPump2";
40     }
41    
42     // Start the service.
43     protected override void OnStart(string[] args)
44     {
45     try
46     {
47     transportController = TransportController.getInstance();
48     }
49     catch (Exception e)
50     {
51     EventLog.WriteEntry("DaoMqPump2", "Error starting DaoMqPump2: " + e.Message, EventLogEntryType.Error);
52     throw e;
53     }
54    
55    
56     // create our threadstart object to wrap our delegate method
57     ThreadStart ts = new ThreadStart(this.ServiceWorkerMethod);
58    
59     // create the manual reset event and
60     // set it to an initial state of unsignaled
61     m_shutdownEvent = new ManualResetEvent(false);
62    
63     // create the worker thread
64     m_thread = new Thread(ts);
65    
66     // go ahead and start the worker thread
67     m_thread.Start();
68    
69    
70     startRemoteControl();
71     }
72    
73     // Stop this service.
74     protected override void OnStop()
75     {
76     // New in .NET Framework version 2.0.
77     this.RequestAdditionalTime(10000);
78    
79     // signal the event to shutdown
80     m_shutdownEvent.Set();
81    
82     // wait for the thread to stop giving it 10 seconds
83     m_thread.Join(10000);
84    
85     // call the base class
86     base.OnStop();
87    
88     stopRemoteControl();
89    
90     this.ExitCode = 0;
91     }
92    
93    
94     void startRemoteControl()
95     {
96     // Step 1 Create a URI to serve as the base address.
97     Uri baseAddress = new Uri("http://localhost:8000/RemoteControl/");
98    
99     // Step 2 Create a ServiceHost instance
100     selfHost = new ServiceHost(typeof(RemoteControl), baseAddress);
101    
102     try
103     {
104     /*BasicHttpBinding
105     WSHttpBinding binding = new WSHttpBinding();
106     binding.Security.Mode = SecurityMode.None;//no security*/
107    
108    
109     // Step 3 Add a service endpoint.
110     selfHost.AddServiceEndpoint(typeof(IRemoteControl), new BasicHttpBinding(), "RemoteControl");
111    
112     // Step 4 Enable metadata exchange.
113     ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
114     smb.HttpGetEnabled = true;
115     selfHost.Description.Behaviors.Add(smb);
116     //selfHost.Authorization.ServiceAuthorizationManager = new GrantningAuthorizationManager();
117    
118     // Step 5 Start the service.
119     selfHost.Open();
120     Console.WriteLine("The service is ready.");
121    
122    
123     }
124     catch (CommunicationException ce)
125     {
126     Console.WriteLine("An exception occurred: {0}", ce.Message);
127     EventLog.WriteEntry("DaoMqPump2", "startRemoteControl(): " + ce.Message, EventLogEntryType.Warning);
128     selfHost.Abort();
129     }
130     }
131    
132     void stopRemoteControl()
133     {
134     try
135     {
136     // Close the ServiceHostBase to shutdown the service.
137     selfHost.Close();
138     }
139     catch (CommunicationException ce)
140     {
141     Console.WriteLine("An exception occurred: {0}", ce.Message);
142     EventLog.WriteEntry("DaoMqPump2", "stopRemoteControl(): " + ce.Message, EventLogEntryType.Warning);
143     }
144    
145     }
146    
147     public void ServiceWorkerMethod()
148     {
149     bool bSignaled = false;
150     int count = 20; //count starts high so we get to pump during the first iteration
151     try
152     {
153     do
154     {
155     // Block if the service is paused or is shutting down.
156     bSignaled = m_shutdownEvent.WaitOne(m_delay, true);
157    
158     // if we were signaled to shutdow, exit the loop
159     if (bSignaled == true)
160     break;
161    
162     count ++;
163    
164     int elapsed = INTERVAL * count;
165     if (elapsed >= 30) // only run every 30th second
166     {
167     transportController.transportAllMessages();
168     count = 0;//reset counter
169     }
170    
171     }
172     while (true);
173     }
174     catch (ThreadAbortException)
175     {
176     // Another thread has signalled that this worker
177     // thread must terminate. Typically, this occurs when
178     // the main service thread receives a service stop
179     // command.
180    
181     // Write a trace line indicating that the worker thread
182     // is exiting. Notice that this simple thread does
183     // not have any local objects or data to clean up.
184     }
185     }
186    
187    
188     }
189    
190     public class GrantningAuthorizationManager : ServiceAuthorizationManager
191     {
192     protected override bool CheckAccessCore(OperationContext operationContext)
193     {
194     return true;
195     }
196     }
197     }

  ViewVC Help
Powered by ViewVC 1.1.20