LJServerBase.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using LJLib.Net.SPI.Com;
  2. using Microsoft.AspNetCore.Mvc;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. namespace LJProxy.LJLib.Net.SPI.Server
  8. {
  9. public abstract class LJServerBase
  10. {
  11. public delegate IActionResult DHandle(ILJRequest request, object state);
  12. protected Dictionary<string, DHandle> handlers = new Dictionary<string, DHandle>(StringComparer.OrdinalIgnoreCase);
  13. protected Dictionary<string, Type> requestTypes = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
  14. public IActionResult DoExcute(ILJRequest request, object state)
  15. {
  16. if (!handlers.ContainsKey(request.GetApiName()))
  17. {
  18. throw new Exception("接口未定义:" + request.GetApiName().ToString());
  19. }
  20. var handler = handlers[request.GetApiName()];
  21. if (handler == null)
  22. {
  23. throw new Exception("接口未定义:" + request.GetApiName().ToString());
  24. }
  25. return handler(request, state);
  26. }
  27. public Type GetRequestType(string apiName)
  28. {
  29. if (requestTypes.ContainsKey(apiName))
  30. {
  31. return requestTypes[apiName];
  32. }
  33. else
  34. {
  35. return null;
  36. }
  37. }
  38. public void AddMap(string apiName,Type requestType,DHandle handler)
  39. {
  40. requestTypes[apiName] = requestType;
  41. handlers[apiName] = handler;
  42. }
  43. }
  44. }