WCF Post 显示反序列化错误

WCF Post Shows Deserialization error

我有一个与我的移动应用程序一起使用的 WCF 服务。当我尝试将一些参数传递给服务时,它会抛出一条错误消息

The server encountered an error processing the request. The exception message is 'The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'InspectVisit'. Encountered unexpected character 'â'.'. See server logs for more details. The exception stack trace is:

at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.CompositeDispatchFormatter.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.ProcessMessage41(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) at 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);
}`

我的输入class是

[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
}

}

任何人都请帮助我。

您需要将 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 编码。