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

Contents of /dao/DaoMqPump2/DaoMqPump2/PumpService.cs

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20