//Use the ArcGIS REST API to create a profile line based on the input geometry
    public async Task<Polyline> GetProfileLine(Geometry geometry)
    {
      try
      {
        string requestUrlBase = @"http://elevation.arcgis.com/arcgis/rest/services/Tools/Elevation/GPServer/Profile/";

        //Create the token to use
        TokenService elevationServices = new TokenService();
        _token = await elevationServices.GenerateTokenAsync();

        #region Submit a profile task to be executed asynchronously. A unique job ID will be assigned for the transaction.
        string oidField = "OID";
        string lengthField = "Shape_Length";
        string InputLineFeatures = CreateInputLineFeaturesJson(geometry, oidField, lengthField);
        string additonalParams = "&ProfileIDField=" + oidField + "&DEMResolution=FINEST&MaximumSampleDistance=10&MaximumSampleDistanceUnits=Kilometers&returnZ=true&returnM=true&env%3AoutSR=102100&env%3AprocessSR=102100&f=json";
        string profileServiceUrl = string.Format("{0}submitJob?token={1}&InputLineFeatures={2}{3}", requestUrlBase, _token.AccessToken, InputLineFeatures, additonalParams);

        System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(profileServiceUrl);
        webRequest.Timeout = 0xea60;
        System.Net.WebResponse response = await webRequest.GetResponseAsync(); 
        #endregion

        #region Use the jobId to check the status of the job. Keep checking if the jobStatus is not "Succeeded"
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(JobStatus));
        _jobStatus = (JobStatus)serializer.ReadObject(response.GetResponseStream() as Stream);
        while (_jobStatus.Status.Contains("Executing") || _jobStatus.Status.Contains("esriJobWaiting") || _jobStatus.Status.Contains("Submitted"))
        {
          string statusUrl = string.Format("{0}jobs/{1}?f=pjson&token={2}", requestUrlBase, _jobStatus.Id, _token.AccessToken);
          webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(statusUrl);
          response = await webRequest.GetResponseAsync();
          _jobStatus = (JobStatus)serializer.ReadObject(response.GetResponseStream());
        } 
        #endregion

        #region The job has successfully completed. Use the jobId to retrieve the result, then use the result to create a profile line 
        if (_jobStatus.Status.Contains("Succeeded"))
        {
          string resultsUrl = string.Format("{0}jobs/{1}/results/OutputProfile?returnZ=true&returnM=true&f=pjson&token={2}", requestUrlBase, _jobStatus.Id, _token.AccessToken);
          webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(resultsUrl);
          response = await webRequest.GetResponseAsync();

          serializer = new DataContractJsonSerializer(typeof(OutputProfile));

          //Parse the result as the output profile line
          _outputProfileLine = (OutputProfile)serializer.ReadObject(response.GetResponseStream());
          _outputProfileLine.FeatureSet.HasM = true; 
          _outputProfileLine.FeatureSet.HasZ = true;

          //Create a polyline (profile) from the geometry of the output profile line
          Polyline profile = new Polyline();
          foreach (var points in _outputProfileLine.FeatureSet.Features.FirstOrDefault().Geometry.Paths)
          {
            PointCollection collection = new PointCollection();
            foreach (var point in points)
              collection.Add(
                new MapPoint(
                  Convert.ToDouble(point[0]), //[0] is x
                  Convert.ToDouble(point[1]), //[1] is x
                  Convert.ToDouble(point[2]), //[2] is z
                  Convert.ToDouble(point[3]), //[3] is m
                  new ESRI.ArcGIS.Client.Geometry.SpatialReference(102100)));   
            profile.Paths.Add(collection);
          }

          return profile;
        }
        return null;
        #endregion
      }
      catch (Exception)
      {
        return null;
      }
    }
    /// <summary>
    /// Create a .pdf document based on location and buffer radius
    /// </summary>
    public async Task<string> GetReportGenerationAsync(ESRI.ArcGIS.Client.Graphic feature, int bufferRadius, int wkid)
    {
      //Create a token so we can gain access to the geoenrich service
      TokenService tokenService = new TokenService();
      Token = await tokenService.GenerateTokenAsync();

      string pdfLocation = Assembly.GetExecutingAssembly().Location;
      pdfLocation = pdfLocation.Replace(".dll", ".pdf");
      if (File.Exists(pdfLocation))
        File.Delete(pdfLocation);

      if (bufferRadius <= 0) bufferRadius = 3;

      MapPoint currentLocation = feature.Geometry as MapPoint;

      string token = string.Format("&token={0}", Token.AccessToken);
      string studyAreas = "&studyareas=[{\"geometry\":{\"x\":" + currentLocation.X + ",\"y\":" + currentLocation.Y + "}}]";
      string format = "&format=pdf";
      string studyareasoptions = string.Format("&studyareasoptions=%7B%22areaType%22%3A%22RingBuffer%22%2C%22bufferUnits%22%3A%22esriMiles%22%2C%22bufferRadii%22%3A%5B{0}%5D%7D", bufferRadius);
      string insr = string.Format("&inSR={0}", wkid);
      string f = "&f=bin";
      string reportGenerationUrl =
        @"http://geoenrich.arcgis.com/arcgis/rest/services/World/MapServer/exts/BAServer/Geoenrichment/CreateReport?" + token + studyAreas + format + studyareasoptions + insr + f;
      
      #region Issue the request to generate a report
      WebResponse response;
      try
      {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(reportGenerationUrl);
        webRequest.Timeout = 0xea60;
        response = await webRequest.GetResponseAsync();
        if (response == null || response.ContentLength == 0)
          return null;
      }
      catch
      {
        return null;
      } 
      #endregion

      #region Read the result into pdfLocation
      try
      {
        using (Stream responseStream = response.GetResponseStream())
        {
          using (FileStream fileStream = new FileStream(pdfLocation, FileMode.Create))
          {
            byte[] buffer = new byte[4096];
            int bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length);
            while (bytesRead > 0)
            {
              await fileStream.WriteAsync(buffer, 0, bytesRead);
              bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length);
            }
          }
        }
      }
      catch
      {
        return null;
      } 
      #endregion

      return pdfLocation;
    }
        /// <summary>
        /// Create a .pdf document based on location and buffer radius
        /// </summary>
        public async Task <string> GetReportGenerationAsync(ESRI.ArcGIS.Client.Graphic feature, int bufferRadius, int wkid)
        {
            //Create a token so we can gain access to the geoenrich service
            TokenService tokenService = new TokenService();

            Token = await tokenService.GenerateTokenAsync();

            string pdfLocation = Assembly.GetExecutingAssembly().Location;

            pdfLocation = pdfLocation.Replace(".dll", ".pdf");
            if (File.Exists(pdfLocation))
            {
                File.Delete(pdfLocation);
            }

            if (bufferRadius <= 0)
            {
                bufferRadius = 3;
            }

            MapPoint currentLocation = feature.Geometry as MapPoint;

            string token             = string.Format("&token={0}", Token.AccessToken);
            string studyAreas        = "&studyareas=[{\"geometry\":{\"x\":" + currentLocation.X + ",\"y\":" + currentLocation.Y + "}}]";
            string format            = "&format=pdf";
            string studyareasoptions = string.Format("&studyareasoptions=%7B%22areaType%22%3A%22RingBuffer%22%2C%22bufferUnits%22%3A%22esriMiles%22%2C%22bufferRadii%22%3A%5B{0}%5D%7D", bufferRadius);
            string insr = string.Format("&inSR={0}", wkid);
            string f    = "&f=bin";
            string reportGenerationUrl =
                @"http://geoenrich.arcgis.com/arcgis/rest/services/World/MapServer/exts/BAServer/Geoenrichment/CreateReport?" + token + studyAreas + format + studyareasoptions + insr + f;

            #region Issue the request to generate a report
            WebResponse response;
            try
            {
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(reportGenerationUrl);
                webRequest.Timeout = 0xea60;
                response           = await webRequest.GetResponseAsync();

                if (response == null || response.ContentLength == 0)
                {
                    return(null);
                }
            }
            catch
            {
                return(null);
            }
            #endregion

            #region Read the result into pdfLocation
            try
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (FileStream fileStream = new FileStream(pdfLocation, FileMode.Create))
                    {
                        byte[] buffer    = new byte[4096];
                        int    bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length);

                        while (bytesRead > 0)
                        {
                            await fileStream.WriteAsync(buffer, 0, bytesRead);

                            bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length);
                        }
                    }
                }
            }
            catch
            {
                return(null);
            }
            #endregion

            return(pdfLocation);
        }