asp.net mvc - Handling concurrency exceptions with external API calls -
i have following post edit action method, perform 2 update actions:-
- edit object on external system suing api calls.
edit object on our system database.
[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) {
as shown in above code not repository.save() update object on our system, unless api return “success”. facing following problem:- if api return “success” concurrency exception occurred, api update object on external system, object not updated on our system? there way handle situation?
there's no easy way solve situation. 1 way handle ask designers of external api expose method allowing commit transaction done in previous call. first call make modifications external system boolean flag indicating changes still pending. update system , in case of success call external api flag data pending valid.
if have no control on external api , makes changes data first call irreversible, afraid not have choices left. might remember state of object modifying on external system before calling api , in case of exception on system, revert previous state calling api previous values.
Comments
Post a Comment