none
Can WCF service call the Location REST API for geocode data? RRS feed

  • Question

  • I can connect to Bing Maps Location REST api within a console application. When I copy and paste that exact same code into my WCF service, I receive the error: "No connection could be made because the target machine actively refused it". Why would it not work through WCF?

    public ResponseContract GetGeoCodeCoordinates()
            {
                if (request == null)
                {
                    throw new ArgumentException("Parameter cannot be null", "request");
                } 
                Uri geoCodeRequestURI = null;                  
                ResponseContract response = new ResponseContract();                       
                Location location = null;            
                foreach (var addr in request.Addresses)
                {
                    try
                    {
                        geoCodeRequestURI =
                            new Uri(@"https://dev.virtualearth.net/REST/v1/Locations/50317?&key=XXXXXX");                                        
                        
                        HttpWebRequest requestRestSvc = WebRequest.Create(geoCodeRequestURI) as HttpWebRequest;                    
                        using (HttpWebResponse responseRestSvc = requestRestSvc.GetResponse() as HttpWebResponse)
                        {
                            if (responseRestSvc.StatusCode != HttpStatusCode.OK)
                                throw new Exception(String.Format(
                                "Server error (HTTP {0}: {1}).",
                                responseRestSvc.StatusCode,
                                responseRestSvc.StatusDescription));
                            DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Response));
                            object objResponse = jsonSerializer.ReadObject(responseRestSvc.GetResponseStream());
                            Response jsonResponse = objResponse as Response;
                            if (jsonResponse.ResourceSets.Count() > 0)
                            {
                                for (int i = 0; i < jsonResponse.ResourceSets[0].Resources.Length; i++)
                                {
                                    location = (Location)jsonResponse.ResourceSets[0].Resources[i];
                                    for (int j = 0; j < location.GeocodePoints.Length; j++)
                                    {
                                        Console.WriteLine("RequestID = {0}, Latitude = {1}, Longitude = {2}", 1, location.GeocodePoints[j].Coordinates[0], location.GeocodePoints[j].Coordinates[1]);
                                    }
                                }
                            }
                            else
                            {
                                // response.Coordinates.Add(new CoordinatesContract() { RequestID = addr.RequestId, ErrorResponse = "No coordinates returned" });
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        if (response.Coordinates == null)
                        {
                            response.Coordinates = new List<CoordinatesContract>();
                        }
                        response.Coordinates.Add(new CoordinatesContract(){ RequestID = addr.RequestId, ErrorResponse = ex.Message });
                    }                                           
                }
                return response;
            }        
        } 

    Thursday, February 21, 2013 7:19 PM

Answers

  • Ok. If you are planning to geocode a large number of locations in a short period of time you may want to use the Batch Geocoding service which allows you to geoocde up to 200,000 locations in a single request and is much faster when geocoding larger data sets. It also doesn't eat into your transactions. Information on this service can be found here: http://msdn.microsoft.com/en-us/library/ff701733.aspx

    http://rbrundritt.wordpress.com

    Monday, February 25, 2013 7:32 PM

All replies

  • It is possible to use the REST services inside of a WCF service. I have done this myself a few times in the past. Maybe try it without using https and using http instead.

    http://rbrundritt.wordpress.com

    Thursday, February 21, 2013 8:05 PM
  • This didn't seem to matter either way. Not sure what's going on but thank you for the reply. We're going to architect this a little different now. I'm going to put the code I need in a .dll for a .net app to call. We'll have a process that will cache the results into a db and the web service will just call that.

    Thank You.

    Monday, February 25, 2013 5:12 PM
  • Ok. If you are planning to geocode a large number of locations in a short period of time you may want to use the Batch Geocoding service which allows you to geoocde up to 200,000 locations in a single request and is much faster when geocoding larger data sets. It also doesn't eat into your transactions. Information on this service can be found here: http://msdn.microsoft.com/en-us/library/ff701733.aspx

    http://rbrundritt.wordpress.com

    Monday, February 25, 2013 7:32 PM