0

I have 2 different async method with different return types. I want to check the connection availability globally between service call. But i don't want to check that in each method

method 1:

public async void method1()
{
   using (HttpClient client = new HttpClient()
   {
      HttpResponseMessage res = await client.GetAsync("url");
   }
}

method 2:

public async void method2()
{
   using (HttpClient client = new HttpClient()
   {
      HttpResponseMessage res = await client.GetAsync("url");
   }
}

thanks in advance

dinesh

GarethJ
  • 6,496
  • 32
  • 42
Dinesh
  • 31
  • 8

2 Answers2

0

You can have a variable (or a method) that both method1 and method2 can refer to and that variable can cache results of a connectivity check. As user1490835 commented above, you can use answer to What is the best way to check for Internet connectivity using .NET? to actually check for connectivity.

Community
  • 1
  • 1
Vijay
  • 481
  • 3
  • 5
  • 13
0

Something like this should work.

assembly reference

System.Net.WebClient

public static bool CheckForInternetConnection()
{
try
{
    using (var client = new WebClient())
    {
        using (var stream = client.OpenRead("http://www.google.com"))
        {
            return true;
        }
    }
}
catch
{
    return false;
}
}
Siby Sunny
  • 714
  • 6
  • 14