/*-------------------------------------------------------------------------
* 功能描述:UploadService
* 作者:xulisong
* 创建时间: 2019/7/30 10:44:23
* 版本号:v1.0
* -------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using MBIRevitBase.Config;
using MBIRevitBase.Tools;
using Polly;
using Polly.Retry;
using SAGA.DotNetUtils.Logger;
namespace MBIRevitBase.Services
{
///
/// 上传服务类
///
public class UploadService
{
public static BResult UploadExportFile(string result)
{
return DelayRetryPolicy(result);
}
///
/// 上传导出文件
///
///
///
public static T UploadExportFileRetry(string result)where T:BResult
{
var stream = ZipUtils.ZipString(result, "export.json");
var url = ApiConfig.AlgorithmUrl();
return HttpUtils.PostFormDataFileThrowException(url, stream) as T;
}
static BResult DelayRetryPolicy(string result)
{
try
{
#if DEBUG
var waitAndRetryPolicy = Policy.Handle().WaitAndRetry(4,
(i, t) => TimeSpan.FromSeconds(5), ReportError);
#else
var waitAndRetryPolicy = Policy.Handle().WaitAndRetry(4,
(i, t) => TimeSpan.FromSeconds(1*60), ReportError);
#endif
return waitAndRetryPolicy.Execute(()=> UploadExportFileRetry(result));
}
catch (Exception e)
{
Log4Net.Debug($"Execute failed,Message:{e.Message}");
return e.Message;
}
}
static void ReportError(Exception e, TimeSpan timeSpan, int intento, Context context)
{
Log4Net.Debug($"异常{intento:00} <调用秒数:{timeSpan.Seconds} 秒>\t 执行时间:{DateTime.Now}");
}
}
}