0

我正在实现一个 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 提供服务是空的。

有什么解决办法吗?

4

1 回答 1

1

使用IEnumerable而不是 List List<phoneBook>


编辑:

考虑使用IEnumerablelike 一个选项。但是你也需要对你的代码进行一些更新。DataContract首先,您应该使用Attribute标记所有类,提供与您标记的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; }
    }
于 2013-03-06T10:51:53.750 回答