private void LoadPlugIns() { ServiceLogger.LogInfo("LoadPlugIns() - Start"); if (!Directory.Exists(_PluginFolder)) { ServiceLogger.LogInfo("Directory not exist, creating directory, directory name: " + _PluginFolder); Directory.CreateDirectory(_PluginFolder); } string[] pluginFiles = Directory.GetFiles(_PluginFolder, "*.plug"); foreach (string pluginFile in pluginFiles) { try { Assembly assembly = Assembly.LoadFrom(pluginFile); System.Type[] assemblyTypes = assembly.GetTypes(); foreach (System.Type type in assemblyTypes) { if (type.GetInterface("IPlugin") != null) { if (type.GetCustomAttributes(typeof(PlugDisplayNameAttribute), false).Length != 1) { throw new PlugNotValidException(type, "PlugDisplayNameAttribute is not supported"); } if (type.GetCustomAttributes(typeof(PlugDescriptionAttribute), false).Length != 1) { throw new PlugNotValidException(type, "PlugDescriptionAttribute is not supported"); } IPlugin plugin = (IPlugin)Activator.CreateInstance(type, new object[] {}); plugin.Host = this; ServiceLogger.LogInfo("Add plugin, plugin name:" + plugin.Name); lstPlugins.Add(plugin); } } } catch (Exception ex) { ServiceLogger.LogError("LoadPlugIns() - Error: " + ex.Message); } } ServiceLogger.LogInfo("LoadPlugIns() - Finish"); }
private void ProcessPluginAction(Func<string, bool> pluginMethod, string fileFullName, bool archiveFiles, bool deleteFiles, string filePrefix, string zipPassword) { ServiceLogger.LogInfo("ProcessPluginAction() - Start"); string strArchivePath = SmartWatcherService.SettingsFailureArchivePath; try { pluginMethod(fileFullName); strArchivePath = SmartWatcherService.SettingsSuccessArchivePath; } catch (Exception ex) { //ToDo: add exception handeling ServiceLogger.LogError("() - Error: " + ex.Message); } finally { if (archiveFiles == true) { string strArchiveFileName = filePrefix + "_" + DateTime.Now.ToString("yyyyMMddHHmmssss") + "_" + Path.GetFileName(fileFullName) + "." + SmartWatcherService.SettingsZipFileExtention; ZipUtil.ZipFile(fileFullName, strArchivePath + strArchiveFileName, zipPassword); } if (deleteFiles == true) { if (File.Exists(fileFullName)) { // Check if file is accessble if (FileProcessor.WaitReady(fileFullName, SmartWatcherService.SettingsReadTries) == true) { try { File.Delete(fileFullName); } catch (Exception ex) { //ToDo: add exception handeling ServiceLogger.LogError("ProcessPluginAction() - Failed to delete file, file name: " + fileFullName + " Error Message: " + ex.Message); } } } } } ServiceLogger.LogInfo("ProcessPluginAction() - Finish"); }
protected override void OnStart(string[] args) { ServiceLogger.LogInfo("SmartWatcher service starting."); try { // Create Failure Archiving Directory if (!Directory.Exists(SettingsFailureArchivePath)) { Directory.CreateDirectory(SettingsFailureArchivePath); } // Create Success Archiving Directory if (!Directory.Exists(SettingsSuccessArchivePath)) { Directory.CreateDirectory(SettingsSuccessArchivePath); } } catch (Exception ex) { ServiceLogger.LogError(ex.Message + "/n" + ex.StackTrace); throw; } }
/// <summary> /// Waits until a file can be opened with write permission /// </summary> private static bool WaitReady(string fileName, int numberOfTries) { // file not ready bool result = false; int intCounter = 0; while (intCounter <= numberOfTries) { try { using (Stream stream = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) { if (stream != null) { Trace.WriteLine(String.Format("Output file {0} ready.", fileName)); // file is ready result = true; break; } } } catch (FileNotFoundException ex) { ServiceLogger.LogError("WaitReady - Error:" + String.Format("Output file {0} not yet ready ({1})", fileName, ex.Message)); } catch (IOException ex) { ServiceLogger.LogError("WaitReady - Error:" + String.Format("Output file {0} not yet ready ({1})", fileName, ex.Message)); } catch (UnauthorizedAccessException ex) { ServiceLogger.LogError("WaitReady - Error:" + String.Format("Output file {0} not yet ready ({1})", fileName, ex.Message)); } Thread.Sleep(500); intCounter += 1; } return result; }