0

我需要创建一个托管在 IIS 7 中的简单 HTTP C# restful web 服务,该服务可以支持来自标注的 POST,并且 POST 数据的格式似乎没有得到正确处理。数据成员名称的要求是其中包含破折号,例如 custom-1。我的问题是自定义 10 或更高的数据成员名称只给我 null 数据。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
4

1 回答 1

0

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

无论如何,这是修复(还有其他不重要的成员开始使用 1 和 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; }
于 2012-08-24T16:34:16.750 回答