2013-05-15 6 views
1

내가 WCF 응용 프로그램 일하고, 여기JSON 구문 분석 오류 WCF

"Object Reference not set to an instance" 

여기

public string UserAuthentication(string username, UserData userInfo) 
{ 
    string outputData = string.Empty; 
    return userInfo.ToString(); // << Error at this line 
} 

는 JSON 클래스이며이 작업을

[ServiceContract] 
public interface IAuditDataService 
{ 

    [OperationContract(Name = "UserAuthentication")] 
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest, UriTemplate = "/UserAuthentication?username={username}")] 
    string UserAuthentication(string username, UserData userInfo); 

} 

나는 점점 오전 오류를 호출하고

[DataContract] 
[Serializable()] 
public class UserData 
{ 
    [DataMember(Name = "UserName", Order = 1)] 
    public string UserName { get; set; } 

    [DataMember(Name = "Password", Order = 2)] 
    public string Password { get; set; } 

    [DataMember(Name = "Token", Order = 3)] 
    public string Token { get; set; } 
} 
,

여기

다음
{"UserName":"abcd", 
"Password":"1234", 
"Token":"1234"} 

응답 화면이 enter image description here

도움으로 찍은 사진입니다 POST 방법을 통해 JSON 요청입니다!

+0

userInfo가 null입니다. 그 가치는 어디서 얻었습니까? –

+0

나는 그것을 편집했다 친절하게 그것을 검사하십시오. – Ahmed

답변

4

[WebInvoke] 속성의 BodyStyle 속성 WrappedRequest로 스타일을 지정합니다 - 당신이 해야 입력으로 전달하려는 오브젝트가 그 멤버 이름 속성 이름과 동일 객체에을 감싸 즉, Steve Wilkes가 언급했듯이.

또 다른 방법은 BodyStyleBare으로 변경하는 것입니다. 이 경우 귀하의 의견은 잘 작동합니다. 즉, 이것이 귀하의 운영 선언이라면 귀하가 질문에 입력 한 내용이 효과가 있습니다.

[OperationContract(Name = "UserAuthentication")] 
[WebInvoke(Method = "POST", 
      ResponseFormat = WebMessageFormat.Json, 
      BodyStyle = WebMessageBodyStyle.Bare, 
      UriTemplate = "/UserAuthentication?username={username}")] 
string UserAuthentication(string username, UserData userInfo); 
+0

위대한 당신은 수퍼맨입니다. . 감사합니다. – Ahmed

1

나는이 테스트를하지 않은,하지만 당신은 게시해서는 안 :

{ 
    username: "abcd" 
    userInfo: { 
     "UserName": "abcd", 
     "Password": "1234", 
     "Token": "1234" 
    } 
} 

...?

+3

그는 요청 URL에서'username'을 얻고 있습니다. 'UriTemplate'을 확인하십시오. 따라서 솔루션의'{userInfo : {...}}'만이 그렇게해야합니다. –

+0

여전히 동일한 문제가 있습니다. 그것은 "객체 참조가 인스턴스로 설정되지 않았기 때문에"오류가 발생했습니다. – Ahmed

+0

'사용자 이름'이 나쁘다 - 그것을 지적 해 주셔서 감사합니다. - 스크롤하기에는 너무 게으름을 받았습니다. –