UploadService.cs 2.5 KB

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