简体   繁体   English

XML&WCF POST请求:读取某些参数,其他则不读取

[英]XML&WCF POST request: some parameters are read, other not

I have a wcf restful service on a IIS server. 我在IIS服务器上有一个wcf restful服务。

I have made some API, which can be called sending both xml or json. 我做了一些API,可以称为发送xml或json。

I've made my C# classes and then, I'm testing it. 我已经制作了C#类,然后对其进行测试。 With JSON is perfect, but I have still some issues with XML request. 使用JSON是完美的,但是XML请求仍然存在一些问题。

I want to send the xml with a post and this is the xml I send: 我想发送带有帖子的xml,这是我发送的xml:

 
 
 
 
  
  
  <?xml version="1.0" encoding="utf-8" ?> <SetClientiXML xmlns="http://tempuri.org/"> <dati> <ArrayOfWrapClienti xmlns="http://schemas.datacontract.org/2004/07/MultipayOnline" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <WrapClienti> <CODRETE>0018</CODRETE> <CODICE>20685</CODICE> <NOME>ATER Azienda Territoriale</NOME> <INDIRIZZO>PIAZZA POZZA</INDIRIZZO> <CITTA>Verona</CITTA> <CAP>37123</CAP> <PROV>VR</PROV> <CODICEFISCALE>00223640236</CODICEFISCALE> <PIVA>223640236</PIVA> <EMAIL/> <ESPOSIZ_CONTABILE>937,02</ESPOSIZ_CONTABILE> <STATO>FALSE</STATO> </WrapClienti> </ArrayOfWrapClienti> </dati> <retista>3303903</retista> <hashedString>oklkokokokok</hashedString> </SetClientiXML>
 
 
  

the wcf read well "retista" and "hashedString", but "dati" is empty (0 elements), while I expect it has got the "wrapClienti" object I sent. WCF读得很好“ retista”和“ hashedString”,但“ dati”为空(0个元素),而我希望它已经收到了我发送的“ wrapClienti”对象。 This is the prototype of my API: 这是我的API的原型:

    [OperationContract]
    [WebInvoke(UriTemplate = "SetClienti.xml", Method = "POST", BodyStyle=WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Xml)]
    GenericResponse SetClientiXML(List<WrapClienti> dati, string retista, string hashedString);

So, the problem is that the List is empty.. why? 因此,问题在于列表为空..为什么? How can I write the xml to make readable the list? 如何编写xml以使列表可读? Ask me if I can give to you more details. 问我是否可以给您更多细节。

UPDATE: More weird!! 更新:更奇怪!

With this xml: 使用此xml:

<?xml version="1.0" encoding="utf-8" ?><SetClientiXML xmlns="http://tempuri.org/">
    <dati>
        <WrapClienti xmlns="http://schemas.datacontract.org/2004/07/MultipayOnline" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
            <CODRETE>0018</CODRETE> 
            <CODICE>20685</CODICE> 
            <NOME>A.T.E.R. Azienda Territoriale</NOME> 
            <INDIRIZZO>PIAZZA POZZA</INDIRIZZO> 
            <CITTA>Verona</CITTA> 
            <CAP>37123</CAP>
            <PROV>VR</PROV> 
            <CODICEFISCALE>00223640236</CODICEFISCALE> 
            <PIVA>223640236</PIVA> 
            <EMAIL/> 
            <ESPOSIZ_CONTABILE>937,02</ESPOSIZ_CONTABILE> 
            <STATO>FALSE</STATO> 
        </WrapClienti> 
    </dati>
    <retista>3303903</retista>
    <hashedString>oklkokokokok</hashedString>
</SetClientiXML>

the wcf read some attributes of the List, and other.. are nul!!! wcf读取了列表的某些属性,而其他..都是nul !!!

I my WrapClienti I have a lof of attributes. 我的WrapClienti我有一个lof属性。 Two of them are: 其中两个是:

    private string p_CAP { get; set; }
    public string CAP
    {
        get
        {
            if (model == null)
                return p_CAP.ToSafeString();
            else
                return this.model.CAP.ToSafeString();
        }
        set { p_CAP = value; }
    }
    private string p_PROV { get; set; }
    public string PROV
    {
        get
        {
            if (model == null)
                return p_PROV.ToSafeString();
            else
                return this.model.PROV.ToSafeString();
        }
        set { p_PROV = value; }
    }

the problem is, with the xml above and with two breakpoint on the two set methods, only the set of PROV is called and, the one of CAP, not!!! 问题是,使用上面的xml和两个set方法上的两个断点,仅调用了PROV的集合,而CAP中的一个则不被调用! Why? 为什么? Now I'm really getting crazy... why this behavior?? 现在我真的快要疯了...为什么要这样?

Solution here . 解决这里

This has to do with the ordering of the fields in your XML. 这与XML中字段的顺序有关。 It sounds very strange, but WCF DataContractSerializer is really fussy about the order in which the fields are encountered in the XML, but even worse, also in comparison to how they are defined in the code . 听起来很奇怪,但是WCF DataContractSerializer对于XML中字段的顺序确实很挑剔,但与代码中的定义方式相比,更糟糕的

You see, the serializer wants the fields to be defined in alphabetical order, and if you serialize an instance of your class, you will find that the resulting XML fields are in alphabetical order. 您会看到,序列化程序希望按字母顺序定义字段,如果序列化类的实例,则会发现生成的XML字段按字母顺序排列。 However, on deserialization, the serializer finds that the type you want to deserialize to has the fields defined in the "wrong" order. 但是,在反序列化时,序列化程序会发现您要反序列化的类型具有按“错误”顺序定义的字段。 In this situation the behavior can seem random, but I think it has something to do with the fact that CAP should be the first field encountered, whereas PROV should be the last field, alphabetically. 在这种情况下,行为似乎是随机的,但我认为这与以下事实有关:CAP应该是遇到的第一个字段,而PROV应该是按字母顺序排列的最后一个字段。

So you have two options: 因此,您有两种选择:

  1. Reorder your XML and the fields in your class to be in alphabetical order, or 将您的XML和类中的字段重新排序为字母顺序,或者

  2. Decorate your class members with the DataMemeber property, and define the order of serialization. 用DataMemeber属性装饰您的类成员,并定义序列化的顺序。

You can do 2 like this: 您可以这样做2:

[DataContract]
public class WrapClienti
{ 
    [DataMember(Order=1)]
    public string CAP { get; set; } 

    [DataMember(Order=2)]
    public string PROV { get; set; } 

    ...etc
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM