UploadService.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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(new[]
  44. {
  45. TimeSpan.FromSeconds(5),
  46. TimeSpan.FromSeconds(5),
  47. TimeSpan.FromSeconds(5),
  48. TimeSpan.FromSeconds(5)
  49. }, ReportError);
  50. return waitAndRetryPolicy.Execute<BResult>(()=> UploadExportFileRetry<BResult>(result));
  51. }
  52. catch (Exception e)
  53. {
  54. Console.WriteLine($"Execute failed,Message:{e.Message}");
  55. return e.Message;
  56. }
  57. }
  58. static void ReportError(Exception e, TimeSpan timeSpan, int intento, Context context)
  59. {
  60. Console.WriteLine($"异常{intento:00} <调用秒数:{timeSpan.Seconds} 秒>\t 执行时间:{DateTime.Now}");
  61. }
  62. }
  63. }