I have a web service in ASP.NET being called by a time-sensitive process. If the web service takes longer than N seconds to run I want to return from the call so the time sensitive operation can continue. Is this possible and if so what is the code for this?
+3
A:
Call your web service asynchronously.
http://ondotnet.com/pub/a/dotnet/2005/08/01/async_webservices.html
Robert Harvey
2010-04-19 19:37:46
I considered this...the reason I'm trying to get around doing this is to avoid compiling the application that references the service.
Achilles
2010-04-19 19:39:58
+1
A:
Have the web service call its long-running part in a separate thread. Then wait, kill it, and bail if it takes too long. Be forewarned this could leave scarce resources hooked (e.g., db connections, etc), but you can fix this too.
(sorry for the quick and dirty code, it's for demo, you should elaborate and add types, webservice specifics, etc.)
dim ResultFromWorker as String
dim DoneWorking as boolean
sub GetWebServiceMagicNumber
DoneWorking = False
dim t as new thread = addressof (GetWebServiceMagicNumber_Worker)
t.start
dim count as integer = 0
while not DoneWorking and Count <100
count +=1
System.Threading.Thread.Sleep(100) ' Important to not kill CPU time
wend
if DoneWorking then
return ResultFromWorker
else
return "TimeOut"
EndIf
end sub
sub GetWebServiceMagicNumber_Worker
try
ResultFromWorker = SearchTheWholeWorldForTheAnswer()
catch ex1 as ThreadAbortException
' ignore - My code caused this on purpose
' calling thread should clean up scarce resources, this is borrowed time
catch ex2 as Exception
ResultFromWorker = "Error - " & ex.tostring
finally
DoneWorking=True
End Try
end sub
FastAl
2010-04-19 20:02:45
Oops! for those looking... I forgot to kill the thread before the statement 'return "TimeOut"'. It would still end itself when the webservice came back but if called again could mess w/ the shared member variables if you use the same object 2x.
FastAl
2010-05-24 19:26:49