【发布时间】:2009-08-18 14:30:35
【问题描述】:
在 VB.NET(使用 Visual Studio 2008)中,我的 WCF 服务有一个类似的接口:
<ServiceContract()> _
Public Interface IThingService
<OperationContract()> _
Function GetThingByNumber(ByVal thingNumber As MyKeyClass) As Thing
<OperationContract()> _
Function GetThing(ByVal thingId As Guid) As Thing
' ...
End Interface
我最近更改了两个具有相似代码的项目,以使用 basicHttpBinding 而不是 wsHttpBinding。一切都在服务端编译得很好。现在,在客户端应用程序中,我选择“更新服务参考”。在一个项目中,我生成的 reference.vb 似乎是正确的——不到 100 行,每种方法都有简单的包装器。但是,另一方面,生成的 reference.vb 似乎无法理解服务是什么。我得到一个超过 1000 行的 reference.vb,如下所示:
'------------------------------------------------------------------------------
' <auto-generated>
' This code was generated by a tool.
' Runtime Version:2.0.50727.3053
'
' Changes to this file may cause incorrect behavior and will be lost if
' the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------
Option Strict On
Option Explicit On
Imports System.Data
Namespace ThingService
<System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0"), _
System.ServiceModel.ServiceContractAttribute(ConfigurationName:="GetThingByVersion.IGetThingByVersion")> _
Public Interface IThingService
'CODEGEN: Parameter 'GetThingByNumberResult' requires additional schema information that cannot be captured using the parameter mode. The specific attribute is 'System.Xml.Serialization.XmlElementAttribute'.
<System.ServiceModel.OperationContractAttribute(Action:="http://tempuri.org/ThingService/GetThingByVersion", ReplyAction:="http://tempuri.org/ hingService/GetThingByVersionResponse"), _
System.ServiceModel.XmlSerializerFormatAttribute()> _
Function GetThingByNumber(ByVal request As ThingService.GetThingByVersionRequest) As ThingService.GetThingByVersionResponse
'CODEGEN: Parameter 'GetThingResult' requires additional schema information that cannot be captured using the parameter mode. The specific attribute is 'System.Xml.Serialization.XmlElementAttribute'.
<System.ServiceModel.OperationContractAttribute(Action:="http://tempuri.org/ThingService/GetThing", ReplyAction:="http://tempuri.org/ThingService/GetThingResponse"), _
System.ServiceModel.XmlSerializerFormatAttribute()> _
Function GetThing(ByVal request As ThingService.GetThingRequest) As ThingService.GetThingResponse
'...
End Interface
'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3082"), _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://schemas.datacontract.org/2004/07/ThingLibraryCore")> _
Partial Public Class MyKeyClass
Inherits Object
Implements System.ComponentModel.INotifyPropertyChanged
Private concatenatedThingNumberField As String
Private ThingNumberField As Integer
Private ThingNumberFieldSpecified As Boolean
'... goes on and on...
好像生成的代码对我的实际服务接口一无所知。知道如何解决这个问题吗?提前致谢。
编辑:看起来我需要确保服务器可以使用 DataContractSerializer 而不是 XmlSerializer:请参阅 http://blogs.msdn.com/sonuarora/archive/2007/06/16/contract-generation-from-wsdl-xml-schema-datacontractserializer-vs-xmlserializer.aspx 。有谁知道我如何弄清楚我的代码(可能在 Class Thing 中)违反了 DataContractSerializer 的限制?
【问题讨论】:
-
查看生成代码中的cmets。他们在告诉你出了什么问题。
-
我听不懂。什么是“参数模式”?它应该在哪里获取模式信息? XmlElementAttribute 应该告诉我什么?我在谷歌上搜索无济于事。
-
注意,例如GetThing(Guid)在接口中,但是生成的客户端有GetThing(String)。我也想不出那部分。
-
有谁知道我如何弄清楚我的代码(可能在 Class Thing 中)违反了 DataContractSerializer 的限制?
-
我是对的:Thing 中有一个属性是 DataContractSerializer 无法处理的,因此它回退到使用 XmlSerializer,这会生成不可用的架构/客户端代码。有问题的属性继承自通用 Dictionary(Of string, OtherClass)。我会使用 KnownTypeAttribute 来克服这个问题,但我不能,因为它的属性似乎不适用于泛型。我将为 Dictionary(Of string, OtherClass) 创建一个包装器,而不是从它继承。
标签: .net visual-studio wcf xml-serialization datacontractserializer