简体   繁体   English

将KnownTypeAttribute应用于对象类型?

[英]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.ne​​t/: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.ne​​t/:anyType '包含映射到名称' http://api.wxbug.ne​​t/: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.

相关问题 DataContractJsonSerializer抛出SerializationException,即使我已使用KnownTypeAttribute指定它,但仍抱怨意外类型 - DataContractJsonSerializer throws SerializationException, complains about unexpected type even though I've specified it with KnownTypeAttribute 在DataContractSerializer上添加KnownTypeAttribute? - Adding KnownTypeAttribute on DataContractSerializer? 序列化不接受我的KnownTypeAttribute,为什么? - Serialization doesn't accept my KnownTypeAttribute, why? 使用数据MVC .Net更新对象模型无法将带[]的索引应用于类型对象的表达式 - Update object model with data MVC .Net Cannot apply indexing with [] to an expression of type object 对 IQueryable object 应用过滤器 - Apply filters to IQueryable object 无法将带有 [] 的索引应用于“type”类型的表达式 - Cannot apply indexing with [] to an expression of type 'type' 返回基础对象列表的服务方法给我“复杂类型不能指定KnownTypeAttribute”消息 - Service method returning list of base objects gives me 'Complex types cannot specify a KnownTypeAttribute' message 通常将通用TypeConverter应用于现有的通用类型 - Generically apply a generic TypeConverter to an existing generic type 无法将带有[]的索引应用于IConfiguration类型的表达式 - Cannot apply indexing with [] to an expression of type IConfiguration 不能将运算符'<'应用于T和T类型的操作数 - Cannot apply Operator '<' to operands of type T and T
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM