代码之家  ›  专栏  ›  技术社区  ›  Redwood

WCF SOAP服务在(反)序列化时更改数组项元素的名称

  •  3
  • Redwood  · 技术社区  · 14 年前

    void Test(int[] test) :

    <?xml version="1.0" encoding="UTF-8" ?>
    <SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <SOAP:Body>
            <Test xmlns="http://www.mydomain.com/">
                <test>
                    <item>1</item>
                    <item>2</item>
                    <item>3</item>
                    <item>4</item>
                    <item>5</item>
                    <item>7</item>
                </test>
            </Test>
        </SOAP:Body>
    </SOAP:Envelope>
    

    默认情况下,它抛出以下异常:

    System.ServiceModel.Dispatcher.NetDispatcherFaultException: The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'Test'. End element 'test' from namespace 'http://www.mydomain.com/' expected. Found element 'item' from namespace 'http://www.mydomain.com/'. Line 6, position 7. ---> System.Xml.XmlException: End element 'test' from namespace 'http://www.mydomain.com/' expected. Found element 'item' from namespace 'http://www.mydomain.com/'. Line 6, position 7.
       at System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader, String res, String arg1, String arg2, String arg3)
       at System.Xml.XmlBaseReader.ReadEndElement()
       at System.ServiceModel.Dispatcher.PrimitiveOperationFormatter.PartInfo.ReadValue(XmlDictionaryReader reader)
       at System.ServiceModel.Dispatcher.PrimitiveOperationFormatter.DeserializeParameters(XmlDictionaryReader reader, PartInfo[] parts, Object[] parameters)
       at System.ServiceModel.Dispatcher.PrimitiveOperationFormatter.DeserializeRequest(XmlDictionaryReader reader, Object[] parameters)
       at System.ServiceModel.Dispatcher.PrimitiveOperationFormatter.DeserializeRequest(Message message, Object[] parameters)
       --- End of inner exception stack trace ---
       at System.ServiceModel.Dispatcher.PrimitiveOperationFormatter.DeserializeRequest(Message message, Object[] parameters)
       at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)
       at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
       at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
       at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
       at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
    

    XmlSerializerFormat 属性,则不会引发异常,但结果数组为空(我怀疑它无法正确识别数组的各个元素)。

    如果您添加 XmlArrayItem 属性,它似乎没有改变任何东西( void Test([XmlArrayItem("item")] int[] test);

    我试过其他各种组合 XmlSerializerFormat格式 XmlArrayItem项目 ,和 XmlArray ,没有运气。

    我需要做什么才能让这个工作如期进行?

    1 回复  |  直到 14 年前
        1
  •  6
  •   Redwood    14 年前

    以下是答案,希望对某人有用:

    [CollectionDataContract(ItemName = "item", Namespace = "http://www.mydomain.com/")]
    public class ClientArray<T> : List<T> {
    
    }
    
    void Test(ClientArray<int> test);