0

I have a Silverlight 4 app that runs out of the browser, it also has a web service that it talks to for specific functionality.

I am trying to figure out how to check to see if the web service is available because the app is crashing when there is a break in internet connectivity.

KenEucker
  • 4,932
  • 5
  • 22
  • 28

2 Answers2

4

The simplest answer is to make a request to the web service! Anything else you do will tell you if you can talk to some particular host or other, but that's not really what you care about... you care about whether or not you can talk to that particular web service. The web service being down is equivalent to the internet being down, as far as your app is concerned.

Find a cheap and harmless request you can make as a test call, and use that. Of course, just because you have a connection now doesn't mean you'll have one in a couple of minutes... so you should still make sure that your app doesn't actually crash when the connection goes away. That should actually be your first priority IMO: making it fail gracefully.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Correct, correct, and correct. I don't much care about the internet as much as I care if I can connect to the web service, as it's the only thing I am trying to access on the internet. However, in order to make a request to the service (that I know of) is to create the client and then call one of the async functions. But creating the client will crash the app in an internal thread because the service is not available. – KenEucker Nov 23 '10 at 17:22
  • Also, this isn't an answer at all as it does not answer my question, only alluding to how I should approach my solution. I don't know how this got marked as an answer. – KenEucker Nov 23 '10 at 17:29
  • @Epic720: It sounds like your client is broken then. You should look into fixing that instead of trying to find a workaround which will break again when the connection is dropped after the test request. However, it's hard to give any advice without knowing what kind of client you're talking about. Is this code under your control? Autogenerated code? 3rd party code? What kind of crash is it? Do you get an exception and a stack trace? Do you really get a crash on *creating* the client, even without making a request? – Jon Skeet Nov 23 '10 at 17:51
  • @Jon Skeet: Because the web service has asynchronous calls, the crash happens within Reference.cs (An autogenerated file from adding the web service to the project). It seems as though I can make changes to this file, adding a try-catch block everywhere. But I don't see that as an efficient solution. I misspoke about it crashing when creating the client, I can create the client no problem without an internet connection. It only crashes when I call one of the async functions. – KenEucker Nov 23 '10 at 18:05
  • @Epic720: So how are you generating the client? What does the crash look like? Are there any events you can hook up to in order to handle the error? It sounds unlikely that it would give you no way of avoiding the app just dying. – Jon Skeet Nov 23 '10 at 18:32
  • @Jon Skeet: I'm not exactly worried about the crashing. I find it more ridiculous that I can't find a solution to checking to see if the internet is available in Silverlight. I've found a couple of solutions that are supposed to work using NetworkInterface.GetIsNetworkAvailable(), but that function returns true if any network connection is up (which has nothing to do with a valid internet connection). Also one using an external win32 function called InternetGetConnectedState, but I can't get the DLLImport to compile. :( ugh – KenEucker Nov 23 '10 at 18:39
  • @Epic720: Well it partly depends on what you mean by "the internet". If you can get to Bing but not Google, do you have an internet connection or not? It's easy enough to set up an async call to fetch some arbitrary web page. Personally if I were you I *would* be worried about it crashing - because that's the most important thing to fix. If you can *handle* being offline, do you still actually need to detect it first? – Jon Skeet Nov 23 '10 at 19:43
1

I found following solution

NetworkChange.NetworkAddressChanged += (sender, e) =>
{
   if (NetworkInterface.GetIsNetworkAvailable())
   {
       // network available
   }
   else
   {  
       // network is not available
   }
}

proof link

Jorgen
  • 83
  • 1
  • 7
  • This is not specific to an internet connection. This will succeed if there is a connection to a router, and this is not exclusive to an internet connection where utilization of web services is available. – KenEucker Aug 29 '11 at 18:38