WCF,POST JSON化的数据

16 浏览
0 Comments

WCF,POST JSON化的数据

我有一个复杂的类型:

[DataContract]
public class CustomClass
{
   [DataMember]
   public string Foo { get; set; }
   [DataMember]
   public int Bar { get; set; }
}

然后我有一个WCF RESTful webservice包含以下内容:

[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/class/save")]
bool Save(CustomClass custom);

在浏览器端,我将我的CustomClass对象转换成json格式,如下:

var myClass = "{ foo: \"hello\", bar: 2 }";
$.ajax({
    contentType: "application/json",
    data: { custom: myClass },
    dataType: "json",
    success: callback,
    type: "POST",
    url: "MyService.svc/class/save"
});

我使用$.ajax提交数据,以便手动将内容类型设置为"application/json",当提交时,postbody的形式如下:

custom=<经过uri编码的myClass>

我收到以下错误:

服务器在处理请求时发生错误。异常消息为'在类型MyAssembly.CustomClass的对象的开始元素检查时出错。遇到意外字符'c'。'。有关更多详细信息,请参阅服务器日志。异常堆栈跟踪为:

在System.Runtime.Serialization.XmlObjectSerializer.IsStartObjectHandleExceptions(XmlReaderDelegator reader)中

在System.Runtime.Serialization.Json.DataContractJsonSerializer.IsStartObject(XmlDictionaryReader reader)中

在System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.ReadObject(Message message)中

在System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.DeserializeRequest(Message message, Object[] parameters)中

在System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters)中

在System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)中

在System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)中

在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.ProcessMessage4(MessageRpc& rpc)中

在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)中

在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)中

在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)中

在System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)中

我尝试包装我的json化数据...我尝试使用$.post发送消息(但这样不会设置contenttype为application/json,因此webservice无法理解)...有任何想法吗?

0
0 Comments

问题的原因是WCF要求在JSON中的属性名字要用""包裹起来,而你遇到的问题是一个序列化错误。当你使用data:'{id: 1 }',时,出现了错误,但是当你使用data:'{"id": 1 }',时,问题得到了解决。希望这能帮到其他人。

0
0 Comments

问题的原因是,在使用jQuery的post方法构建复杂的Json对象时,没有正确转义包装器。为了解决这个问题,可以选择以下两种方法之一:

1. 转义整个JS对象,例如:"{ \"custom\": \"{ foo: \"hello\", bar: 2 }\"}";

2. 更好的解决方法是使用JSON.stringify({ custom: myClass })。

WCF对于接收到的JSON对象进行序列化非常敏感。

0
0 Comments

WCF是一种用于构建分布式应用程序的微软技术。在使用WCF时,有时候我们需要将JSON格式的数据发送到WCF服务中。然而,有些开发者在使用WCF时遇到了一个问题,即无法使用POST方法将JSON化的数据发送到WCF服务。

这个问题的出现的原因是WCF默认只支持通过SOAP消息来传输数据。因此,当我们尝试使用POST方法发送JSON化的数据时,WCF服务无法正确地处理这些数据。

然而,我们可以通过以下解决方法来解决这个问题。首先,我们需要在WCF服务契约中声明一个方法,该方法接受一个字符串参数,用于接收JSON化的数据。然后,我们需要在Web.config文件中配置WCF服务,以便它能够正确地处理JSON化的数据。具体来说,我们需要在Web.config文件中的节点下添加一个节点,并将它的属性automaticFormatSelectionEnabled设置为true。最后,我们需要在WCF服务实现类中实现刚才声明的方法,以便它能够正确地处理接收到的JSON化的数据。

下面是一个示例代码,演示了如何在WCF服务中处理POST方法发送的JSON化的数据:

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "GetData", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    string GetData(string jsonData);
}
public class MyService : IMyService
{
    public string GetData(string jsonData)
    {
        // 处理接收到的JSON化的数据
        // ...
        return "Success";
    }
}

通过上述解决方法,我们可以成功地在WCF服务中使用POST方法发送JSON化的数据。这样,我们就能够更方便地在分布式应用程序中传输和处理JSON格式的数据了。

0