【问题标题】:Silverlight 4 & WCF Deseralizing Issue- There was an error deserializing the object of type 'T'. Unexpected end of fileSilverlight 4 和 WCF 反序列化问题 - 反序列化“T”类型的对象时出错。文件意外结束
【发布时间】:2011-05-01 19:52:54
【问题描述】:

当我将 Silverlight 和 Web 项目分别升级到版本 4 时,我遇到了一个突然暴露的大问题。

由于这样做,我所有的 WCF 服务都失败并显示以下错误消息:

反序列化 Anyboat.Admin.UserService.User 类型的对象时出错。文件意外结束。以下元素未关闭:LastName、AuthenticateUserResult、AuthenticateUserResponse、Body、Envelope。

StackTrace 是:

   at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeParameterPart(XmlDictionaryReader reader, PartInfo part, Boolean isRequest)
   at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, String action, MessageDescription messageDescription, Object[] parameters, Boolean isRequest)
   at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest)
   at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeReply(Message message, Object[] parameters)
   at System.ServiceModel.Dispatcher.ProxyOperationRuntime.AfterReply(ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
   at System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)
   at Anyboat.Admin.UserService.UserServiceClient.UserServiceClientChannel.EndAuthenticateUser(IAsyncResult result)
   at Anyboat.Admin.UserService.UserServiceClient.Anyboat.Admin.UserService.UserService.EndAuthenticateUser(IAsyncResult result)
   at Anyboat.Admin.UserService.UserServiceClient.OnEndAuthenticateUser(IAsyncResult result)
   at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)

我在我的 SilverlightFaultBehaviour 中的响应消息上设置了一个断点,这是我收到的信封响应。注意 *"\r\n"*

之间的回车
"<s:Envelope xmlns:a=\"http://www.w3.org/2005/08/addressing\" xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\">\r\n  <s:Header>\r\n    <a:Action s:mustUnderstand=\"1\">urn:UserService/AuthenticateUserResponse</a:Action>\r\n    <a:RelatesTo>urn:uuid:07cc1361-c74c-4449-b423-e982d2ac8e05</a:RelatesTo>\r\n  </s:Header>\r\n  <s:Body>\r\n    <AuthenticateUserResponse>\r\n      <AuthenticateUserResult xmlns:d4p1=\"http://schemas.datacontract.org/2004/07/AnyBoat.Web.Classes.DataObjects\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">\r\n        <d4p1:Active>true</d4p1:Active>\r\n        <d4p1:EmailAddress>robbie@robbietapping.com.au</d4p1:EmailAddress>\r\n        <d4p1:FirstName>Robbie</d4p1:FirstName>\r\n        <d4p1:IsAdministrator i:nil=\"true\" />\r\n        <d4p1:LastName>Tapping</d4p1:LastName>\r\n        <d4p1:Password>xxxxxxxxx</d4p1:Password>\r\n        <d4p1:UserID>2</d4p1:UserID>\r\n        <d4p1:UserName>robbietapping</d4p1:UserName>\r\n      </AuthenticateUserResult>\r\n    </AuthenticateUserR
esponse>\r\n  </s:Body>\r\n</s:Envelope>"

谁能告诉我这个问题是否存在,我将我的 VS2010 升级到 SP1,这已经开始显现了。

任何信息或答案将不胜感激。

谢谢

编辑

发生错误的代码是:

 public Anyboat.Admin.UserService.User EndAuthenticateUser(System.IAsyncResult result) {
                object[] _args = new object[0];
                Anyboat.Admin.UserService.User _result = ((Anyboat.Admin.UserService.User)(base.EndInvoke("AuthenticateUser", _args, result)));
                return _result;
            }

【问题讨论】:

    标签: silverlight wcf deserialization


    【解决方案1】:

    您在服务端使用哪种序列化类型?我很确定您使用了一些自定义序列化。 "\r\n" 问题很常见,当您尝试反序列化从服务接收到的格式不正确的 xml 时会发生此问题。
    所以我看到了两种方法:

    1. 最可取的方式。检查服务端的序列化程序。很明显,服务返回的 xml 格式错误。
    2. IClientMessageInspector 添加到客户端并在反序列化之前从响应中删除特殊符号。

    【讨论】:

    • 谢谢。这帮助了我,结果发现问题与通过 CustomBindingBehaviour 传输的对象有关,所以我改用了 basicHttpBinding 行为并修复了它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多