wcf REST response in XML format

2019-08-12 07:55发布

问题:

Following is my Operation Contract

[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Xml,
UriTemplate = "Add?num1={a}&num2={b}")]
Addition AddNumbers(string a, string b);

and Following is my OperationContract implementation and im returning an Addition Class object

public Addition AddNumbers(string a, string b)
{
Addition objadd = new Addition();
objadd.num1 = a;
objadd.num2 = b;
objadd.num3 = (Convert.ToInt32(a )+ Convert.ToInt32(b)).ToString();
return objadd;
}

and following is my DataContract

[DataContract(Name="Add")]
public class Addition
{
    private string _num1;
    private string _num2;
    private string _num3;
    [DataMember(Name="FirstNumber",Order=1)]
    public string num1
    {
        set { _num1 = value; }
        get { return _num1; }
    }
    [DataMember(Name="SecondNumber",Order=2)]
    public string num2
    {
        set { _num2 = value; }
        get { return _num2; }
    }
    [DataMember(Name = "Result",Order=3)]
    public string num3
    {
        set { _num3 = value; }
        get { return _num3; }
    }
}

im testing my service as below http://localhost/Service1.svc/xml/Add?num1=5&num2=4

when i browse above URL , im getting the below output

 <Add 
xmlns="http://schemas.datacontract.org/2004/07/MySampleService"  
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<FirstNumber>5</FirstNumber> 
<SecondNumber>4</SecondNumber> 
<Result>9</Result> 
 </Add>

but my requirement is in such way that my output should look like below

<Add FirstNumber="5" SecondNumbe="4" Result="9">

</Add>

is there anyway i can achieve this with Datamembers

回答1:

Others can elaborate on this more, but I believe that is the output you get because WCF uses the DataContractSerializer by default and it does not support serializing to attributes. If you want to support attributes, you have to use XmlSerializer.

You can specify the serializer on the service level or an operation level.



回答2:

Yes, WCF indeed uses DataContractSerializer by default and it doesn't support Xml Attributes for performance reasons. DataContractSerializer is better in performance than XmlSerializer (around 10%).

You can use DataContractSerializer with [XmlSerializerFormatAttribute] applied on ServiceContract/OperationContract. And then, you can define you DataContract as follows,

[DataContract(Name="Add")]
public class Addition 
{     
    private string _num1;     
    private string _num2;     
    private string _num3;     

    [DataMember(Name="FirstNumber",Order=1), XmlAttribute]     
    public string num1     
    {         
        set { _num1 = value; }         
        get { return _num1; }     
    }     

    [DataMember(Name="SecondNumber",Order=2), XmlAttribute]     
    public string num2     
    {         set { _num2 = value; }         
          get { return _num2; }     
    }     

    [DataMember(Name = "Result",Order=3), XmlAttribute]     
    public string num3     
    {         
        set { _num3 = value; }         
        get { return _num3; }     
    } 
} 

Please note that by doing above, you lose the performance benifit by DataContractSerializer.

HTH, Amit Bhatia



标签: wcf rest