Name attribute of DataMember in WCF looks like not

2019-07-14 18:31发布

问题:

I have WCF service exposed to multiple client. In some of client datamember name casing was not proper. My Class properties have invalid property name as per casing standards like

public class TransactionParamter
{
    [DataMember]
    public string orderId;
    [DataMember]
    public string orderDetails;
    [DataMember]
    public double orderSumTotal;
}

I have tried to change it to

public class TransactionParamter
{
    [DataMember(Name= "orderId")]
    public string OrderId;
    [DataMember(Name= "orderDetails")]
    public string OrderDetails;
    [DataMember(Name= "orderSumTotal")]
    public double OrderSumTotal;
}

but when looks like data member Name property not working. I have tried WCF test client and when taking WCF reference it shows peroperty like OrderId and OrderDetails instead of what i thought of the one i declare in Name Attribute. Please help me in correcting it

回答1:

I believe you forgot to decorate your class with DataContract. You need that in order to make custom data member name to work.

[DataContract(Name="transactionParamter")]
public class TransactionParamter
    {
        [DataMember(Name= "orderId")]
        public string OrderId;
        [DataMember(Name= "orderDetails")]
        public string OrderDetails;
        [DataMember(Name= "orderSumTotal")]
        public double OrderSumTotal;
}


标签: c# wcf .net-4.5