未使用WCF Datacontractserializer。 Guid是字符串

时间:2015-01-23 08:53:18

标签: wcf datacontractserializer

我的服务配置中有以下basicHttpBinding。当我使用此服务时,任何guid都被序列化为字符串。仔细检查参考文件后,我看到我的类和对象是使用system.xml.serialization而不是datacontractserializer序列化的。

我在同一项目中有另一项服务,它共享端点行为&配置和服务按预期处理guids。

任何指针都非常感谢!

<service behaviorConfiguration="DefaultServiceBehaviour" name="ExtranetService.Repositories.Client.Submission.PSPSubmission.GoalRepository.GoalRepositoryService">
    <endpoint address="basic" 
              behaviorConfiguration="basicBehaviorConfig"
              binding="basicHttpBinding"
              bindingConfiguration="basicBindingConfig"
              name="basicEndpoint" 
              bindingName="basicBinding"
              contract="ExtranetService.Repositories.Client.Submission.PSPSubmission.GoalRepository.GoalRepositoryService"
              listenUriMode="Explicit" />
    <host>
        <baseAddresses>
            <add baseAddress="https://blah.com/Repositories/client/submission/pspsubmission/GoalRepositoryService.svc"/>
        </baseAddresses>
    </host>
</service>

<basicHttpBinding>
    <binding name="basicBindingConfig" 
             closeTimeout="00:05:00" 
             maxBufferPoolSize="2147483646" maxBufferSize="2147483646"
             maxReceivedMessageSize="2147483646">
       <readerQuotas maxDepth="32" 
                     maxStringContentLength="2147483646"
                     maxArrayLength="2147483646" />
       <security mode="Transport">
           <message clientCredentialType="Certificate" />
        </security>
    </binding>
</basicHttpBinding>

<serviceBehaviors>
    <behavior name="DefaultServiceBehaviour">
    <serviceMetadata />
    <serviceDebug />
        <dataContractSerializer />
    </behavior>
</serviceBehaviors>

<endpointBehaviors>
    <behavior name="basicBehaviorConfig">
        <dataContractSerializer />
    </behavior>
</endpointBehaviors>


<System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234"),  _
System.SerializableAttribute(),  _
System.Diagnostics.DebuggerStepThroughAttribute(),  _
System.ComponentModel.DesignerCategoryAttribute("code"),  _
System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://schemas.datacontract.org/2004/07/ExtranetService.Repositories.Client.Submission.PSPSubmission.GoalRepository")>  _
Partial Public Class GetGoalByIdRequestObject
    Inherits Object
    Implements System.ComponentModel.INotifyPropertyChanged

    Private idField As String

    <System.Xml.Serialization.XmlElementAttribute(Order:=0)>  _
    Public Property Id() As String
        Get
            Return Me.idField
        End Get
        Set
            Me.idField = value
            Me.RaisePropertyChanged("Id")
        End Set
    End Property

    Public Event PropertyChanged As System.ComponentModel.PropertyChangedEventHandler Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    Protected Sub RaisePropertyChanged(ByVal propertyName As String)
        Dim propertyChanged As System.ComponentModel.PropertyChangedEventHandler = Me.PropertyChangedEvent
        If (Not (propertyChanged) Is Nothing) Then
            propertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs(propertyName))
        End If
    End Sub
End Class

修改

这是来自同一项目中另一项服务的代码,显示了预期的guid

<System.Diagnostics.DebuggerStepThroughAttribute(),  _
 System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0"),  _
 System.Runtime.Serialization.DataContractAttribute(Name:="GetClientRecommendationListRequestObject", [Namespace]:="http://schemas.datacontract.org/2004/07/ExtranetService.Repositories.Client.Submi"& _ 
    "ssion.PSPSubmission"),  _
 System.SerializableAttribute()>  _
Partial Public Class GetClientRecommendationListRequestObject
    Inherits Object
    Implements System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged

    <System.NonSerializedAttribute()>  _
    Private extensionDataField As System.Runtime.Serialization.ExtensionDataObject

    <System.Runtime.Serialization.OptionalFieldAttribute()>  _
    Private CLNT_CLIENT_IDField As System.Guid

    <Global.System.ComponentModel.BrowsableAttribute(false)>  _
    Public Property ExtensionData() As System.Runtime.Serialization.ExtensionDataObject Implements System.Runtime.Serialization.IExtensibleDataObject.ExtensionData
        Get
            Return Me.extensionDataField
        End Get
        Set
            Me.extensionDataField = value
        End Set
    End Property

    <System.Runtime.Serialization.DataMemberAttribute()>  _
    Public Property CLNT_CLIENT_ID() As System.Guid
        Get
            Return Me.CLNT_CLIENT_IDField
        End Get
        Set
            If (Me.CLNT_CLIENT_IDField.Equals(value) <> true) Then
                Me.CLNT_CLIENT_IDField = value
                Me.RaisePropertyChanged("CLNT_CLIENT_ID")
            End If
        End Set
    End Property

    Public Event PropertyChanged As System.ComponentModel.PropertyChangedEventHandler Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    Protected Sub RaisePropertyChanged(ByVal propertyName As String)
        Dim propertyChanged As System.ComponentModel.PropertyChangedEventHandler = Me.PropertyChangedEvent
        If (Not (propertyChanged) Is Nothing) Then
            propertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs(propertyName))
        End If
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

GUID不是有效的XML数据类型,因此它们不能用作XSD架构中的成员类型。无论您使用哪个序列化程序,如果您想使用其中一个Http绑定,GUID将被序列化为字符串在架构中定义为字符串。

从此架构生成的任何代理都将创建一个字符串类型的属性,因为它是XSD定义的内容。

重复的问题显示了如何将GUID作为字节数组传递,尽管这会损害互操作性。

另一种选择是将所有数据协定类放在由客户端和服务器代码共享的单独库中。在生成代理而不是创建新代理时,将使用DTO程序集中的类。

另一种替代方法是向代理DTO类添加另一个属性,例如IdAsGuid(),它解析字符串值并返回GUID。所有代理类都是部分的,因此您可以在不修改生成的文件的情况下添加新属性