3 Replies Latest reply on Mar 26, 2012 6:30 PM by ctsurveyors

    What is required for QueryTask Credentials ?

    ctsurveyors
      Hello

      I'm trying to do a query during a Periodic Background Agent on a Secure Service and would like to just get the credentials from the app's local storage after the user has already sign in at least once.

      Here is my code..
      QueryTask queryTask =                 new QueryTask("http://servicesbeta4.esri.com/arcgis/rest/services/SaveTheBay/FeatureServer/0");             queryTask.Credentials = WHAT IS REQUIRED HERE????             queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;              ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query();             query.Where = "Confirmed = '1'";              // This query will just populate the drop-down, so no need to return geometry             query.ReturnGeometry = false;             query.OutFields.Add("*");             queryTask.ExecuteAsync(query);


      What comes after
      queryTask.Credentials = 


      I know the username and password is "user1", but I just don't know how to submit that in code.

      Thanks
        • Re: What is required for QueryTask Credentials ?
          JNery-esristaff
          using System.Net;
                      queryTask.Credentials = new NetworkCredential("userName", "password", "domain");
          • Re: What is required for QueryTask Credentials ?
            ctsurveyors
            Thanks Jennifer

            Unfortunately that wasn't the problem though. I don't think i can do a query for attributes during a periodic task because its on the UI thread according to these responses from other people that got the same error.

            I keep getting "Invalid cross thread access exception" when it hits
            QueryTask queryTask =
                            new QueryTask("http://servicesbeta4.esri.com/arcgis/rest/services/SaveTheBay/FeatureServer/0");
            and found these links for solutions http://stackoverflow.com/questions/8805356/invalid-cross-thread-access-exception-in-periodic-task

            I don't have much time to mess with it, is it possible to run a attribute query during a periodic task?

            using Microsoft.Phone.Controls;
            using Microsoft.Phone.Shell;
            using System.Collections.Generic;
            using System;
            using System.Linq;
            using System.Windows;
            using System.Windows.Controls;
            using System.Windows.Media;
            using System.Windows.Data;
            using ESRI.ArcGIS.Client;
            using ESRI.ArcGIS.Client.Geometry;
            using ESRI.ArcGIS.Client.Symbols;
            using ESRI.ArcGIS.Client.FeatureService;
            using ESRI.ArcGIS.Client.Tasks;
            using Microsoft.Phone.Scheduler;
            using System.Net;
            
            namespace SeaLifeScheduledTask
            {
            
                public class ScheduledAgent : ScheduledTaskAgent
                {
            
                    private static volatile bool _classInitialized;
            
                    // Activate identity manager.
                    /// <remarks>
                    /// ScheduledAgent constructor, initializes the UnhandledException handler
                    /// </remarks>
                    public ScheduledAgent()
                    {
            
                        IdentityManager.Current.ChallengeMethod = Challenge;
                        
                        //ORGINIAL CODE HERE
                        if (!_classInitialized)
                        {
                            _classInitialized = true;
                            // Subscribe to the managed exception handler
                            Deployment.Current.Dispatcher.BeginInvoke(delegate
                            {
                                Application.Current.UnhandledException += ScheduledAgent_UnhandledException;
                            });
                        }
                    }
            
                    
            
                    /// Code to execute on Unhandled Exceptions
                    private void ScheduledAgent_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
                    {
                        if (System.Diagnostics.Debugger.IsAttached)
                        {
                            // An unhandled exception has occurred; break into the debugger
                            System.Diagnostics.Debugger.Break();
                        }
                    }
            
            
                    /// <summary>
                    /// Agent that runs a scheduled task
                    /// </summary>
                    /// <param name="task">
                    /// The invoked task
                    /// </param>
                    /// <remarks>
                    /// This method is called when a periodic or resource intensive task is invoked
                    /// </remarks>
                    protected override void OnInvoke(ScheduledTask task)
                    {
                        //TODO: Add code to perform your task in background
                        IdentityManager.Current.ChallengeMethod = Challenge;
                        
                        //Intial Query to get scheduled Jobs
                        QueryTask queryTask =
                            new QueryTask("http://servicesbeta4.esri.com/arcgis/rest/services/SaveTheBay/FeatureServer/0");
                        queryTask.Credentials = new NetworkCredential("user1", "user1", "servicesbeta4.esri.com"); 
                        queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
            
                        ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query();
                        query.Where = "Confirmed = '1'";
            
                        // This query will just populate the drop-down, so no need to return geometry
                        query.ReturnGeometry = false;
                        query.OutFields.Add("*");
                        queryTask.ExecuteAsync(query);
                    }
            
                    private void Challenge(string url,
                                  IdentityManager.CredentialCompleteHandler callback,
                                  IdentityManager.GenerateTokenOptions options)
                    {
                        // Pop-up dialog box to enter credentials - user action to call GenerateCredentialAsync. 
                        IdentityManager.Current.GenerateCredentialAsync(url, "user1", "user1",
                            (credential, ex) =>
                            // lambda used to create anonymous block so call to GenerateCredentialAsync is self-contained.
                            {
                                callback(credential, ex);
                            },
                            options);                 
                    }
            
                    void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
                    {
                        FeatureSet featureSet = args.FeatureSet;
            
                        //Get Total Jobs in Que
                        int JobCount = featureSet.Features.Count;
                        //string LastJobType = featureSet.Features
            
                        ShellTile PrimaryTile = ShellTile.ActiveTiles.First();
            
                        if (PrimaryTile != null)
                        {
                            StandardTileData tile = new StandardTileData();
            
                            //tile.BackgroundImage = new Uri("images/sadface.png", UriKind.Relative);
                            tile.Count = JobCount;
                            tile.Title = "added by background agent";
                            tile.BackTitle = "Get to work dude";
                            tile.BackContent = "Buddy";
                            PrimaryTile.Update(tile);
                        }
            
                        NotifyComplete();
                    }
                }
            }
            • Re: What is required for QueryTask Credentials ?
              ctsurveyors
              Yay I got it to work see attached for for querying during an background agent on windows phone

              using Microsoft.Phone.Controls;
              using Microsoft.Phone.Shell;
              using System.Collections.Generic;
              using System;
              using System.Linq;
              using System.Windows;
              using System.Windows.Controls;
              using System.Windows.Media;
              using System.Windows.Data;
              using ESRI.ArcGIS.Client;
              using ESRI.ArcGIS.Client.Geometry;
              using ESRI.ArcGIS.Client.Symbols;
              using ESRI.ArcGIS.Client.FeatureService;
              using ESRI.ArcGIS.Client.Tasks;
              using Microsoft.Phone.Scheduler;
              using System.Net;
              
              namespace SeaLifeScheduledTask
              {
              
                  public class ScheduledAgent : ScheduledTaskAgent
                  {
              
                      private static volatile bool _classInitialized;
              
                      // Activate identity manager.
                      /// <remarks>
                      /// ScheduledAgent constructor, initializes the UnhandledException handler
                      /// </remarks>
                      public ScheduledAgent()
                      {
              
                          IdentityManager.Current.ChallengeMethod = Challenge;
                          
                          //ORGINIAL CODE HERE
                          if (!_classInitialized)
                          {
                              _classInitialized = true;
                              // Subscribe to the managed exception handler
                              Deployment.Current.Dispatcher.BeginInvoke(delegate
                              {
                                  //Intial Query to get scheduled Jobs
                                  QueryTask queryTask =
                                      new QueryTask("http://servicesbeta4.esri.com/arcgis/rest/services/SaveTheBay/FeatureServer/0");
                                  queryTask.Credentials = new NetworkCredential("user1", "user1", "servicesbeta4.esri.com");
                                  queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
              
                                  ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query();
                                  query.Where = "Confirmed = '1'";
              
                                  // This query will just populate the drop-down, so no need to return geometry
                                  query.ReturnGeometry = false;
                                  query.OutFields.Add("*");
                                  queryTask.ExecuteAsync(query);
                                  Application.Current.UnhandledException += ScheduledAgent_UnhandledException;
                              });
                          }
                      }
              
                      
              
                      /// Code to execute on Unhandled Exceptions
                      private void ScheduledAgent_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
                      {
                          if (System.Diagnostics.Debugger.IsAttached)
                          {
                              // An unhandled exception has occurred; break into the debugger
                              System.Diagnostics.Debugger.Break();
                          }
                      }
              
              
                      /// <summary>
                      /// Agent that runs a scheduled task
                      /// </summary>
                      /// <param name="task">
                      /// The invoked task
                      /// </param>
                      /// <remarks>
                      /// This method is called when a periodic or resource intensive task is invoked
                      /// </remarks>
                      protected override void OnInvoke(ScheduledTask task)
                      {
                          //TODO: Add code to perform your task in background
                          IdentityManager.Current.ChallengeMethod = Challenge;
                          
                          
                      }
              
                      private void Challenge(string url,
                                    IdentityManager.CredentialCompleteHandler callback,
                                    IdentityManager.GenerateTokenOptions options)
                      {
                          // Pop-up dialog box to enter credentials - user action to call GenerateCredentialAsync. 
                          IdentityManager.Current.GenerateCredentialAsync(url, "user1", "user1",
                              (credential, ex) =>
                              // lambda used to create anonymous block so call to GenerateCredentialAsync is self-contained.
                              {
                                  callback(credential, ex);
                              },
                              options);                 
                      }
              
                      void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
                      {
                          FeatureSet featureSet = args.FeatureSet;
              
                          //Get Total Jobs in Que
                          int JobCount = featureSet.Features.Count;
                          //string LastJobType = featureSet.Features
              
                          ShellTile PrimaryTile = ShellTile.ActiveTiles.First();
              
                          if (PrimaryTile != null)
                          {
                              StandardTileData tile = new StandardTileData();
              
                              //tile.BackgroundImage = new Uri("images/sadface.png", UriKind.Relative);
                              tile.Count = JobCount;
                              tile.Title = "added by background agent";
                              tile.BackTitle = "Get to work dude";
                              tile.BackContent = "Buddy";
                              PrimaryTile.Update(tile);
                          }
              
                          NotifyComplete();
                      }
                  }
              }