LocalApiServiceExtensions.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. 
  2. using Sers.Core.Module.Api.ApiDesc;
  3. using Sers.Core.Module.Api.LocalApi;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Reflection;
  7. using Sers.SersLoader;
  8. using Sers.Serslot;
  9. using ApiLoader = Sers.Serslot.ApiLoader;
  10. using LocalApiNode = Sers.Serslot.LocalApiNode;
  11. namespace Vit.Extensions
  12. {
  13. public static partial class LocalApiServiceExtensions
  14. {
  15. /// <summary>
  16. /// 调用Serslot加载器加载api
  17. /// </summary>
  18. /// <param name="data"></param>
  19. /// <param name="assembly"></param>
  20. /// <param name="server"></param>
  21. public static void LoadSerslotApi(this LocalApiService data, Assembly assembly, SerslotServer server)
  22. {
  23. if (null == data)
  24. {
  25. return;
  26. }
  27. #region (x.1) api from host
  28. var config = new ApiLoaderConfig { assembly= assembly };
  29. data.apiNodeMng.AddApiNode(new ApiLoader(server).LoadApi(config));
  30. #endregion
  31. #region (x.2) api from appsettings.json::serslot.extApi
  32. Vit.Core.Util.ConfigurationManager.ConfigurationManager.Instance.GetByPath<List<SsApiDesc>>("serslot.extApi")?.ForEach(apiDesc =>
  33. {
  34. IApiNode apiNode;
  35. var reply = apiDesc.extendConfig?["reply"];
  36. if (reply == null)
  37. {
  38. //(x.x.1)由host处理
  39. apiNode = new LocalApiNode(apiDesc, server);
  40. }
  41. else
  42. {
  43. //(x.x.2)直接返回 指定的数据
  44. var replyBytes = reply.SerializeToBytes();
  45. #region onInvoke
  46. Func<ArraySegment<byte>, byte[]> onInvoke = (ArraySegment<byte> arg_OriData) => {
  47. return replyBytes;
  48. };
  49. #endregion
  50. apiNode = new ApiNode_Original(onInvoke, apiDesc);
  51. }
  52. data.apiNodeMng.AddApiNode(apiNode);
  53. });
  54. #endregion
  55. }
  56. }
  57. }