private IdentityResult HandleNonCatchedExceptionReturn(object[] paras, BuildErrorFormatStringEventHandler stringHandler2) { //极有可能是DbUpdateConcurrencyException最终无法解决,需要记录一个关键错误, //并且返回不要为空,否则外层使用await则会异常 string temp2 = stringHandler2.Invoke(paras); LogHelper.Fatal(temp2, new Exception(temp2)); LogHelper.Error(temp2); LogHelper.Debug(temp2); //return //Task<IdentityResult>.Run( // () => // {//保证返回非空的结果对象 return(IdentityResult.Failed(temp2)); //} //); }
internal IdentityResult HandleAndAction(IPrivilegeAsyncImpl asyncImpl, object[] paras) { CoreActionEventHandler coreAction = asyncImpl.CoreAction; BuildDbUpdateConcurrencyExceptionFormatStringEventHandler stringHandler1 = asyncImpl.DbUpdateConcurrencyExceptionFormatStringBuilder; BuildErrorFormatStringEventHandler stringHandler2 = asyncImpl.ErrorFormatStringBuilder; IdentityResult taskResult = null; int counter = 5; //通过配置,设定一个尝试的初始值,暂时写死5 while (taskResult == null && counter > 0) { try { IdentityResult identResult = coreAction.Invoke(paras); //Task<IdentityResult> tresult = coreAction.Invoke(paras); //tresult.Wait(); var result = identResult;// tresult.Result; if (result != null && result.Succeeded) { taskResult = IdentityResult.Success; //Task<IdentityResult>.Run( //() => //{//保证返回非空的Task结果对象 // return IdentityResult.Success; //} //); ////如果到这里都没有报错,说明已经添加成功无异常,可以直接返回 break; } } catch (DbUpdateConcurrencyException db) { HandleDbUpdateConcurrencyExceptionReload(paras, stringHandler1, db); } catch (AggregateException taskException) { LogHelper.Error("ApplicationUserManager.HandleAndAction Exception: ", taskException); if (taskException.InnerException != null) { LogHelper.Error("ApplicationUserManager.HandleAndAction InnerException: ", taskException.InnerException); if (taskException.InnerException is DbUpdateConcurrencyException) { HandleDbUpdateConcurrencyExceptionReload(paras, stringHandler1, taskException.InnerException as DbUpdateConcurrencyException); } } } //在这里出来就是要么没有异常,要么异常已经被处理,无论如何先减Counter避免死循环 counter--;//如果Counter未为0,则继续尝试直到不爆这个异常 } if (taskResult == null) { return(HandleNonCatchedExceptionReturn(paras, stringHandler2)); } return(taskResult); }