IwbSettingProvider.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Xml;
  6. using Abp.Configuration;
  7. using IwbZero.ToolCommon.LogHelpers;
  8. namespace WeEngine.Configuration
  9. {
  10. public class IwbSettingProvider : SettingProvider
  11. {
  12. private static string FileName => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin/Resources", "Setting.iwbx");
  13. public override IEnumerable<SettingDefinition> GetSettingDefinitions(SettingDefinitionProviderContext context)
  14. {
  15. if (!File.Exists(FileName))
  16. {
  17. throw new Exception("未查询到Setting文件,请检查配置!");
  18. }
  19. var list = new List<SettingDefinition>();
  20. var t = typeof(IwbSettingNames);
  21. var fts = t.GetFields().Select(a=>a.GetRawConstantValue()).ToList();
  22. try
  23. {
  24. var xDoc = new XmlDocument();
  25. xDoc.Load(FileName);
  26. XmlNode xmlNode = xDoc.LastChild;
  27. if (xmlNode != null && xmlNode.HasChildNodes)
  28. {
  29. foreach (XmlNode node in xmlNode.ChildNodes)
  30. {
  31. var name = node.Attributes?["name"] != null ? node.Attributes["name"].Value : "";
  32. var value = node.Attributes?["value"] != null ? node.Attributes["value"].Value : "";
  33. if (fts.Contains(name))
  34. {
  35. var setting = new SettingDefinition(name, value,
  36. scopes: SettingScopes.Application | SettingScopes.Tenant | SettingScopes.User,
  37. isVisibleToClients: true);
  38. if (!list.Contains(setting))
  39. {
  40. list.Add(setting);
  41. }
  42. }
  43. }
  44. }
  45. }
  46. catch (Exception e)
  47. {
  48. typeof(IwbSettingProvider).LogError(e);
  49. throw new Exception($"Setting文件解析失败,请检查配置!({e.Message})");
  50. }
  51. return list;
  52. }
  53. }
  54. }