UploadService.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. return DelayRetryPolicy(result);
  28. }
  29. /// <summary>
  30. /// 上传导出文件
  31. /// </summary>
  32. /// <param name="result"></param>
  33. /// <returns></returns>
  34. public static T UploadExportFileRetry<T>(string result)where T:BResult
  35. {
  36. var stream = ZipUtils.ZipString(result, "export.json");
  37. var url = ApiConfig.AlgorithmUrl();
  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(1*60), ReportError);
  50. #endif
  51. return waitAndRetryPolicy.Execute<BResult>(()=> UploadExportFileRetry<BResult>(result));
  52. }
  53. catch (Exception e)
  54. {
  55. Log4Net.Debug($"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. Log4Net.Debug($"异常{intento:00} <调用秒数:{timeSpan.Seconds} 秒>\t 执行时间:{DateTime.Now}");
  62. }
  63. }
  64. }