WCF帖子显示反序列化错误

时间:2015-10-08 17:23:33

标签: c# json wcf serialization deserialization

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

  

服务器在处理请求时遇到错误。异常消息是'格式化程序在尝试反序列化消息时引发异常:反序列化操作'InspectVisit'的请求消息体时出错。遇到了意外的角色'â'。'。请参阅服务器日志以获取更多详异常堆栈跟踪是:

        

at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message,Object [] parameters)      在System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(消息消息,Object []参数)      在System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(消息消息,Object []参数)      在System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(消息消息,Object []参数)      在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 个答案:

答案 0 :(得分: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编码。