OnDeserializing 回调后,WCF 反序列化将 DataMember 值重置为默认值

发布于 2024-11-11 06:41:02 字数 1051 浏览 2 评论 0原文

我开发了一个托管在 IIS 7.5 中的 WCF 服务应用程序,目标是 .NET 3.5,仅配置了 basicHttpBinding 端点。 OperationContract 签名由 Composite 类型组成,其中其属性之一是自定义类型。当使用客户端未初始化此属性时,服务上的反序列化器似乎会忽略该属性,使其为空/无。我想初始化这个自定义类型(如果它为空/无),并且我意识到 WCF 序列化不会调用构造函数,因此我使用了反序列化回调。回调执行并初始化类型,但在回调完成后此属性立即返回 null/nothing。单步执行代码,ExtensionData 属性设置器在回调后立即执行,此时我注意到该属性已重置为 null/无。我缺少什么?这是我的示例代码

 <DataContract(Name:="Request")> _
Public Class Request
    Implements IExtensibleDataObject

    <DataMember(Name:="MyCustomType")>
    Public MyCustomType As CustomType 

    Private _ExtensionDataObject As ExtensionDataObject

    Public Overridable Property ExtensionData() As ExtensionDataObject Implements IExtensibleDataObject.ExtensionData
        Get
            Return _ExtensionDataObject
        End Get
        Set(value As ExtensionDataObject)
            _ExtensionDataObject = value
        End Set
    End Property


    <OnDeserializing()>
    Sub OnDeserializing(c As StreamingContext)
        Me.myCustomType = New CustomType()
    End Sub

End Class

I have developed a WCF Service Application hosted in IIS 7.5 targeting .NET 3.5 configured with only a basicHttpBinding endpoint. The OperationContract signature consists of a Composite type where one of its properties is a custom type. When this property is not initialized by the consuming client, the deserializer on the service appears to ignore the property leaving it null/nothing. I would like to initialize this custom type if it null/nothing and I realize that WCF serialization doesn't call constructors so I've used the deserialization callback. The callback executes and intializes the type but immediately after the callback completes this property returns to null/nothing. Stepping through the code, the ExtensionData property setter executes immediately after the callback and it is at this point where I notice that the property is reset to null/nothing. What am I missing? Here is my sample code

 <DataContract(Name:="Request")> _
Public Class Request
    Implements IExtensibleDataObject

    <DataMember(Name:="MyCustomType")>
    Public MyCustomType As CustomType 

    Private _ExtensionDataObject As ExtensionDataObject

    Public Overridable Property ExtensionData() As ExtensionDataObject Implements IExtensibleDataObject.ExtensionData
        Get
            Return _ExtensionDataObject
        End Get
        Set(value As ExtensionDataObject)
            _ExtensionDataObject = value
        End Set
    End Property


    <OnDeserializing()>
    Sub OnDeserializing(c As StreamingContext)
        Me.myCustomType = New CustomType()
    End Sub

End Class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

臻嫒无言 2024-11-18 06:41:02

如果客户端没有初始化该属性,那么它的值实际上是 Nothing,并且它为 null/Nothing 的事实将出现在序列化的 Request 对象中。因此,在反序列化发生之前,将调用 OnDeserializing 方法,并初始化变量;但随后发生反序列化,并且由于该属性有一个值(恰好是 Nothing/null),因此它将覆盖它。

我认为您想要的是有一个 OnDeserializ*ed* 回调,如果它的值为 Nothing,它将在反序列化发生后初始化该成员:

<OnDeserialized()>
Sub OnDeserialized(ByVal c as StreamingContext)
    If Me.myCustomType Is Nothing Then
        Me.myCustomType = new CustomType()
    End If
End Sub

If the client didn't initialize the property, then it's value is actually Nothing, and the fact that it is null/Nothing will be present in the serialized Request object. So prior to the deserialization happening, your OnDeserializing method is called, and it initializes the variable; but then the deserialization happens, and since there is a value for the property (which happens to be Nothing/null), it will override it.

I think what you want is to have an OnDeserializ*ed* callback, which will initialize the member after the deserialization happened, if it's value is Nothing:

<OnDeserialized()>
Sub OnDeserialized(ByVal c as StreamingContext)
    If Me.myCustomType Is Nothing Then
        Me.myCustomType = new CustomType()
    End If
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文