UploadService.cs 2.3 KB

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