WCF,POST JSONized数据

puf*_*pio 10 .net wcf jquery post json

我有一个复杂的类型:

[DataContract]
public class CustomClass
{
   [DataMember]
   public string Foo { get; set; }
   [DataMember]
   public int Bar { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后,我有一个WCF RESTful Web服务,其中包含:

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

所以在浏览器端我将我的CustomClass对象jsonized到它看起来像:

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

我使用$ .ajax提交数据w/jquery所以我可以手动将内容类型设置为"application/json",当它提交时,postbody看起来像

custom=<uri encoded version of myClass>
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

服务器遇到处理请求的错误.异常消息是'检查MyAssembly.CustomClass类型的对象的start元素时出错.遇到意想不到的字符'c'.'.请参阅服务器日志以获取更多详 的异常堆栈跟踪是:在在System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.ReadObject System.Runtime.Serialization.XmlObjectSerializer.IsStartObjectHandleExceptions(XmlReaderDelegator阅读器)在System.Runtime.Serialization.Json.DataContractJsonSerializer.IsStartObject(XmlDictionaryReader读取器)(消息信息) System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest上的System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(消息消息,Object []参数)中的System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.DeserializeRequest(消息消息,Object []参数)在System.ServiceModel.Dispatcher.DispatchOperationRuntime.Invok的System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc&rpc)处的System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(消息消息,Object []参数)处的消息,Object []参数)System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&rpc)中的System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&rpc)处的System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&rpc)处的eBegin(MessageRpc&rpc).位于System.ServiceModel.Dispatcher.MessageRpc.Process的System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&rpc)中的ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&rpc)(Boolean isOperationContextSet)

我已经尝试包装我的json'ized数据...我已经尝试使用$ .post发送消息(但是没有将contenttype设置为application/json所以webservice不理解)..任何想法?