2009-07-14 29 views
10

Tôi có một loại phức tạp:WCF, đăng JSONized dữ liệu

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

sau đó tôi có một webservice RESTful WCF có này trong đó:

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

vân vân phía trình duyệt tôi jsonized CustomClass của tôi đối tượng có vẻ như:

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

Tôi gửi dữ liệu bằng cách sử dụng $ .ajax để tôi có thể đặt loại nội dung thành "ứng dụng ation/json" và khi nó trình, các postbody trông giống như

custom=<uri encoded version of myClass> 

tôi nhận được lỗi sau:

Máy chủ gặp lỗi khi xử lý yêu cầu. Thông báo ngoại lệ là 'Có là lỗi kiểm tra phần tử bắt đầu của đối tượng thuộc loại MyAssembly.CustomClass. Đã gặp phải ký tự không mong muốn 'c'. '. Xem nhật ký máy chủ để biết thêm chi tiết. Trường hợp ngoại lệ stack trace là: tại System.Runtime.Serialization.XmlObjectSerializer.IsStartObjectHandleExceptions (đọc XmlReaderDelegator) tại System.Runtime.Serialization.Json.DataContractJsonSerializer.IsStartObject (XmlDictionaryReader reader) tại System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter. readObject (nhắn tin) tại System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.DeserializeRequest (tin nhắn , Object [] thông số) tại System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest (Message nhắn, Object [] thông số) tại hệ thống .ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequ est (Tin nhắn , Object [] thông số) tại System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest (nhắn tin, Object [] thông số) tại System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs (MessageRpc & rpc) tại System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin (MessageRpc & rpc) tại System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5 (MessageRpc & rpc) tại System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4 (MessageRpc & rpc) tại hệ thống. ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3 (MessageRpc & rpc) tại System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2 (MessageRpc & rpc) tại System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1 (MessageRpc & rpc) tại System.ServiceModel.Dispatcher.MessageRpc.Process (Boolean isOperationContextSet)

Tôi đã thử gói dữ liệu json'ized của mình ... tôi đã thử sử dụng $ .post để gửi thư (nhưng điều đó không đặt contenttype thành ứng dụng/json để webservice không hiểu) .. bất kỳ ý tưởng?

Trả lời

3

Vì vậy, các vấn đề mà bạn đang gặp là một lỗi serialization.WCF muốn nhìn thấy tên của các thuộc tính trong JSON được bao bọc bởi ""

Vì vậy, tôi chỉ chạy vào các lỗi tương tự nơi

data:'{id: 1 }', 

đã không làm việc nhưng

data:'{"id": 1 }', 

làm việc

Tôi hy vọng điều này sẽ giúp người khác ra khỏi

3

Sự cố bạn đã thoát khỏi ob ject một cách chính xác, nhưng khi bạn đang xây dựng đối tượng Json phức tạp trong phương thức jQuery post bạn không thoát khỏi wrapper. Vì vậy, bạn cần phải thoát toàn bộ đối tượng JS như sau: "{\" tùy chỉnh \ ": \" {foo: \ "hello \", thanh: 2} \ "}" (Tôi thực sự không tự mình thử , nhưng nên làm việc), hay (có thể là giải pháp tốt hơn) sử dụng JSON.stringify ({tùy chỉnh: myClass})

WCF là thực sự nhạy cảm đối với các đối tượng JSON mà nó nhận được cho serialization.

Các vấn đề liên quan