문제

In my project I have a view model City that is exposed by wcf.
One of the properties is named differently then the already exposed contract dictates.
Therefore I added the DataMember attribute and set the Name value like so:

<DataContract(Namespace:=ServiceNamespace)> _ 
Public Class City
    private mySelectedTranslation as String
    <DataMember(Name:="CityName")> _
    Public Property SelectedTranslation() As String
          Get
              Return mySelectedTranslation
          End Get
          Set (ByVal value As String)
              mySelectedTranslation = value
          End Set
      End Property
End Class

In my consuming test project the service reference doesn't seem to pick up on the DataMember attribute however and receives the SelectedTranslation property instead of the CityName property.

What am I missing?

UPDATE
I found out that when I remove the ServiceContract's XmlSerializerFormat(Style:=OperationFormatStyle.Rpc) setting, the datamember attribute is correctly used. I'm thinking the RPC XmlSerializer might be bugged?

도움이 되었습니까?

해결책

There are two default serializers for XML in WCF: the DataContractSerializer (DCS) and the XmlSerializer. The former understands attributes such as <DataContract> and <DataMember> (from the System.Runtime.Serialization namespace). The latter understands attributes from the System.Xml.Serialization namespace, such as <XmlElement>, <XmlAttribute>, etc. When you decorate your contract with <XmlSerializerFormat>, you're telling WCF to use the XmlSerializer, so it will ignore any of the DCS-specific attributes (DCS is the default).

If you want to change the element name while using the XmlSerializer, you can use the <XmlElement> attribute:

<XmlType(Namespace:=ServiceNamespace)> _
Public Class City
    private mySelectedTranslation as String
    <XmlElement(ElementName:="CityName")> _
    Public Property SelectedTranslation() As String
          Get
              Return mySelectedTranslation
          End Get
          Set (ByVal value As String)
              mySelectedTranslation = value
          End Set
      End Property
End Class
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top