Apply KnownTypeAttribute to Object type?

847 views Asked by At

I'm attempting to use the WeatherBug API (WSDL is here). I'm attempting to learn some things about WCF.

I used svcutil.exe to create the classes from the WSDL from the link above.

I then tried to execute the following code to test it:

var proxy = new WeatherBugWebServicesSoapClient();
var stations = proxy.GetStationListByUSZipCode("97211", UnitType.English, "myapikey");

I end up getting a NetDispatcherFaultException:

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://api.wxbug.net/:GetStationListByUSZipCodeResponse. The InnerException message was 'Error in line 1 position 352. Element 'http://api.wxbug.net/:anyType' contains data from a type that maps to the name 'http://api.wxbug.net/:ApiStationData'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'ApiStationData' 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.'. Please see InnerException for more details.

It appears that I should be getting back an object of type ArrayOfAnyType that is declared like this (generated by svcutil.exe):

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.CollectionDataContractAttribute(Name = "ArrayOfAnyType", Namespace = "http://api.wxbug.net/", ItemName = "anyType")]
public class ArrayOfAnyType : System.Collections.Generic.List<object> {
}

The array looks like it should be a collection of ApiStationData, which is declared like this:

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name = "ApiStationData", Namespace = "http://api.wxbug.net/")]
public partial class ApiStationData : object, System.Runtime.Serialization.IExtensibleDataObject {

From what I've read on various web pages, I think I need to add a KnownTypeAttribute to the Object class, since the ArrayOfAnyType is a List<object>. I don't think that's possible, though (or correct)...

It's also odd that the exception mentions http://api.wxbug.net/:anyType, but there isn't a definition for anyType in the svcutil generated code (which is why I'm making the assumption about the ArrayOfAnyType).

Any suggestions on how to solve this issue?

1

There are 1 answers

3
Honza Brestan On BEST ANSWER

KnownTypeAttribute is used at the data contract, not the target type - that would be really impractical for many reasons. Check the Example section here for the application.

From what it seems in your case, the data contract doesn't know the ApiStationData type, so adding [KnownType(typeof (ApiStationData))] to your data contract should solve the issue.