1#

1.新建一个控制台程序

2.使用nuget引用最新的Topshelf.dll,也可以直接去网站下载并引用。

3.首先编辑控制台程序入口program.cs的调用代码

  1. static void Main(string[] args)
  2.         {
  3.             try
  4.             {
  5.                 Main2(args);
  6.             }
  7.             catch (Exception ex)
  8.             {
  9.                 
  10.                 Console.WriteLine(ex.Message);
  11.             }
  12.         }


  13.         static void Main2(string[] args)
  14.         {
  15.             
  16.             var host = HostFactory.New(x =>
  17.             {
  18.                 x.Service<Main>(s =>
  19.                 {

  20.                     s.ConstructUsing(name => new Main());
  21.                     s.WhenStarted(tc => tc.Start());
  22.                     s.WhenStopped(tc => tc.Stop());
  23.                 });

  24.                 x.RunAsLocalSystem();

  25.                 var desc = ConfigurationManager.AppSettings["WSServiceDesc"];
  26.                 var serviceName = ConfigurationManager.AppSettings["WSServiceName"];

  27.                 x.SetDescription(desc);
  28.                 x.SetDisplayName(serviceName);
  29.                 x.SetServiceName(serviceName);
  30.             });

  31.             host.Run();
  32.         }
复制代码

当服务启动时,我们会调用Main.cs中的启动代码以及方法。

4.程序开始时会执行Main.cs中的Start方法,start方法中的代码逻辑应当是传入所在XML文件夹的地址(docDict即为文件路径)

  1. var xmlRefreshCacheService = new XmlRefreshCacheOperateService();
  2.             var docDict = ConfigurationManager.AppSettings["XmlDocAddress"];

  3.             if (docDict != null) xmlRefreshCacheService.OperateStart(Path.Combine(docDict));
复制代码

5.在operateStart方法里面定义当xml文件发生改变时的事件逻辑,下方逻辑便是要将文件路径中的所有的xml拿出进行事件的监听,

一旦发生改动自动执行updateCouch方法去主动更新缓存(Log类下载:http://bbs.cfxixi.com/showtopic-686.aspx

  1. private static FileSystemWatcher fsw = null;

  2.         public void OperateStart(string filePath)
  3.         {
  4.             try
  5.             {
  6.                 fsw = new FileSystemWatcher(filePath);
  7.                 fsw.EnableRaisingEvents = true;
  8.                 fsw.Filter = "*.xml";
  9.                 fsw.Changed += OnChanged;
  10.             }
  11.             catch (Exception err)
  12.             {
  13.                 Log.WriteLog(err.Message);
  14.             }
  15.           


  16.         }

  17.         public static void OnChanged(object source, FileSystemEventArgs e)
  18.         {
  19.             try
  20.             {
  21.                 if (File.Exists(e.FullPath))
  22.                 {
  23.                     UpdateCouch(e.FullPath,e.Name);
  24.                 }
  25.                 else
  26.                 {

  27.                 }
  28.             }
  29.             catch (Exception exp)
  30.             {
  31.                 Log.WriteLog(exp.Message);
  32.             }
  33.         }
复制代码

6.第六步是通过Xml的名字从DLL中自动找到相同名称的class

  1. private static FileSystemWatcher fsw = null;

  2.         public void OperateStart(string filePath)
  3.         {
  4.             try
  5.             {
  6.                 fsw = new FileSystemWatcher(filePath);
  7.                 fsw.EnableRaisingEvents = true;
  8.                 fsw.Filter = "*.xml";
  9.                 fsw.Changed += OnChanged;
  10.             }
  11.             catch (Exception err)
  12.             {
  13.                 Log.WriteLog(err.Message);
  14.             }
  15.           


  16.         }

  17.         public static void OnChanged(object source, FileSystemEventArgs e)
  18.         {
  19.             try
  20.             {
  21.                 if (File.Exists(e.FullPath))
  22.                 {
  23.                     UpdateCouch(e.FullPath,e.Name);
  24.                 }
  25.                 else
  26.                 {

  27.                 }
  28.             }
  29.             catch (Exception exp)
  30.             {
  31.                 Log.WriteLog(exp.Message);
  32.             }
  33.         }
复制代码

6.第六步是通过Xml的名字从DLL中自动找到相同名称的class

  1. //动态加载程序集
  2.             var assemblyLoadFrom = ConfigurationManager.AppSettings["AssemblyLoadFrom"];
  3.             Assembly myAssembly = Assembly.LoadFrom(assemblyLoadFrom);


  4.             //获得程序集里的所有类
  5.             var classNameSpace = ConfigurationManager.AppSettings["ClassNameSpace"];
  6.             Type myType = myAssembly.GetType(classNameSpace + Path.GetFileNameWithoutExtension(filePath));
复制代码

7.第七步拿到对象类型并且读取Xml文件(将XML里面的内容反序列化成对象)

  1.                 var storeTime = Convert.ToInt32(ConfigurationManager.AppSettings["StoreCouchTime"]);
  2.                 
  3.                 if (rootObj != null)
  4.                 {

  5.                     foreach (Appsettings ass in rootObj.App)
  6.                     {

  7.                         string key = StringConst.Prefix + "AppSetting." + ass.Name;
  8.                         object value;
  9.                         if (CacheInstance.Couch.TryGet(key, out value))
  10.                         {
  11.                             CacheInstance.Couch.Remove(key);
  12.                         }

  13.                         CacheInstance.Couch.Add(key, ass.Value, new TimeSpan(0, 0, 0, storeTime, 0));
  14.                         try
  15.                         {
  16.                             Log.WriteLog(string.Format("更新:变量名:{0},OLD值{1},NEW值{2}.", ass.Name, value, ass.Value));
  17.                         }
  18.                         catch
  19.                         {
  20.                         }
  21.                     }
  22.                 }
  23.                 else if (textHelpObj != null)
  24.                 {
  25.                     foreach (HelpTextItem textItem in textHelpObj.TextList)
  26.                     {

  27.                         string key = StringConst.Prefix + "TextHelp." + textItem.Key;
  28.                         object value;
  29.                         if (CacheInstance.Couch.TryGet(key, out value))
  30.                         {
  31.                             CacheInstance.Couch.Remove(key);
  32.                         }

  33.                         CacheInstance.Couch.Add(key, textItem, new TimeSpan(0, 0, 0, storeTime, 0));
  34.                         try
  35.                         {
  36.                             var jsonResult = JsonHelper.ToJson(textItem);
  37.                             Log.WriteLog(string.Format("更新:变量名:{0},OLD值{1},NEW值{2}.", textItem.Key, value, jsonResult));
  38.                         }
  39.                         catch
  40.                         {
  41.                         }
  42.                     }
  43.                 }
  44.                 else
  45.                 {

  46.                     

  47.                     string key = StringConst.Prefix + fileName;
  48.                     object value;
  49.                     if (CacheInstance.Couch.TryGet(key, out value))
  50.                     {
  51.                         CacheInstance.Couch.Remove(key);
  52.                     }

  53.                     CacheInstance.Couch.Add(key, root, new TimeSpan(0, 0, 0, storeTime, 0));
  54.                     try
  55.                     {
  56.                         var jsonResult = JsonHelper.ToJson(root);
  57.                         Log.WriteLog(string.Format("更新:变量名:{0},OLD值{1},NEW值{2}.", fileName, value, jsonResult));
  58.                     }
  59.                     catch
  60.                     {
  61.                     }
  62.                 }
复制代码