Guest User

Untitled

a guest
Aug 2nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.35 KB | None | 0 0
  1. Streamreader locks file
  2. //Code that fires the timer
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Diagnostics;
  8. using System.Linq;
  9. using System.ServiceProcess;
  10. using System.Text;
  11. using System.Timers;
  12.  
  13. namespace SmsWindowsService
  14. {
  15. public partial class SmsWindowsService : ServiceBase
  16. {
  17. private static System.Timers.Timer aTimer;
  18.  
  19. public SmsWindowsService()
  20. {
  21. InitializeComponent();
  22.  
  23. if (!System.Diagnostics.EventLog.SourceExists("MatterCentreSMSSource"))
  24. {
  25. System.Diagnostics.EventLog.CreateEventSource(
  26. "MatterCentreSMSSource", "MatterCentreSMSLog");
  27. }
  28. elMatterCentreSMS.Source = "MatterCentreSMSSource";
  29. elMatterCentreSMS.Log = "MatterCentreSMSLog";
  30.  
  31. }
  32.  
  33. protected override void OnStart(string[] args)
  34. {
  35. string logText = string.Empty;
  36.  
  37. logText = "MatterCentreSMS Service started successfully on " + DateTime.Now;
  38.  
  39. WriteEventLog(logText);
  40.  
  41. //Create a timer with a ten second interval.
  42. aTimer = new System.Timers.Timer(10000);
  43.  
  44. //Hook up the Elapsed event for the timer.
  45. aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
  46.  
  47. //Set the Interval to 5 minutes.
  48. //aTimer.Interval = 300000;
  49. aTimer.Interval = 60000;
  50. aTimer.Enabled = true;
  51.  
  52. // If the timer is declared in a long-running method, use
  53. // KeepAlive to prevent garbage collection from occurring
  54. // before the method ends.
  55. //GC.KeepAlive(aTimer);
  56. GC.Collect();
  57. }
  58.  
  59. protected override void OnStop()
  60. {
  61. string logText = string.Empty;
  62.  
  63. logText = "MatterCentreSMS Service stopped on " + DateTime.Now;
  64.  
  65. WriteEventLog(logText);
  66. }
  67.  
  68. private void WriteEventLog(string logText)
  69. {
  70. elMatterCentreSMS.WriteEntry(logText);
  71. }
  72.  
  73. private void OnTimedEvent(object source, ElapsedEventArgs e)
  74. {
  75. string ex = string.Empty;
  76.  
  77. SendSms s = new SendSms();
  78.  
  79. ex = s.ProcessSms();
  80.  
  81. if (ex.Length > 1)
  82. WriteEventLog(ex);
  83.  
  84. //ex = RestartService("SmsWindowsService", 60000);
  85. //WriteEventLog(ex);
  86. }
  87.  
  88. public string RestartService(string serviceName, int timeoutMilliseconds)
  89. {
  90. ServiceController service = new ServiceController(serviceName);
  91.  
  92. try
  93. {
  94. int millisec1 = Environment.TickCount;
  95. TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
  96.  
  97. service.Stop();
  98. service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
  99.  
  100. // count the rest of the timeout
  101. int millisec2 = Environment.TickCount;
  102. timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2 - millisec1));
  103.  
  104. service.Start();
  105. service.WaitForStatus(ServiceControllerStatus.Running, timeout);
  106.  
  107. return "MatterCentreSMS Service successfully restarted on " + DateTime.Now;
  108. }
  109.  
  110. catch (Exception e)
  111. {
  112. return Convert.ToString(e);
  113. }
  114. }
  115. }
  116. }
  117.  
  118. //Code that reads the file
  119. using System;
  120. using System.Collections.Generic;
  121. using System.Linq;
  122. using System.Text;
  123. using System.IO;
  124. using System.Xml;
  125.  
  126. namespace SmsWindowsService
  127. {
  128. class Message
  129. {
  130. private string filePath;
  131.  
  132. public Message(string filePath)
  133. {
  134. this.filePath = filePath;
  135. }
  136.  
  137. public string readSMS(string filePath)
  138. {
  139. const string searchmessage = "[B-->]";
  140. StreamReader smsmessage = new StreamReader(filePath);
  141.  
  142. try
  143. {
  144. FileInfo filenameinfo = new FileInfo(filePath);
  145. if (filenameinfo.Exists == false)
  146. throw new SMSReaderException(String.Format("SMS Message {0} cannot be found ...", filePath), filePath);
  147.  
  148. smsmessage = filenameinfo.OpenText();
  149. string smsoutput = smsmessage.ReadToEnd();
  150. int endpos = smsoutput.IndexOf(searchmessage);
  151. smsoutput = smsoutput.Substring(endpos + searchmessage.Length);
  152. smsoutput = smsoutput.Replace("&", "&");
  153. smsoutput = smsoutput.Replace(""", """);
  154. smsoutput = smsoutput.Replace("'", "'");
  155.  
  156. filenameinfo = null;
  157. smsmessage.Close();
  158. smsmessage.Dispose();
  159.  
  160. return smsoutput;
  161. }
  162.  
  163. catch(Exception e)
  164. {
  165. throw new Exception("Help", e.InnerException);
  166. }
  167.  
  168. finally
  169. {
  170. smsmessage.Close();
  171. smsmessage.Dispose();
  172. }
  173. }
  174. }
  175.  
  176. public class SMSReaderException : System.IO.FileNotFoundException
  177. {
  178. public SMSReaderException(string message, string filename)
  179. : base(message, filename)
  180. {
  181. }
  182. }
  183. }
  184.  
  185. //Code that connects to web service and send sms
  186. using System;
  187. using System.Collections.Generic;
  188. using System.Linq;
  189. using System.Text;
  190. using System.ComponentModel;
  191. using System.Data;
  192. using System.IO;
  193. using System.Net;
  194. using System.Configuration;
  195. using SmsWindowsService.EsendexSendSmsService;
  196.  
  197. namespace SmsWindowsService
  198. {
  199. class SendSms
  200. {
  201. string filePath = string.Empty;
  202. string directoryPath = string.Empty;
  203. string directoryPathProcessing = string.Empty;
  204. string directoryPathCompleted = string.Empty;
  205. string smsLogfileDirectory = string.Empty;
  206. string smsLogfilePath = string.Empty;
  207. string mattercentreSMS = string.Empty;
  208. string messageBody = string.Empty;
  209. string messageId = string.Empty;
  210. string messageStatus = string.Empty;
  211. string dateTodayString = string.Empty;
  212. long mobileNumber;
  213. EsendexSendSmsService.SendService send;
  214.  
  215. public SendSms()
  216. {
  217. directoryPath = ConfigurationSettings.AppSettings[@"directoryPath"];
  218. directoryPathProcessing = ConfigurationSettings.AppSettings[@"directoryPathProcessing"];
  219. directoryPathCompleted = ConfigurationSettings.AppSettings[@"directoryPathCompleted"];
  220. smsLogfileDirectory = ConfigurationSettings.AppSettings[@"smsLogfileDirectory"];
  221. dateTodayString = DateTime.Now.ToString("yyyy/MM/dd");
  222. smsLogfilePath = smsLogfileDirectory + dateTodayString.Replace(@"/", "_") + ".txt";
  223. send = new EsendexSendSmsService.SendService();
  224. }
  225.  
  226. public string ProcessSms()
  227. {
  228. string ex = string.Empty;
  229.  
  230. try
  231. {
  232. DirectoryInfo di = new DirectoryInfo(directoryPathProcessing);
  233.  
  234. ex = MoveFilesToCompleted(directoryPathProcessing, directoryPathCompleted);
  235.  
  236. if (ex.Length > 1)
  237. return ex;
  238.  
  239. ex = MoveFilesToProcessing(directoryPath, directoryPathProcessing);
  240.  
  241. if (ex.Length > 1)
  242. return ex;
  243.  
  244. FileInfo[] subFilesProcessing = di.GetFiles();
  245.  
  246. foreach (FileInfo subFile in subFilesProcessing)
  247. {
  248. filePath = directoryPathProcessing + subFile.Name;
  249.  
  250. Message sms = new Message(filePath);
  251.  
  252. mattercentreSMS = sms.readSMS(filePath);
  253.  
  254. MessageDetails d = new MessageDetails(mattercentreSMS);
  255.  
  256. mobileNumber = d.GetMobileNumber();
  257. messageBody = d.GetMessageBody();
  258.  
  259. ex = SetHeader();
  260.  
  261. if (ex.Length > 1)
  262. return ex;
  263.  
  264. ex = SetProxy();
  265.  
  266. if (ex.Length > 1)
  267. return ex;
  268.  
  269. //Send the message and get the returned messageID and send status
  270. messageId = send.SendMessage(Convert.ToString(mobileNumber), messageBody, EsendexSendSmsService.MessageType.Text);
  271. messageStatus = Convert.ToString(send.GetMessageStatus(messageId));
  272.  
  273. ex = WriteLogFile(messageId, subFile.Name, messageStatus);
  274.  
  275. if (ex.Length > 1)
  276. return ex;
  277.  
  278. send.Dispose();
  279. }
  280.  
  281. di = null;
  282. subFilesProcessing = null;
  283.  
  284. return ex;
  285. }
  286.  
  287. catch (Exception e)
  288. {
  289. return Convert.ToString(e);
  290. }
  291. }
  292.  
  293. private string MoveFilesToCompleted(string directoryPathProcessing, string directoryPathCompleted)
  294. {
  295. DirectoryInfo din = new DirectoryInfo(directoryPathProcessing);
  296.  
  297. try
  298. {
  299. FileInfo[] subFiles = din.GetFiles();
  300.  
  301. foreach (FileInfo subFile in subFiles)
  302. {
  303. subFile.MoveTo(directoryPathCompleted + subFile.Name);
  304. }
  305.  
  306. subFiles = null;
  307. return "";
  308. }
  309.  
  310. catch (Exception e)
  311. {
  312. return Convert.ToString(e);
  313. }
  314.  
  315. finally
  316. {
  317. din = null;
  318. }
  319. }
  320.  
  321. private string MoveFilesToProcessing(string directoryPath, string directoryPathProcessing)
  322. {
  323. DirectoryInfo din = new DirectoryInfo(directoryPath);
  324.  
  325. try
  326. {
  327. FileInfo[] subFiles = din.GetFiles();
  328.  
  329. foreach (FileInfo subFile in subFiles)
  330. {
  331. subFile.MoveTo(directoryPathProcessing + subFile.Name);
  332. }
  333.  
  334. subFiles = null;
  335. return "";
  336. }
  337.  
  338. catch (Exception e)
  339. {
  340. return Convert.ToString(e);
  341. }
  342.  
  343. finally
  344. {
  345. din = null;
  346. }
  347. }
  348.  
  349. private string SetHeader()
  350. {
  351. try
  352. {
  353. //Setup account details in the header
  354. EsendexSendSmsService.MessengerHeader header = new EsendexSendSmsService.MessengerHeader();
  355. header.Account = ConfigurationSettings.AppSettings[@"smsServiceUrl"];
  356. header.Username = ConfigurationSettings.AppSettings[@"smsServiceUsername"];
  357. header.Password = ConfigurationSettings.AppSettings[@"smsServicePassword"];
  358.  
  359. // set the SOAP header Authentication values
  360. send.MessengerHeaderValue = header;
  361.  
  362. return "";
  363. }
  364.  
  365. catch (Exception e)
  366. {
  367. return Convert.ToString(e);
  368. }
  369. }
  370.  
  371. private string SetProxy()
  372. {
  373. try
  374. {
  375. //Create a web proxy object as the proxy server block direct request to esendex
  376. WebProxy myProxy = new WebProxy(ConfigurationSettings.AppSettings[@"proxyaddress"], true);
  377. myProxy.Credentials = new NetworkCredential(ConfigurationSettings.AppSettings[@"username"], ConfigurationSettings.AppSettings[@"password"]);
  378. WebRequest.DefaultWebProxy = myProxy;
  379. send.Proxy = myProxy;
  380.  
  381. return "";
  382. }
  383.  
  384. catch (Exception e)
  385. {
  386. return Convert.ToString(e);
  387. }
  388. }
  389.  
  390. private string WriteLogFile(string messageId, string smsFileName, string messageStatus)
  391. {
  392. try
  393. {
  394. if (File.Exists(smsLogfilePath))
  395. {
  396. //file is not empty - append log entry to file
  397. using (StreamWriter writeSmsLog = File.AppendText(smsLogfilePath))
  398. {
  399. writeSmsLog.WriteLine(messageId + " " + smsFileName + " " + DateTime.Now + " " + messageStatus);
  400. writeSmsLog.Close();
  401. }
  402. }
  403. else
  404. {
  405. FileStream fs = File.OpenWrite(smsLogfilePath);
  406. fs.Flush();
  407. fs.Close();
  408. fs.Dispose();
  409.  
  410. using (StreamWriter writeSmsLog = new StreamWriter(smsLogfilePath, true))
  411. {
  412. writeSmsLog.WriteLine("Message_ID File_Name Date_Sent Status");
  413. writeSmsLog.WriteLine("======================================================================================================================================");
  414. writeSmsLog.WriteLine(messageId + " " + smsFileName + " " + DateTime.Now + " " + messageStatus);
  415. writeSmsLog.Close();
  416. }
  417. }
  418.  
  419. return "";
  420. }
  421.  
  422. catch (Exception e)
  423. {
  424. return Convert.ToString(e);
  425. }
  426. }
  427. }
  428. }
  429.  
  430. StreamReader smsmessage = new StreamReader(filePath);
  431.  
  432. try
  433. {
  434. FileInfo filenameinfo = new FileInfo(filePath);
  435. ....
  436. smsmessage = filenameinfo.OpenText();
  437. ...
Add Comment
Please, Sign In to add comment