How to check if ESRI server is accessible ?

147
4
Jump to solution
12-05-2022 05:55 AM
vergerk
New Contributor II

Hello,

I am using Esri.ArcGISRuntime 100.14.0 and I wanted to know, before I can display the map to my users, if your servers are accessible or not.

The idea is that if the users don't have internet access, are located in an area where access is filtered or the ESRI servers are inaccessible, I don't want to display access to the map and indicate an error message, rather than risk causing an exception (and I don't want to handle everything in a try/catch).

Is this possible?

Sincerely

0 Kudos
2 Solutions

Accepted Solutions
JoeHershman
MVP Regular Contributor

Just use HttpClient to make a call to the endpoint and see if you get a response or a timeout before trying to load the map.  This uses the generic sharing Url, but could use the Url of the basemap itself

public override async Task<bool> CheckIsConnectedAsync()
{
	try
	{
		var tokenUri = new Uri($"{ConnectionUrl}/sharing/rest?f=json");

		HttpClient client = new HttpClient { Timeout = TimeSpan.FromSeconds(7) };
		var result = await client.GetStringAsync(tokenUri);

		var jsonObject = JsonConvert.DeserializeObject<Dictionary<string, object>>(result);

		return jsonObject.ContainsKey("currentVersion");
	}
	catch ( TaskCanceledException )
	{
		//timeout - server down
		return false;
	}
	catch ( Exception )
	{
		//Can connect but server not fully up and running
		return false;
	}
}

 

If you are using Xamarin the Essentials library has methods to check general device connectivity as a way to look at that.

View solution in original post

dotMorten_esri
Esri Notable Contributor

You could just try and load the map and layers. This will load their metadata - if that fails, the service is most likely down.

            try
            {
                await _map.LoadAsync();
                await Task.WhenAll(_map.AllLayers.Select(l => l.LoadAsync()));
            }
            catch (Exception ex)
            {
                // Can't load
            }

View solution in original post

4 Replies
JoeHershman
MVP Regular Contributor

Just use HttpClient to make a call to the endpoint and see if you get a response or a timeout before trying to load the map.  This uses the generic sharing Url, but could use the Url of the basemap itself

public override async Task<bool> CheckIsConnectedAsync()
{
	try
	{
		var tokenUri = new Uri($"{ConnectionUrl}/sharing/rest?f=json");

		HttpClient client = new HttpClient { Timeout = TimeSpan.FromSeconds(7) };
		var result = await client.GetStringAsync(tokenUri);

		var jsonObject = JsonConvert.DeserializeObject<Dictionary<string, object>>(result);

		return jsonObject.ContainsKey("currentVersion");
	}
	catch ( TaskCanceledException )
	{
		//timeout - server down
		return false;
	}
	catch ( Exception )
	{
		//Can connect but server not fully up and running
		return false;
	}
}

 

If you are using Xamarin the Essentials library has methods to check general device connectivity as a way to look at that.

dotMorten_esri
Esri Notable Contributor

You could just try and load the map and layers. This will load their metadata - if that fails, the service is most likely down.

            try
            {
                await _map.LoadAsync();
                await Task.WhenAll(_map.AllLayers.Select(l => l.LoadAsync()));
            }
            catch (Exception ex)
            {
                // Can't load
            }
vergerk
New Contributor II

Hello,

The problem is that the application can be deported on our network and the bandwidth is limited. That's why I want to test the connectivity instead of trying to load the card.

I will test with @JoeHershman's solution with the URL to access the ArcGIS service (https://www.arcgis.com/sharing/rest - https://developers.arcgis.com/net/forms/sample-code/authenticate-with-oauth/) which has the advantage of just testing the connection.

If it doesn't work, I'll try loading the map.

0 Kudos
dotMorten_esri
Esri Notable Contributor

 That's why I want to test the connectivity instead of trying to load the card.

You could start with NetworkInterface.GetIsNetworkAvailable: https://learn.microsoft.com/en-us/dotnet/api/system.net.networkinformation.networkinterface.getisnet...

Joe's code is doing the same thing - making a request to the service, but won't handle authentication, use etag caching etc, not test if the actual services are up and running (but yes it could be used to see if there's any sort of connection to arcgis.com). The benefit of just loading the map/layers is you won't have to load it later and will save you repeating the requests once you do want to load.

0 Kudos