业务流.二开案例.WebApi在调用端重试

栏目:云星空知识作者:金蝶来源:金蝶云社区发布:2024-09-16浏览:1

业务流.二开案例.WebApi在调用端重试

【场景】系统反写时,会针对需要的单据启用反写网控;当反写网控没有解除时,调用接口就会出错 ![image.webp](/download/01003273c71d50c04559a67f3da0a6ca5c40.webp) 【案例】在调用端遇到异常时重新调用接口 ```csharp using Kingdee.BOS.WebApi.Client; using Newtonsoft.Json.Linq; using System; namespace TestApi { public class ApiSample { private static K3CloudApiClient ClientInstance = new K3CloudApiClient(ApiKeyConst.Url, 5*60); private static bool isLogin = false; private readonly object lockObj = new object(); public void SaveBillWithRetry(string billNo, int qty, int sBillid, int sEntryId) { string json = GenerateSaveJson(billNo, qty, sBillid, sEntryId); SaveRetry(billNo, json, true); } /// <summary> /// 待重试的保存 /// </summary> /// <param name="json"></param> /// <param name="isAsync"></param> private void SaveRetry(string billNo, string json, bool isAsync) { Action saveAct = () => { int retryCnt = 5000; int curCnt = 0; while (curCnt < retryCnt) { var sResult = this.SaveBill(json); if (sResult) { Console.WriteLine(string.Format("billNo : {0}, Success", billNo)); break; } ++curCnt; System.Threading.Thread.Sleep(TimeSpan.FromMilliseconds(50)); } }; if (isAsync) { System.Threading.Tasks.Task.Factory.StartNew(saveAct, System.Threading.Tasks.TaskCreationOptions.LongRunning); //强制使用线程而非线程池,否则无法模拟并发 //System.Threading.Thread t = new System.Threading.Thread(()=> { saveAct(); }); //t.Start(); } else { saveAct(); } } /// <summary> /// 单次保存的实现 /// </summary> /// <param name="data"></param> /// <returns></returns> private bool SaveBill(string data) { string sResult = string.Empty; try { sResult = ClientInstance.Save(ApiKeyConst.FormId, data); } catch (Exception e) { Console.WriteLine(e); return false; } var responseStatus = JObject.Parse(sResult)["Result"]["ResponseStatus"]; var isSuccess = responseStatus["IsSuccess"].Value<bool>(); if (!isSuccess) { var msgCode = responseStatus["MsgCode"].Value<int>(); if (msgCode == 1)//如何是会话丢失,则重新登录 { isLogin = false; Login(); //重新登录 } else { //其他情况,则记录日志或再次调用啥的 Console.WriteLine(sResult); //Kingdee.BOS.Log.Logger.Error("ApiSample", sResult, null); } } return isSuccess; } /// <summary> /// 生成保存单据的json包 /// </summary> /// <param name="billNo"></param> /// <param name="qty"></param> /// <param name="sBillid"></param> /// <param name="sEntryId"></param> /// <returns></returns> private string GenerateSaveJson(string billNo, int qty, int sBillid, int sEntryId) { string json = @" {{ ""NeedUpDateFields"": [], ""NeedReturnFields"": [], ""IsDeleteEntry"": ""true"", ""SubSystemId"": """", ""IsVerifyBaseDataField"": ""false"", ""IsEntryBatchFill"": ""true"", ""ValidateFlag"": ""true"", ""NumberSearch"": ""true"", ""IsAutoAdjustField"": ""false"", ""InterationFlags"": """", ""IgnoreInterationFlag"": """", ""IsControlPrecision"": ""false"", ""ValidateRepeatJson"": ""false"", ""Model"": {{ ""FBillNo"": ""{0}"", ""F_BHR_Remarks"": """", ""FEntity"": [ {{ ""F_BHR_Integer"": {1}, ""FEntity_Link"" : [ {{ ""FEntity_Link_FRuleId"" :""de8c3949-284c-4d6a-a7e1-8c87b5bd03f5"", ""FEntity_Link_FSTableName"" :""BHR_t_Cust_Entry100228"", ""FEntity_Link_FSid"" :""{2}"", ""FEntity_Link_FSBillId"" :""{3}"" }} ] }} ] }} }} "; string result = string.Format(json, billNo, qty, sBillid, sEntryId); return result; } /// <summary> /// 登录操作 /// </summary> /// <returns></returns> private bool Login() { lock (lockObj) { if (!isLogin) { var loginResult = ClientInstance.ValidateLogin(ApiKeyConst.Dbid, ApiKeyConst.UserName, ApiKeyConst.Password, 2052); var resultType = JObject.Parse(loginResult)["LoginResultType"].Value<int>(); if (resultType == 1) { isLogin = true; return true; } throw new Exception(loginResult); } } return true; } } public static class ApiKeyConst { /// <summary> /// 地址 /// </summary> public static string Url = "http://localhost:1200/k3cloud/"; /// <summary> /// 账套id /// </summary> public static string Dbid = "dbid"; /// <summary> /// 用户名 /// </summary> public static string UserName = "username"; /// <summary> /// 密码 /// </summary> public static string Password = "password"; /// <summary> /// 表单标识 /// </summary> public static string FormId = "k9ac4e55c07054946b08f7acc818f919e"; } } ``` 【效果】 ![20231016 1856.webp](/download/01008d08b0b181614b52965f2e0841a67d5d.webp) 【备注说明】 (1)该方案没有持久化该信息,因此调用方重启之后就丢失信息 (2)根据自己单据的保存计算时间,调整重试次数和间隔,避免短时间内大量重试请求导致的服务端卡慢

业务流.二开案例.WebApi在调用端重试

【场景】系统反写时,会针对需要的单据启用反写网控;当反写网控没有解除时,调用接口就会出错![image.webp](/download/01003273c71d50c045...
点击下载文档
确认删除?
回到顶部
客服QQ
  • 客服QQ点击这里给我发消息