【问题标题】:WCF SOAP service changing name of array item elements when (de)serializingWCF SOAP 服务在(反)序列化时更改数组项元素的名称
【发布时间】:2010-10-29 16:07:48
【问题描述】:

我有一个客户端(它发送的 SOAP 不是很灵活),它从带有签名 void Test(int[] test) 的方法生成类似于以下内容的 SOAP:

<?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>

我需要我的 WCF SOAP Web 服务来正确反序列化它。

默认情况下会抛出以下异常:

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);)。

我尝试了XmlSerializerFormatXmlArrayItemXmlArray 的各种其他组合,但没有成功。

我需要做些什么才能使其按预期工作?

【问题讨论】:

    标签: c# wcf serialization soap


    【解决方案1】:

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

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

    【讨论】:

    • 我过去遇到过这个问题。如果您不知道要寻找什么,就很难弄清楚。
    • 谢谢,[CollectionDataContract] 做到了。这里解释了msdn.microsoft.com/en-us/library/aa347850.aspx
    • 所以您必须将 List 扩展为自定义类型才能使其正常工作?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多