如何将包含列表的xml发送到wcf服务?

时间:2013-03-06 10:30:37

标签: asp.net wcf

我正在实现一个wcf restful服务,我想发送一个包含类列表的xml到wcf服务。其他参数成功传递给服务但列表为空。 我的xml;

    <phoneBookWebServiceSearchResponse xmlns="http://callturksvc.alfion.com.tr">
    <uniqueId>40</uniqueId>
    <webServiceResponseType>SUCCESS</webServiceResponseType>
    <clientQueryNo>0</clientQueryNo>
    <responseMessage>SUCCESS</responseMessage>
    <phoneBookList>
    <phoneBook>
        <cityCode>34</cityCode>
        <cityName>İstanbul</cityName>
        <districtName>Beşiktaş</districtName>
        <firstName>Şuayp</firstName>
        <lastModifiedDateTime>2013-01-28T15:30:36.925+02:00</lastModifiedDateTime>
        <lastName>Çiçek</lastName>
        <phoneBookTypeEnumValue>TURKCELL</phoneBookTypeEnumValue>
        <phoneNumberText>5356875946</phoneNumberText>
    </phoneBook>
    </phoneBookList>
    </phoneBookWebServiceSearchResponse>

将处理此xml的类;

[DataContract(Name = "phoneBookWebServiceSearchResponse", Namespace ="http://callturksvc.alfion.com.tr")]
public class phoneBookWebServiceSearchResponse
{
    [DataMember(Order = 1, Name = "uniqueId")]
    public string uniqueId { get; set; }
    [DataMember(Order = 2, Name = "webServiceResponseType")]
    public string webServiceResponseType { get; set; }
    [DataMember(Order = 3, Name = "clientQueryNo")]
    public string clientQueryNo { get; set; }
    [DataMember(Order = 4, Name = "responseMessage")]
    public string responseMessage { get; set; }
    [DataMember(Order = 5, Name = "phoneBookList")]
    public phoneBookList phoneBookList { get; set; }

    public phoneBookWebServiceSearchResponse()
    {
        uniqueId = string.Empty;
        webServiceResponseType = string.Empty;
        clientQueryNo = string.Empty;
        responseMessage = string.Empty;
        phoneBookList = new phoneBookList();
    }
}


public class phoneBookList
{
    [DataMember(Order = 1, Name = "phoneBook")]
    public List<phoneBook> phoneBook { get; set; }
    public phoneBookList()
    {
        phoneBook = new List<phoneBook>();
    }
}

public class phoneBook
{
    [DataMember(Order = 1, Name = "firstName")]
    public string firstName { get; set; }
    [DataMember(Order = 2, Name = "lastName")]
    public string lastName { get; set; }
    [DataMember(Order = 3, Name = "cityCode")]
    public string cityCode { get; set; }
    [DataMember(Order = 4, Name = "cityName")]
    public string cityName { get; set; }
    [DataMember(Order = 5, Name = "districtName")]
    public string districtName { get; set; }
    [DataMember(Order = 6, Name = "lastModifiedDateTime")]
    public string lastModifiedDateTime { get; set; }
    [DataMember(Order = 7, Name = "phoneBookTypeEnumValue")]
    public string phoneBookTypeEnumValue { get; set; }
    [DataMember(Order = 8, Name = "phoneNumberText")]
    public string phoneNumberText { get; set; }
    public phoneBook()
    {
        firstName = string.Empty;
        lastName = string.Empty;
        cityCode = string.Empty;
        cityName = string.Empty;
        districtName = string.Empty;
        lastModifiedDateTime = string.Empty;
        phoneBookTypeEnumValue = string.Empty;
        phoneNumberText = string.Empty;
    }
}

响应类是;

    [DataContract(Name="CallTurkResponse", Namespace ="http://callturksvc.alfion.com.tr")]
    public class CallTurkResponse
    {
      [DataMember(Name="responseMessage", Order=1)]
      public string responseMessage { get; set; }
     }

我的服务类是;

    [OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Xml,
        RequestFormat = WebMessageFormat.Xml,
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "auth")]
    CallTurkResponse Auth(phoneBookWebServiceSearchResponse res);

最后我的服务是;

    public CallTurkResponse Auth(phoneBookWebServiceSearchResponse res)
    {
        return new CallTurkResponse
        {
            responseMessage = "SUCCESS"
        };
    }

问题是当我发送上面的xml请求时,我得到uniqueId,webServiceResponseType, clientQueryNo和responseMessage在调试器中成功完成。 但是phoneBookList服务空了。

任何解决方案?

1 个答案:

答案 0 :(得分:1)

使用IEnumerable代替列表List<phoneBook>


编辑:

考虑使用IEnumerable之类的选项。但您也需要对代码进行少量更新。首先,您应该使用DataContract属性标记所有类,并提供标记为phoneBookWebServiceSearchResponse类的相同名称空间。其他方式导致xml中的命名冲突。此外,它看起来根本不需要课程phoneBookList

[DataContract(Name = "phoneBookWebServiceSearchResponse", Namespace = "http://callturksvc.alfion.com.tr")]
    public class phoneBookWebServiceSearchResponse
    {
        [DataMember(Order = 1, Name = "uniqueId")]
        public string uniqueId { get; set; }
        [DataMember(Order = 2, Name = "webServiceResponseType")]
        public string webServiceResponseType { get; set; }
        [DataMember(Order = 3, Name = "clientQueryNo")]
        public string clientQueryNo { get; set; }
        [DataMember(Order = 4, Name = "responseMessage")]
        public string responseMessage { get; set; }
        [DataMember(Order = 5, Name = "phoneBookList")]
        public List<phoneBook> phoneBookList { get; set; }
    }


 [DataContract(Name = "phoneBook", Namespace = "http://callturksvc.alfion.com.tr")]
    public class phoneBook
    {
        [DataMember(Order = 1, Name = "firstName")]
        public string firstName { get; set; }
        [DataMember(Order = 2, Name = "lastName")]
        public string lastName { get; set; }
        [DataMember(Order = 3, Name = "cityCode")]
        public string cityCode { get; set; }
        [DataMember(Order = 4, Name = "cityName")]
        public string cityName { get; set; }
        [DataMember(Order = 5, Name = "districtName")]
        public string districtName { get; set; }
        [DataMember(Order = 6, Name = "lastModifiedDateTime")]
        public string lastModifiedDateTime { get; set; }
        [DataMember(Order = 7, Name = "phoneBookTypeEnumValue")]
        public string phoneBookTypeEnumValue { get; set; }
        [DataMember(Order = 8, Name = "phoneNumberText")]
        public string phoneNumberText { get; set; }
    }