views:

31

answers:

1

Hi All,

I am calling an external web service from a windows service using C#.

The web service is in .NET. The Web method GetRandomNumber() returns a random integer. The GetRandomNumber() is invoked asynchronously The web service client code is as below:

Service1 s1 = new Service1();
s1.Timeout = 2000;
s1.GetRandomNumberCompleted += new GetRandomNumberCompletedEventHandler(s1_GetRandomNumberCompleted);
s1.GetRandomNumberAsync();

I am setting the timeout to 2secs (2000 ms). The web service call, in most of the cases takes 5-10 secs. But in this case the time out does not occur. Even after the timeout period is over, the client gets the data. I am missing some thing OR is there any other way to use time out while invoking the web service Asynchronously?

-N

;

A: 

Here's an excerpt from an Article on MSDN that should help.

http://msdn.microsoft.com/en-us/library/ff647786.aspx#scalenetchapt10_topic14

Asynchronous calls to a Web service. In this case, you should decide on the number of seconds you can wait for the Web service call to return the results. When using a WaitHandle, you can pass the number of milliseconds the executing thread is blocked on the WaitHandle before it aborts the request to the Web service. This is shown in the following code snippet.

MyWebServ obj = new MyWebServ();
IAsyncResult ar = obj.BeginFunCall(5,5,null,null);

// wait for not more than 2 seconds
ar.AsyncWaitHandle.WaitOne(2000,false);
if (!ar.IsCompleted) //if the request is not completed  { 
  WebClientAsyncResult wcar = (WebClientAsyncResult)ar;
  wcar.Abort();//abort the call to web service 
}
else
{ //continue processing the results from web service }
Shawn Steward