Reputation: 2088
I am trying to obtain driving directions via the Bing Maps REST services. The response seems to be going through, but when deserializing to JSON with the provided Data Contracts the following exception is thrown:
Element ':item' contains data from a type that maps to the name 'http://schemas.microsoft.com/search/local/ws/rest/v1:Route'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'Route' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.
The MSDN link I am working off of is available at: http://msdn.microsoft.com/en-us/library/jj819168.aspx
I have copied the necessary DataContracts from the link on the bottom of the sample, but can't figure out why the exception is occuring when deserializing. Here is the code I am executing:
private async void btnGetRoute_Click(object sender, RoutedEventArgs e)
{
string locationFrom = "100 Commonwealth Ave Boston MA";
string locationTo = "100 Yawkey Way Boston MA";
string url = string.Format("http://dev.virtualearth.net/REST/V1/Routes/Driving?wp.0={0}&wp.1={1}&key={2}", locationFrom, locationTo, ConfigurationManager.AppSettings["BingMapsKey"]);
Uri drivingRouteRequest = new Uri(url);
Response response = await GetResponse(drivingRouteRequest);
}
private async Task<Response> GetResponse(Uri uri)
{
HttpClient client = new System.Net.Http.HttpClient();
var response = await client.GetAsync(uri);
using (var stream = await response.Content.ReadAsStreamAsync())
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Response));
return ser.ReadObject(stream) as Response;
}
}
Any thoughts?
Upvotes: 1
Views: 923
Reputation: 210
This thread at MSDN confirms, that the original classes from Microsoft are not working. But if you just generate your own classen with json2csharp.com and use that ones to deserialize it is working.
Tom
Upvotes: 0
Reputation: 17954
The Bing Maps team is aware of that and have updated documentation coming soon. The issue is that a new object is being returned, where as before we only knew the property name. I have an updated set of data contracts in one of my projects which you can find here: http://mapstoolbox.codeplex.com/SourceControl/latest#Microsoft.Maps.Spatialtoolbox/Source/Microsoft.Maps.SpatialToolbox.Core/Bing/Services/RestServices.cs
Upvotes: 1