I have a webservice that gets a list from client and inserts it to database. Client has a windows service that is sending a list per 10 seconds. But there is a problem. What if it cannot reach to webservice(server). I should not lost any of the data. I decided to save data to a txt or binary if server is not reachable, and then upload them after the server starts to run. However, how can I decide whether the webservice is unavaliable. If I store the data to a file in a catch block, it will store when ever it gets an error, not only webservice unavaliable error. Any advice?
Asked
Active
Viewed 1,500 times
0
-
Don't just catch Exceptions catch concrete Exceptiontypes and do the right thing per Exceptiontype. – Ralf Dec 03 '13 at 09:47
2 Answers
1
You can make an http request on the service's endpoint url and check if everything is ok :
var url = "http://....";
//OR
var url = service_object.Url;
var request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 2000; //timeout 20 seconds
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode != HttpStatusCode.OK)
{
throw new ApplicationException(response.StatusDescription);
}
}
catch (ApplicationException ex)
{
//Do what you want here, create a file for example...
}

OlivierH
- 3,875
- 1
- 19
- 32
-
Yes, and as soon as you've determined that the web service is/is not available, it could flip state. – Damien_The_Unbeliever Dec 03 '13 at 09:48
-
Even with a queue system, you will have to check after a while if service is up or down... I don't get your point here. In every case you have to handle exceptions (ie handle service is down) when you send your data. – OlivierH Dec 03 '13 at 09:50
-
I added the webserrice as a reference to my project. How can can I control its ststus? – osmanraifgunes Dec 03 '13 at 09:55
-
You can check that endpoint url is reachable just like above, or catch exceptions at the moment you are making the call. Have a look at [this thread](http://stackoverflow.com/a/10209555/2806497) or [this article](http://blogs.msdn.com/b/ericlippert/archive/2008/09/10/vexing-exceptions.aspx) to understand how to handle .NET exceptions. – OlivierH Dec 03 '13 at 10:00
0
I'd introduce a queuing system (such as MSMQ or NServiceBus) so that the windows service only needs to place message(s) into the queue and something else (co-located with the web service) can dequeue messages and apply them (either directly or via the web service methods).
When everything is up and running, it shouldn't introduce much more overhead over your current (direct) web service call, and when the web service is down, everything just builds up in the queuing system.

Damien_The_Unbeliever
- 234,701
- 27
- 340
- 448
-
This method has a problem also. If the client closes the computer before the queuing is not finished. I think I will lost data. – osmanraifgunes Dec 03 '13 at 09:50
-
@sanalism - all I'm proposing is a structured way to store the data i.e. it has all of the characteristics of your "write to a file" option - except it's designed to reliably get the information across the network and can be used all of the time rather than being just a fallback option. – Damien_The_Unbeliever Dec 03 '13 at 09:54