【问题标题】:WCF Post Shows Deserialization errorWCF Post 显示反序列化错误
【发布时间】:2016-01-06 10:58:44
【问题描述】:

我的移动应用程序正在使用 WCF 服务。当我尝试将一些参数传递给服务时,它会抛出一个错误,说

服务器在处理请求时遇到错误。异常消息是“格式化程序在尝试反序列化消息时抛出异常:反序列化操作“InspectVisit”的请求消息正文时出错。遇到了意想不到的字符'a'.'。有关更多详细信息,请参阅服务器日志。异常堆栈跟踪是:

在 System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(消息消息,对象 [] 参数) 在 System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(消息消息,对象 [] 参数) 在 System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(消息消息,对象 [] 参数) 在 System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(消息消息,对象 [] 参数) 在 System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin (MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

我的服务是

[ServiceContract]
public interface ISiteVisitService
{


    [OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest, UriTemplate = "InspectVisit/")]
    Output InspectVisit(InspectVisitInput inspectInfo);
}`

我的输入类是

[DataContract]
public class InspectVisitInput
{
    [DataMember]
    public string UserId { get; set; }

    [DataMember]
    public int VisitId { get; set; }

    [DataMember]
    public string Catogery { get; set; }

    [DataMember]
    public string InspectionDate { get; set; }

    [DataMember]
    public bool ServiceRequired { get; set; }
}

我的 json 字符串看起来像这样

{
"inspectInfo": {
            "UserId":"arun",
            "VisitId":39,
            "Catogery": “Walks”,
            “InspectionDate” : “10/06/2015 11:30:30”,
            “ServiceRequired": true
}

}

请大家帮帮我。

【问题讨论】:

  • 1) 你的 JSON 是什么样的? 2) 是否有InnerException 来显示格式化程序抛出的实际异常?
  • 客户端发生了什么?你是如何创建 WCF 的客户端对象的?您必须在客户端和服务器上使用相同的序列化程序设置。
  • 您是否查看了服务器日志以获取更多详细信息?它们可以在“应用程序和服务日志”->Microsoft->Windows->“应用程序服务器-应用程序”下的事件查看器中找到,无论是操作管理员。
  • @dbc 我已经添加了传递给服务的 json 字符串。
  • @MattC 我正在使用 Windows XP 来托管我的服务,但在服务器上找不到任何服务器日志。

标签: c# json wcf serialization deserialization


【解决方案1】:

您需要将 JSON 更改为 ANSI 格式。如果您将 JSON 复制并粘贴到 Notepad++ 并将 Encoding 更改为 ANSI,您将看到如下所示的 JSON:

{
  "inspectInfo": {
            "UserId":"arun",
            "VisitId":39,
            "Catogery": “Walksâ€,
            “InspectionDate†: “10/06/2015 11:30:30â€,
            “ServiceRequired": true
  }
}

正如您在 Category 值中看到的那样,异常显示的字符 â 相同。问题似乎在于数据周围的那些不同类型的引号"

如果您可以控制修改 JSON,那么您应该将其更改为 ANSI 和那些支持的特殊字符。或者确保在从客户端发送数据时提及 JSON 内容的 UTF-8 编码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    • 2014-03-03
    • 1970-01-01
    • 2020-03-16
    • 2012-03-10
    • 2015-03-29
    相关资源
    最近更新 更多