本文介绍了调用采用对象列表的WCF方法.通过iPhone应用程序消费的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过iPhone应用程序使用的WCF服务.所有其他接受字符串参数或单个对象的方法都可以正常工作,但是,当我调用采用" List< CustomObjectClass> ssf "的方法时.

I have a WCF service that's consumed via an iPhone application. All other methods that accept string parameters or single objects are working fine, however when I invoke a method that takes a "List<CustomObjectClass> ssf".

我正在将 CustomObjectClass NSMutableArray 传递给此方法,并且出现以下错误:

I am passing an NSMutableArray of CustomObjectClass's to this method and I'm getting the following error:

有什么想法吗?

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><s:Fault><faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:DeserializationFailed</faultcode><faultstring xml:lang="en-AU">The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:ssf. The InnerException message was 'Error in line 2 position 6. Expecting state 'Element'.. Encountered 'Text'  with name '', namespace ''. '.  Please see InnerException for more details.</faultstring><detail><ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><HelpLink i:nil="true"/><InnerException><HelpLink i:nil="true"/><InnerException i:nil="true"/><Message>Error in line 2 position 6. Expecting state 'Element'.. Encountered 'Text'  with name '', namespace ''. </Message><StackTrace>   at ReadArrayOfScanShareFriendFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString , XmlDictionaryString , CollectionDataContract )&#xD;
   at System.Runtime.Serialization.CollectionDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)&#xD;
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)&#xD;
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract&amp; dataContract)&#xD;
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Type declaredType, DataContract dataContract, String name, String ns)&#xD;
   at System.Runtime.Serialization.DataContractSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName, DataContractResolver dataContractResolver)&#xD;
   at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)&#xD;
   at System.Runtime.Serialization.DataContractSerializer.ReadObject(XmlDictionaryReader reader, Boolean verifyObjectName)&#xD;
   at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeParameterPart(XmlDictionaryReader reader, PartInfo part, Boolean isRequest)</StackTrace><Type>System.Runtime.Serialization.SerializationException</Type></InnerException><Message>The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:ssf. The InnerException message was 'Error in line 2 position 6. Expecting state 'Element'.. Encountered 'Text'  with name '', namespace ''. '.  Please see InnerException for more details.</Message><StackTrace>   at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeParameterPart(XmlDictionaryReader reader, PartInfo part, Boolean isRequest)&#xD;
   at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeParameter(XmlDictionaryReader reader, PartInfo part, Boolean isRequest)&#xD;
   at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeParameters(XmlDictionaryReader reader, PartInfo[] parts, Object[] parameters, Boolean isRequest)&#xD;
   at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, String action, MessageDescription messageDescription, Object[] parameters, Boolean isRequest)&#xD;
   at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest)&#xD;
   at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message, Object[] parameters)&#xD;
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&amp; rpc)&#xD;
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</StackTrace><Type>System.ServiceModel.Dispatcher.NetDispatcherFaultException</Type></ExceptionDetail></detail></s:Fault></s:Body></s:Envelope>

推荐答案

从iPhone应用程序发送的SOAP消息似乎不是WCF服务期望的格式.如果是这样,在将数组传递给方法时,您可能不得不对 CustomObjectClass es的 NSMutableArray 的序列化进行更多控制.

It looks like the SOAP message that was sent from iPhone application is not in format expected by the WCF service. If that's the case, you will probably have to take more control over serialization of NSMutableArray of CustomObjectClasses when passing the array to the method.

为了检查是否存在此问题,您可以实现并配置WCF消息检查器,该检查器会将SOAP请求消息写入文件中,然后查看该文件以检查其是否类似于以下SOAP消息:

In order to check whether that is the issue, you could implement and configure WCF message inspector that would write the SOAP request message into a file and then review the file to check whether it looks like following SOAP message:

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"
            xmlns:a="http://www.w3.org/2005/08/addressing">
    <s:Header>
        <a:Action s:mustUnderstand="1">http://tempuri.org/IService/SendData</a:Action>
        <a:MessageID>urn:uuid:8a582916-1b9a-47f8-8fb1-c9ff18420391</a:MessageID>
        <a:ReplyTo>
            <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
        </a:ReplyTo>
        <a:To s:mustUnderstand="1">net.tcp://localhost:13031/Service</a:To>
    </s:Header>
    <s:Body>
        <SendData xmlns="http://tempuri.org/">
            <ssf xmlns:b="http://schemas.datacontract.org/2004/07/Common"
                 xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                <!-- Zero or more CustomObjectClass elements-->
                <b:CustomObjectClass>
                    <!-- Zero or more elements for CustomObjectClass properties -->
                </b:CustomObjectClass>
            </ssf>
        </SendData>
    </s:Body>
</s:Envelope>

实施WCF消息检查器:

Implement WCF message inspector:

  1. 实施WCF消息检查器( IDispatchMessageInspector ).
  2. 实施端点行为( IEndpointBehavior ).
  3. 实施自定义行为扩展元素( BehaviorExtensionElement ).
  1. Implement WCF message inspector(IDispatchMessageInspector).
  2. Implement endpoint behavior (IEndpointBehavior).
  3. Implement custom behavior extension element (BehaviorExtensionElement).

WCF消息检查器:

public class FileOutputMessageInspector : IDispatchMessageInspector
{
    public object AfterReceiveRequest( ref Message request, IClientChannel channel,
        InstanceContext instanceContext )
    {
        string path = Path.Combine(
            AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
            Guid.NewGuid().ToString() + ".xml"
        );
        File.WriteAllText( path, request.ToString() );
        return null;
    }

    public void BeforeSendReply( ref Message reply, object correlationState )
    { }
}

端点行为:

public class FileOutputBehavior : IEndpointBehavior
{
    public void AddBindingParameters( ServiceEndpoint endpoint,
        BindingParameterCollection bindingParameters )
    { }

    public void ApplyClientBehavior( ServiceEndpoint endpoint,
        ClientRuntime clientRuntime )
    {
        throw new ApplicationException( "Behavior is not supported on client side." );
    }

    public void ApplyDispatchBehavior( ServiceEndpoint endpoint,
        EndpointDispatcher endpointDispatcher )
    {
        FileOutputMessageInspector inspector = new FileOutputMessageInspector();
        endpointDispatcher.DispatchRuntime.MessageInspectors.Add( inspector );
    }

    public void Validate( ServiceEndpoint endpoint )
    { }
}

行为扩展元素:

public class FileOutputElement : BehaviorExtensionElement
{
    public override Type BehaviorType
    {
        get { return typeof( FileOutputBehavior ); }
    }

    protected override object CreateBehavior()
    {
        return new FileOutputBehavior();
    }
}

配置WCF消息检查器:

Configure WCF message inspector:

  1. 声明新的行为扩展(确保在 type 属性中使用正确的完整类型名称).
  2. 在端点行为中使用声明的行为扩展.
  3. 参考端点行为.
  1. Declare new behavior extension (Make sure that the correct full type name is used in type attribute).
  2. Use the declared behavior extension in an endpoint behavior.
  3. Reference the endpoint behavior.

使用以下配置作为参考:

Use following configuration as reference:

<system.serviceModel>
    <services>
        <service name="Server.Service">
            <endpoint address=""
                      binding="netTcpBinding" bindingConfiguration="TCP"
                      contract="Common.IService"
                      behaviorConfiguration="RequestMessageToFile"/>
            <host>
                <baseAddresses>
                    <add baseAddress="net.tcp://localhost:13031/Service"/>
                </baseAddresses>
            </host>
        </service>
    </services>
    <bindings>
        <netTcpBinding>
            <binding name="TCP">
                <security mode="None"/>
            </binding>
        </netTcpBinding>
    </bindings>
    <behaviors>
        <endpointBehaviors>
            <behavior name="RequestMessageToFile">
                <requestFileOutput />
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <extensions>
        <behaviorExtensions>
            <add name="requestFileOutput"
                 type="Common.FileOutputElement, Common"/>
        </behaviorExtensions>
    </extensions>
</system.serviceModel>

这篇关于调用采用对象列表的WCF方法.通过iPhone应用程序消费的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 11:58