UploadService.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. namespace MBIRevitBase.Services
  17. {
  18. /// <summary>
  19. /// 上传服务类
  20. /// </summary>
  21. public class UploadService
  22. {
  23. public static BResult UploadExportFile(string result)
  24. {
  25. return DelayRetryPolicy(result);
  26. }
  27. /// <summary>
  28. /// 上传导出文件
  29. /// </summary>
  30. /// <param name="result"></param>
  31. /// <returns></returns>
  32. public static T UploadExportFileRetry<T>(string result)where T:BResult
  33. {
  34. var stream = ZipUtils.ZipString(result, "export.json");
  35. var url = ApiConfig.AlgorithmUrl() + @"/upload-json-zip/upload";
  36. Console.WriteLine("BeginUploadData");
  37. return HttpUtils.PostFormDataFileThrowException(url, stream) as T;
  38. }
  39. static BResult DelayRetryPolicy(string result)
  40. {
  41. try
  42. {
  43. var waitAndRetryPolicy = Policy.Handle<Exception>().WaitAndRetry(4,
  44. (i, t) => TimeSpan.FromSeconds(5*60), ReportError);
  45. return waitAndRetryPolicy.Execute<BResult>(()=> UploadExportFileRetry<BResult>(result));
  46. }
  47. catch (Exception e)
  48. {
  49. Console.WriteLine($"Execute failed,Message:{e.Message}");
  50. return e.Message;
  51. }
  52. }
  53. static void ReportError(Exception e, TimeSpan timeSpan, int intento, Context context)
  54. {
  55. Console.WriteLine($"异常{intento:00} <调用秒数:{timeSpan.Seconds} 秒>\t 执行时间:{DateTime.Now}");
  56. }
  57. }
  58. }