IIS 7中托管的Restful Web服务不处理数据协定成员

时间:2012-08-23 18:45:10

标签: web-services iis-7 c#-3.0 datacontract datamember

我需要创建一个在IIS 7中托管的简单HTTP C#restful Web服务,该服务可以支持来自callout的POST,并且POST数据的格式似乎无法正确处理。对数据成员名称的要求是它们中包含破折号,例如custom-1。我的问题是,自定义10或更高版本的数据名称只给我数据空值。 custom-1到custom-9很好。

如果有人可以提供帮助,我很感激!

以下是POST数据所需的格式:

 <person-search-request xmlns="someurl"> 
    <person>
        <custom-1 />
        <custom-2 />
        <custom-3 />
        <custom-4 />
        <custom-5 />
        <custom-6 />
        <custom-7 />
        <custom-8 />
        <custom-9 />
        <custom-10 />
        <custom-11 />
        <custom-12 />
        <custom-13 />
        <custom-14 />
        <custom-15 />
        <custom-16 />
        <custom-17 />
        <custom-18 />
        <custom-19 />
        <custom-20 />
      </person>
     </person-search-request>

我的Web服务数据合约如下所示:

    [CollectionDataContract(Name = "person-search-request", Namespace="")]
    public class PersonsRequest : List<Person>
    { }

    [CollectionDataContract(Name = "person-search-response", Namespace="")]
    public class PersonsResponse : List<Person>
    { }

    [DataContract(Name = "person", Namespace = "")]
    public class Person
    {
        public Person()
        {
            Custom14 = String.Empty;
            Custom13 = String.Empty;
            Custom15 = String.Empty;
            Custom16 = String.Empty;
            Custom17 = String.Empty;
            Custom18 = String.Empty;
            Custom19 = String.Empty;
            Custom20 = String.Empty;
            Custom7 = String.Empty;
            Custom8 = String.Empty;
            Custom9 = String.Empty;
            Custom1 = String.Empty;
            Custom10 = String.Empty;
            Custom11 = String.Empty;
            Custom12 = String.Empty;
            Custom3 = String.Empty;
            Custom4 = String.Empty;
            Custom5 = String.Empty;
            Custom6 = String.Empty;
        }


        [DataMember(Name = "custom-14")]
        public string Custom14 { get; set; }
        [DataMember(Name = "custom-7")]
        public string Custom7 { get; set; }
        [DataMember(Name = "custom-8")]
        public string Custom8 { get; set; }
        [DataMember(Name = "custom-9")]
        public string Custom9 { get; set; }
        [DataMember(Name = "custom-13")]
        public string Custom13 { get; set; }
        [DataMember(Name = "custom-15")]
        public string Custom15 { get; set; }
        [DataMember(Name = "custom-16")]
        public string Custom16 { get; set; }
        [DataMember(Name = "custom-17")]
        public string Custom17 { get; set; }
        [DataMember(Name = "custom-18")]
        public string Custom18 { get; set; }
        [DataMember(Name = "custom-19")]
        public string Custom19 { get; set; }
        [DataMember(Name = "custom-20")]
        public string Custom20 { get; set; }
        [DataMember(Name = "custom-10")]
        public string Custom10 { get; set; }
        [DataMember(Name = "custom-11")]
        public string Custom11 { get; set; }
        [DataMember(Name = "custom-12")]
        public string Custom12 { get; set; }
        [DataMember(Name = "custom-1")]
        public string Custom1 { get; set; }
        [DataMember(Name = "custom-2")]
        public string Custom2 { get; set; }
        [DataMember(Name = "custom-3")]
        public string Custom3 { get; set; }
        [DataMember(Name = "custom-4")]
        public string Custom4 { get; set; }
        [DataMember(Name = "custom-5")]
        public string Custom5 { get; set; }
        [DataMember(Name = "custom-6")]
        public string Custom6 { get; set; }
        /// <summary>


    }

我的服务端点设置如下:

    [ServiceBehavior(IncludeExceptionDetailInFaults = true), AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed), ServiceContract]
public partial class Service
{

     [WebHelp(Comment = "Person POST")]
    [WebInvoke(UriTemplate = "person/v1.0/fetch", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml)]
    [OperationContract]
    public PersonsResponse FetchPerson(PersonsRequest request)
    {
            ....
     }

}

Post数据看起来像这样(使用Fiddler进行测试):

    <person-search-request> 
    <person>
        <custom-1>1</custom-1>
        <custom-2>2</custom-2>
        <custom-3>3</custom-3>
        <custom-4>4</custom-4>
        <custom-5>5</custom-5>
        <custom-6>6</custom-6>
        <custom-7>7</custom-7>
        <custom-8>8</custom-8>
        <custom-9>9</custom-9>
        <custom-10>10</custom-10>
        <custom-11>11</custom-11>
        <custom-12>12</custom-12>
        <custom-13>13</custom-13>
        <custom-14>14</custom-14>
        <custom-15 />
        <custom-16 />
        <custom-17 />
        <custom-18 />
        <custom-19 />
        <custom-20 />
     </person>
</person-search-request>

数据在10-20个自定义标记中以空值进入端点。谁知道为什么??? :(

这是我从传递给方法的请求对象调试时看到的。

     Custom1    "1" string
    Custom10    null    string
    Custom11    null    string
    Custom12    null    string
    Custom13    null    string
    Custom14    null    string
    Custom15    null    string
    Custom16    null    string
    Custom17    null    string
    Custom18    null    string
    Custom19    null    string
    Custom2 "2" string
    Custom20  null  string
    Custom3 "3" string
    Custom4 "4" string
    Custom5 "5" string
    Custom6 "6" string
    Custom7 "7" string
    Custom8 "8" string
    Custom9 "9" string

1 个答案:

答案 0 :(得分:0)

我能够使用order属性修复此问题。仍然行事不可预测。如果其他人有更好的解释,将不胜感激!!!

无论如何,这是修复(还有其他不重要的成员开始使用1&amp; 2插槽):

        [DataMember(Name = "custom-1", Order = 3)]
        public string Unused1 { get; set; }
        [DataMember(Name = "custom-2", Order = 4)]
        public string Unused2 { get; set; }
        [DataMember(Name = "custom-3", Order = 5)]
        public string Unused3 { get; set; }
        [DataMember(Name = "custom-4", Order = 6)]
        public string Unused4 { get; set; }
        [DataMember(Name = "custom-5", Order = 7)]
        public string Unused5 { get; set; }
        [DataMember(Name = "custom-6", Order = 9)]
        public string Unused6 { get; set; }
        [DataMember(Name = "custom-7", Order = 10)]
        public string License { get; set; }
        [DataMember(Name = "custom-8", Order = 11)]
        public string LicenseState { get; set; }
        [DataMember(Name = "custom-9", Order = 12)]
        public string Tax { get; set; }
        [DataMember(Name = "custom-10", Order = 13)]
        public string Unused10 { get; set; }
        [DataMember(Name = "custom-11", Order = 14)]
        public string Unused11 { get; set; }
        [DataMember(Name = "custom-12", Order = 15)]
        public string Unused12 { get; set; }
        [DataMember(Name = "custom-13", Order = 16)]
        public string ProfDesignation { get; set; }
        [DataMember(Name = @"custom-14", Order = 17)]
        public string NPI { get; set; }
        [DataMember(Name = "custom-15", Order = 18)]
        public string Address1 { get; set; }

        [DataMember(Name = "custom-16", Order = 19)]
        public string Address2 { get; set; }

        [DataMember(Name = "custom-17", Order = 20)]
        public string Address3 { get; set; }

        [DataMember(Name = "custom-18", Order = 21)]
        public string City { get; set; }

        [DataMember(Name = "custom-19", Order = 22)]
        public string State { get; set; }

        [DataMember(Name = "custom-20", Order = 23)]
        public string Zip { get; set; }