I am writing a custom workflow that uses Google's Maps and Time Zone APIs. I have a routine that is passed an address, then calls Google Maps to get the latitude and longitude of the address. Then uses that to call the Timezone API to get the time zone offsets. Before trying this in a custom workflow I have the exact same code in a winforms program to test the code and it works with no errors.
The error that I'm getting is ...
Unhandled Exception: Microsoft.Crm.CrmException: Unexpected exception from plug-in (Execute): CrmGuruConnect.Workflow.CrmGuruConnect: System.Runtime.Serialization.InvalidDataContractException: Type 'CrmGuruConnect.Workflow.TimeZone' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types. at Microsoft.Crm.Sandbox.SandboxCodeUnit.Execute(IExecutionContext context) at Microsoft.Crm.Workflow.Services.ProxyCustomActivity.Execute(CodeActivityContext executionContext)
The routine that produces the error is below...
WebRequest req;
WebResponse resp;
DataContractJsonSerializer deserializer;
TimeZone tzr = new TimeZone();
GeocodeJsonResponse gcr = new GeocodeJsonResponse();
tracingService.Trace("Entering getAddressTimeZone");
string address = street + "," + city + "," + state + "," + zipCode;
string geoUrl = googleMapsUrl + address;
req = System.Net.WebRequest.Create(geoUrl);
resp = req.GetResponse();
deserializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(gcr.GetType());
gcr = deserializer.ReadObject(resp.GetResponseStream()) as GeocodeJsonResponse;
string location = gcr.results[0].geometry.location.lat + "," + gcr.results[0].geometry.location.lng;
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
TimeSpan diff = DateTime.Now.ToUniversalTime() - origin;
double currentTimestamp = Math.Floor(diff.TotalSeconds);
string tzUrl = googleAPIsUrl + location + "×tamp=" + currentTimestamp;
req = System.Net.WebRequest.Create(tzUrl);
resp = req.GetResponse();
deserializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(tzr.GetType());
tzr = deserializer.ReadObject(resp.GetResponseStream()) as TimeZone;
return tzr;
Here is the definition of the TimeZone object...
class TimeZone
{
public double dstOffset { get; set; }
public double rawOffset { get; set; }
public string status { get; set; }
public string timeZoneId { get; set; }
public string timeZoneName { get; set; }
}
As you can see I can deserialize the response from the Google Maps api call (the gcr object) but for some reason I cannot from the Time Zone api. I have doubled checked to make sure that I have defined the attributes with the correct types and from what I can see I have.
Why am I having a problem deserializing the response from the Time Zone API but not the Maps api?
Thanks, Gary