Check If Portal Connection Is Valid

526
6
Jump to solution
03-30-2022 07:55 AM
sgn_GSI
New Contributor III

Hello,

After creating a new instance of the ArcGISPortal class, is there a way to check if the connection is still valid? I see that I could check the ExpirationDate of the Credentials. However, that does not help for checking for the scenarios where the user's internet connection drops out or the server hosting their ArcGIS Enterprise is down. Thank you very much for your time.

0 Kudos
1 Solution

Accepted Solutions
JoeHershman
MVP Regular Contributor

I basically just ping the portal and see if I get a response.  I have this run in the background and it sets a flag that I check before doing something that requires a connection

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);
            //just something I know is in the return json
		return jsonObject.ContainsKey("currentVersion");
	}
	catch ( TaskCanceledException )
	{
		//timeout - server down
		return false;
	}
	catch ( Exception )
	{
		//Can connect but server not fully up and running
		return false;
	}
}

 

Thanks,
-Joe

View solution in original post

6 Replies
JoeHershman
MVP Regular Contributor

I basically just ping the portal and see if I get a response.  I have this run in the background and it sets a flag that I check before doing something that requires a connection

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);
            //just something I know is in the return json
		return jsonObject.ContainsKey("currentVersion");
	}
	catch ( TaskCanceledException )
	{
		//timeout - server down
		return false;
	}
	catch ( Exception )
	{
		//Can connect but server not fully up and running
		return false;
	}
}

 

Thanks,
-Joe
sgn_GSI
New Contributor III

Thank you very much for your time. Unfortunately, it does not seem that pinging the server would be a viable solution. Most servers, by default, are configured to deny ICMP echo requests when a server is pinged. Configuring the servers to allow a response to be sent back could pose a security risk.

0 Kudos
JoeHershman
MVP Regular Contributor

I did not mean ping in the literal sense of sending an ICMP request.  It was just meant in a generic way, as send a request to the server and see if you get a response from the server. 

If you look at the code sample it shows that a standard request over 443 is being sent.  The same as any request that is being sent to portal.  Just it requests a very generic reply which can be checked or you can see if it times out instead of getting a reply.  In my case I have this run continuously in the background.  This way prior to doing any operation that requires a connection there is confirmation that a connection to the portal is available.

Thanks,
-Joe
0 Kudos
sgn_GSI
New Contributor III

That is a solution that works for me. Thank you very much for your time. 

0 Kudos
MichaelBranscomb
Esri Frequent Contributor

To find out when the network status changes (and then use an approach like @JoeHershman's) you might be able to use something like the NetworkChange.NetworkAvailabilityChanged Event.

0 Kudos
JoeHershman
MVP Regular Contributor

For internet/cell connectivity I use the Xamarin.Essentials library Connectivity object

Connectivity.ConnectivityChanged += OnConnectivityChanged;

 

Thanks,
-Joe