Monday 15 August 2011

c# - Disadvantages of a synchronous web service call inside an asynchronous method -



c# - Disadvantages of a synchronous web service call inside an asynchronous method -

i'm working on project needs create xml info on format, send third-party service, , process result. third-party's function here validate both info , format of xml, , create codified string (a "stamp") certifying complies requeriments. stamp has added original xml , stored on database.

now, third-party still chosen, have create project in way can work (so far) 2 different web services before release, chance of adding 1 and, after release, changing chosen one. although final result same (the "stamping" of xml) each web service things differently e.g. 1 returns stamp string, while other returns xml stamp included, , pending 1 returns byte array of zip file containing xml (i don't know told them idea, meh, that's story)

with in mind, decided create static class wraps web service calls (one method per web service). these methods receive xml , homecoming either "stamped" xml or xml error code , message in case went wrong. class handles info has create required xml, phone call 1 of these methods, , process result.

so far looks this:

public class edocument { //this class handles info included in xml public void stamp() { //here xml string created, sent correspondig third-party web service, , processed string xmlstring; //code create xml //... //if needed, have alter line phone call corresponding method webservicecalls.mainwebservicecall stamper = webservicecalls.firstservicecall; stamper.begininvoke(xmlstring, stampcallback, null); } public void stampcallback(iasyncresult ar) { asyncresult result = (asyncresult)ar; webservicecalls.mainwebservicecall caller = (webservicecalls.mainwebservicecall)result.asyncdelegate; string response = caller.endinvoke(ar); //call async method save database stamp, create xml file, e-mail , store it, , notify results... //or create exception error details , raise event here notify error } }

web service calls...

public static class webservicecalls { //here i'll set necessary web service calls. in end there one, //but if on future web service changes, method same signature created here //replacing previous 1 public delegate string mainwebservicecall(string xmldata); public static string firstservicecall(string xmldata) { firstwebservice firstws = new firstwebservice(); string serviceresult = firstws.stamp(xmldata); //this returns stamp string //check result, add together stamp original xml or create error xml, , return... homecoming serviceresult; } public static string secondservicecall(string xmldata) { secondwebservice secondws = new secondwebservice(); string serviceresult = secondws.stamp(xmldata); //this returns xml stamp added //check result, create error xml if went wrong, , return... homecoming serviceresult; } public static string thirdservicecall(string xmldata) { thirdwebservice thirdws = new thirdwebservice(); string serviceresultstring; byte[] serviceresult = thirdws.stamp(xmldata); //this (sigh) returns byte array of zip file... //unzip file, check result, create corresponding xml , return... homecoming serviceresultstring; } }

but nail me... althought i'll calling wrapper methods asynchronously, web service method still called synchronously.

question is: disadvantages of this? should instead phone call web service asynchronously on every caller method, handle callback, raise notifying event, grab on edocument class, process result , raise it's corresponding event? wouldn't making things over-complicated considering possible future changes project have?

or overall wrong approach of problem?

keep in mind c# 4.0, (sadly) async-await out of scope.

you not using async io @ all. delegate.begininvoke uses thread-pool. in server apps purely damaging in situations. async io threadless.

either utilize synchronous code, or async io.

you seem think calling webservice asynchronously internally synchronous (or other way around) might have disadvantage. not case. neither of 2 parties can tell how other 1 implemented. going async in tier affects tier.

c# .net asynchronous

No comments:

Post a Comment