2011-10-19 2 views
2

난 그냥 현재 시간 디버깅 몇 시간에 POSTing JSON to WCF REST EndpointGeneric WCF JSON Deserialization 같은 질문을 통해 찾고 있지만, 한 내가 설정 한WCF - JSON을 끝점에 게시 - 요청 콘텐츠 본문은 어떻게 표시되어야합니까?

... 내 코드 및/또는 디버깅은 매우 기본적인 수준에서 실패라고 생각합니다 WCF 서비스와 같은 :

[ServiceContract] 
public interface IAutomationService 
{ 
    [OperationContract] 
    [ServiceKnownType("GetKnownTypes", typeof(KnownTypeProvider))] 
    CommandBase GetNextCommand(int timeoutInMilliseconds); 
} 

내가 지금이 서비스를 SOAP 및 JSON 엔드 포인트에 성공적으로 설치있어 : IAutomationService가

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, IncludeExceptionDetailInFaults = true)] 
public class AutomationService : IAutomationService 
{ 
    [WebInvoke(Method = "POST", UriTemplate = "getNextCommand")] 
    public CommandBase GetNextCommand(int timeoutInMilliseconds) 
    { 
     // stuff 
    } 
} 

.

그러나 ... 필자가 ContentBody에서 전달한 변수를 사용하여 서비스를 호출하는 방법을 알아낼 수 없습니다.

예를 들어 Uri에서 POST로 서비스를 호출 할 수 있습니다.

그러나 콘텐츠를 본문에 넣으려고하면 예외가 발생합니다. 예 :

서버가 요청을 처리하는 중에 오류가 발생

POST http://localhost:8085/phoneAutomation/jsonAutomate/getNextCommand 
Host: localhost:8085 
Connection: keep-alive 
Cache-Control: max-age=0 
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1 
Accept: application/json,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 
Cookie: Orchrd-=%7B%22Exp-N42-Settings%22%3A%22open%22%2C%22Exp-N42-New%22%3A%22open%22%7D 
Content-Length: 31 
Content-Type: application/json 

{"timeoutInMilliseconds":10000} 

는 실패. 예외 메시지가 ' System.Int32 유형의 개체를 deserialize하는 동안 오류가 발생했습니다. ''값을 'Int32'유형으로 구문 분석 할 수 없습니다. '. 자세한 내용은 서버 로그를 참조하십시오. 예외 스택 트레이스이다 System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject (XmlDictionaryReader 리더에서 System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions (XmlReaderDelegator 리더 부울 verifyObjectName, DataContractResolver dataContractResolver)에서

내가 잘못 (다른 WCF를 사용하는 것보다 뭘하는지 어떤 아이디어를 가지고

누구나

에서, 부울 verifyObjectName) ...!) - 나는 J 해요 JSON { "timeoutInMilliseconds": 10000}의 모양이 확실하지 않습니다.

답변

1

기본적으로 WCF REST 서비스의 "본문 스타일"은 "Bare"입니다. 즉, 단일 입력이있는 작업의 경우 작업의 값이 모든 개체를 래핑하지 않고 "있는 그대로"가야 함을 의미합니다. 즉이 작동합니다 귀하의 경우 의미

POST http://localhost:8085/phoneAutomation/jsonAutomate/getNextCommand 
Host: localhost:8085 
Connection: keep-alive 
Cache-Control: max-age=0 
... 
Content-Length: 5 
Content-Type: application/json 

10000 

한 가지 더, 직접 질문에 관련되지 않은 : 당신이 인터페이스에서 서비스 계약을 정의하는 경우, 당신은 또한 (모든 "계약 관련"속성을 추가한다 (예 : WebInvoke)도 인터페이스에 있습니다.

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, IncludeExceptionDetailInFaults = true)] 
public class AutomationService : IAutomationService 
{ 
    public CommandBase GetNextCommand(int timeoutInMilliseconds) 
    { 
     // stuff 
    } 
} 

[ServiceContract] 
public interface IAutomationService 
{ 
    [OperationContract] 
    [ServiceKnownType("GetKnownTypes", typeof(KnownTypeProvider))] 
    [WebInvoke(Method = "POST", UriTemplate = "getNextCommand")] 
    CommandBase GetNextCommand(int timeoutInMilliseconds); 
} 

그리고 다른 정보 : 즉,이 같은 코드 모양을 만들 것 같은 방법으로 요청을 전송하기를 원한다면 원래 ({"tiemoutInMilliseconds":10000})을 가지고, 당신은 [WebInvoke] 속성에 BodyStyle 속성을 설정할 수 있습니다 포장에 (또는 WrappedRequest) :

[ServiceContract] 
public interface IAutomationService 
{ 
    [OperationContract] 
    [ServiceKnownType("GetKnownTypes", typeof(KnownTypeProvider))] 
    [WebInvoke(Method = "POST", 
       UriTemplate = "getNextCommand", 
       BodyStyle = WebMessageBodyStyle.WrappedRequest)] 
    CommandBase GetNextCommand(int timeoutInMilliseconds); 
} 
+1

나는 당신을 사랑한다고 생각합니다. 도와 주셔서 감사합니다! – Stuart

+0

지금 테스트하고 테스트했습니다 ... 당신이 말하는 모든 것은 ... 이제 당신을 사랑합니다. 고맙습니다. 고맙습니다. 고맙습니다. 또한 내 계약을 정리하고 정리할 것입니다. – Stuart

관련 문제