DataExchangeDispatcher.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using System.Collections.Generic;
  3. using Newtonsoft.Json;
  4. namespace Cbdx
  5. {
  6. internal class DataExchangeDispatcher : IDataExchangeDispatcher
  7. {
  8. private readonly Dictionary<string, Dictionary<string, ServiceActionBind>> _serviceDictionary;
  9. public DataExchangeDispatcher(Dictionary<string, Dictionary<string, ServiceActionBind>> serviceDictionary)
  10. {
  11. _serviceDictionary = serviceDictionary;
  12. }
  13. public string InvokeService(string service, string action, string input)
  14. {
  15. DataExchangeResultBase result;
  16. try
  17. {
  18. if (false == _serviceDictionary.TryGetValue(service, out var controller)) throw new EntryPointNotFoundException($"Service [{service}] not found");
  19. if (false == controller.TryGetValue(action, out var actionBind)) throw new EntryPointNotFoundException($"Action [{action}] on [{service}] not found");
  20. var ret = actionBind.Invoke(input);
  21. result = new DataExchangeDataResult<object> { Success = true, Data = ret };
  22. }
  23. catch (Exception ex)
  24. {
  25. result = new DataExchangeErrorResult(ex.Message, ex.StackTrace);
  26. }
  27. return JsonConvert.SerializeObject(result);
  28. }
  29. }
  30. }