Problem with GeodatabaseSyncTask.GenerateGeodatabase with authentication

754
2
03-06-2017 01:48 PM
PhilScovis
New Contributor III

My app is crashing when calling GeodatabaseSyncTask.GenerateGeodatabase, with a feature layer that requires authentication.  I have implemented a ChallengeHandler and registered it with the Esri AuthenticationManager.  My authentication code gets called, and a valid token is generated.

The GenerateGeodatabase job fires off several messages as it does its work:

"Job started."

"Creating server job."

"Sending request generate geodatabase on the server."

"Received response for generate geodatabase on the server."

"Getting server job status."

"Sending request generate geodatabase job status."

"Received response for generate geodatabase job status."

"Server job complete."

"Downloading from server."

"Sending request get geodatabase from server."

"Received response for get geodatabase from server."

"Download complete. Geodatabase path: C:\\Users\\pscovis\\AppData\\Local\\Packages\\96a4bdff-4352-4044-b135-a13377470910_c7d7ttxxmcvhj\\LocalState\\MapCache\\televisions.geodatabase"

"Job succeeded."

Then there is an uncaught exception that crashes the app.

No Data

at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Esri.ArcGISRuntime.Tasks.Job`1.<RuntimeCoreNet-GeneratedWrappers-ICoreCallback_Job_JobDone-JobDone>d__8.MoveNext()

 

The message "No Data" is similar to what I have experienced loading a layer from a corrupted .geodatabase file .  Sure enough, the .geodatabase file that was downloaded to the local system contains a short HTML response that looks like a server error.  It contains the text "Token Required", as though the token were never supplied to the service.

Am I doing something wrong that results in the token not being used in the offline sync request?  Does the sync method not check the response codes from the service?  Is the exception not being caught properly?  Does anyone else have problems using offline sync in conjunction with authentication?  

2 Replies
PhilScovis
New Contributor III

Discovered that the error occurs when the layer requires authentication, but the service uri does not use https.  So this is a client error, but still I'd like the sync process to handle it better than crashing the app.

0 Kudos
JenniferNery
Esri Regular Contributor

What platform are you using? I am unable to reproduce the issue with token-secured sync-enabled feature server (repro app is also not using 'https'). Generate and sync geodatabase both worked for me. I only got "Token required" error when I did not wire up the AuthenticationManager.ChallengeHandler.

            AuthenticationManager.Current.ChallengeHandler = new ChallengeHandler(async (info) =>
            {
                var credential = await AuthenticationManager.Current.GenerateCredentialAsync(info.ServiceUri, "<username>", "<password>");
                return credential;
            });‍‍‍‍‍

I'm not sure what is causing the crash in your app but if you are accessing UI elements in JobChanged, you'll need to invoke dispatcher because this may be raised in a different thread.

        private void GenerateJob_JobChanged(object sender, EventArgs e)
        {
            var action = new Action(() =>
            {
                var job = (GenerateGeodatabaseJob)sender;
                var sb = new StringBuilder();
                if (job.Messages != null)
                {
                    foreach (var m in job.Messages)
                        sb.AppendLine(m.Message);
                }
                JobMessageText.Text = sb.ToString();
            });
            Dispatcher.Invoke(action);
        }

I am using the following code to generate

                if (File.Exists(geodatabasePath))
                {
                    geodatabase = await Geodatabase.OpenAsync(geodatabasePath);
                }
                else
                {
                    var extent = MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry).TargetGeometry.Extent;
                    var task = await GeodatabaseSyncTask.CreateAsync(new Uri(FeatureServer));
                    var parameters = await task.CreateDefaultGenerateGeodatabaseParametersAsync(extent);
                    var generateJob = task.GenerateGeodatabase(parameters, geodatabasePath);
                    geodatabase = await generateJob.GetResultAsync();
                }
                foreach (var table in geodatabase.GeodatabaseFeatureTables)
                    MyMapView.Map.OperationalLayers.Add(new FeatureLayer(table));‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Please feel free to share other info to help us reproduce the issue. Thanks.

0 Kudos