我有以下 POST 编辑操作方法,主要执行两个更新操作:-
- 在调用 API 调用的外部系统上编辑对象。
编辑我们系统数据库上的对象。
[HttpPost] public ActionResult Create(RackJoin rj, FormCollection formValues) {string controllername = RouteData.Values["controller"].ToString(); if (ModelState.IsValid) { var message = ""; var status = ""; long assetid = new long(); XmlDocument doc = new XmlDocument(); using (var client = new WebClient()) { var query = HttpUtility.ParseQueryString(string.Empty); foreach (string key in formValues) { query[key] = this.Request.Form[key]; } query["username"] = System.Web.Configuration.WebConfigurationManager.AppSettings["ApiUserName"]; query["password"] = System.Web.Configuration.WebConfigurationManager.AppSettings["ApiPassword"]; string apiurl = System.Web.Configuration.WebConfigurationManager.AppSettings["ApiURL"]; var url = new UriBuilder(apiurl); url.Query = query.ToString(); try { string xml = client.DownloadString(url.ToString()); doc.LoadXml(xml); status = doc.SelectSingleNode("/operation/operationstatus").InnerText; message = doc.SelectSingleNode("/operation/message").InnerText; } catch (WebException ex) { ModelState.AddModelError(string.Empty, "Error occurred:" + ex.InnerException); } } if (status.ToUpper() == "SUCCESS") { repository.InsertOrUpdateRack(rj.Rack, User.Identity.Name, rj.Resource.RESOURCEID); repository.Save(); return RedirectToAction("Index"); } else { ModelState.AddModelError(string.Empty, message.ToString()); } } } catch (DbUpdateConcurrencyException ex) {
如上面的代码所示,我不会执行 repository.save() 来更新我们系统上的对象,除非 API 返回“成功”。但目前我面临以下问题:-如果API返回“成功”但发生并发异常,那么API将更新外部系统上的对象,但不会更新我们系统上的对象?那么有没有办法处理这种情况呢?