UploadService.cs 2.2 KB

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