UploadService.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:UploadService
  3. * 作者:xulisong
  4. * 创建时间: 2019/7/30 10:44:23
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Security.Cryptography.X509Certificates;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using MBIRevitBase.Config;
  14. using MBIRevitBase.Tools;
  15. using Polly;
  16. using Polly.Retry;
  17. namespace MBIRevitBase.Services
  18. {
  19. /// <summary>
  20. /// 上传服务类
  21. /// </summary>
  22. public class UploadService
  23. {
  24. public static BResult UploadExportFile(string result)
  25. {
  26. return DelayRetryPolicy(result);
  27. }
  28. /// <summary>
  29. /// 上传导出文件
  30. /// </summary>
  31. /// <param name="result"></param>
  32. /// <returns></returns>
  33. public static T UploadExportFileRetry<T>(string result)where T:BResult
  34. {
  35. var stream = ZipUtils.ZipString(result, "export.json");
  36. var url = ApiConfig.AlgorithmUrl();
  37. Console.WriteLine("BeginUploadData");
  38. return HttpUtils.PostFormDataFileThrowException(url, stream) as T;
  39. }
  40. static BResult DelayRetryPolicy(string result)
  41. {
  42. try
  43. {
  44. #if DEBUG
  45. var waitAndRetryPolicy = Policy.Handle<Exception>().WaitAndRetry(4,
  46. (i, t) => TimeSpan.FromSeconds(5), ReportError);
  47. #else
  48. var waitAndRetryPolicy = Policy.Handle<Exception>().WaitAndRetry(4,
  49. (i, t) => TimeSpan.FromSeconds(5*60), ReportError);
  50. #endif
  51. return waitAndRetryPolicy.Execute<BResult>(()=> UploadExportFileRetry<BResult>(result));
  52. }
  53. catch (Exception e)
  54. {
  55. Console.WriteLine($"Execute failed,Message:{e.Message}");
  56. return e.Message;
  57. }
  58. }
  59. static void ReportError(Exception e, TimeSpan timeSpan, int intento, Context context)
  60. {
  61. Console.WriteLine($"异常{intento:00} <调用秒数:{timeSpan.Seconds} 秒>\t 执行时间:{DateTime.Now}");
  62. }
  63. }
  64. }