[英]Apply KnownTypeAttribute to Object type?
I'm attempting to use the WeatherBug API (WSDL is here ). 我正在尝试使用WeatherBug API(WSDL在这里 )。 I'm attempting to learn some things about WCF. 我正在尝试学习一些关于WCF的事情。
I used svcutil.exe to create the classes from the WSDL from the link above. 我使用svcutil.exe从上面的链接创建WSDL中的类。
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
: 我最终得到一个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 . 格式化程序在尝试反序列化消息时抛出异常:尝试反序列化参数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 '. InnerException消息是'第1行位置352中的错误。元素' http://api.wxbug.net/:anyType '包含映射到名称' 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.'. 考虑使用DataContractResolver或将与'ApiStationData'对应的类型添加到已知类型列表中 - 例如,通过使用KnownTypeAttribute属性或将其添加到传递给DataContractSerializer的已知类型列表中。 Please see InnerException for more details. 有关更多详细信息,请参阅InnerException。
It appears that I should be getting back an object of type ArrayOfAnyType
that is declared like this (generated by svcutil.exe): 看来我应该找回一个ArrayOfAnyType
的对象(由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: 该数组看起来应该是ApiStationData
的集合,它的声明如下:
[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>
. 从我在各种网页上看到的内容,我认为我需要向Object
类添加一个KnownTypeAttribute
,因为ArrayOfAnyType
是一个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
). 异常提到http://api.wxbug.net/:anyType
也很奇怪,但svcutil生成的代码中没有anyType
的定义(这就是我对ArrayOfAnyType
做出假设的ArrayOfAnyType
)。
Any suggestions on how to solve this issue? 有关如何解决此问题的任何建议?
KnownTypeAttribute
is used at the data contract, not the target type - that would be really impractical for many reasons. KnownTypeAttribute
用于数据契约, 而不是目标类型 - 由于许多原因,这实际上是不切实际的。 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. 从您的情况看,数据合同不知道ApiStationData
类型,因此将[KnownType(typeof (ApiStationData))]
到数据合同中可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.