ExcutorBase.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Diagnostics;
  3. using JLHHJSvr.BLL;
  4. using LJLib.Net.SPI.Com;
  5. using Newtonsoft.Json;
  6. namespace LJLib.Net.SPI.Server
  7. {
  8. internal abstract class ExcutorBase
  9. {
  10. public abstract LJResponse Excute(ILJRequest request, object state);
  11. }
  12. internal abstract class ExcutorBase<T1, T2> : ExcutorBase
  13. where T1:ILJRequest<T2>
  14. where T2:LJResponse,new()
  15. {
  16. protected abstract void ExcuteInternal(T1 request, object state, T2 rslt);
  17. protected virtual bool PreHandle(T1 request, T2 rslt) { return true; }
  18. public override LJResponse Excute(ILJRequest request, object state)
  19. {
  20. T2 rslt = new T2();
  21. try
  22. {
  23. T1 req = request as T1;
  24. if (req != null && !"Login".Equals(req.GetApiName())) Trace.Write($"{typeof(T1).Name}请求报文:\r\n{JsonConvert.SerializeObject(req)}", "Request");
  25. if (req == null)
  26. {
  27. rslt.ErrMsg = "request不能转换成类型" + typeof(T1).Name;
  28. return rslt;
  29. }
  30. if (PreHandle(req, rslt))
  31. {
  32. ExcuteInternal(req, state, rslt);
  33. }
  34. }
  35. catch (Exception ex)
  36. {
  37. Trace.Write(ex.ToString());
  38. rslt.ErrMsg = ex.ToString();
  39. }
  40. return rslt;
  41. }
  42. }
  43. }