I'm calling a webservice from a console app and want the length of time required for processing as well as the number of records processed, which is returned in int recs. I've posted on this subject a long time ago, but wanted to post a version returning a value from an asynchronous call.
Sorry you had to wait for it. (wait for it... asynchronous calls...)
private static int myMethod()
{
myWebservice ws = GetWS();
DateTime StartTime = DateTime.Now;
AsyncCallback cb = new AsyncCallback(thisapp.myMethodCallback);
IAsyncResult ar = ws.BeginWSMethod(cb, ws);
while (ar.IsCompleted == false) {} // this keeps things cranking so I can get total processing time
Log(StartTime, recs.ToString() + " Records Processed");
return recs; // recs returned to console Main{} block
}
private static void myMethodCallback(IAsyncResult ar)
{
myWebservice ws = (myWebservice) ar.AsyncState;
recs = ws.EndWSMethod(ar);
}
private static myWebservice GetWS() // shared webservice configuration method
{
myWebservice ws = new myWebservice();
if (oXMLUtils.getvalue("ProxyYN") == "YES") // home-grown XML lookup method for use with app.config file
{
WebProxy proxy = new WebProxy(oXMLUtils.getvalue("ProxyServer"), 1); // proxy location
proxy.BypassProxyOnLocal = true;
GlobalProxySelection.Select = proxy;
ws.Proxy = proxy;
ws.Credentials = new NetworkCredential("mydomain\\myusername", "mypassword"); // not a best practice I suppose
}
return ws;
}