1

I am working on WCF application, I am invoking this operation

[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); 

}

I am getting error

"Object Reference not set to an instance"

here

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

Here is the JSON Class

[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; }
}

Here is JSON request via POST method

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

Here is response screen shot enter image description here

Any Help !

2
  • userInfo is null. Where are you getting that value from? Commented May 15, 2013 at 15:09
  • I have edit it , Kindly check it. Commented May 15, 2013 at 15:24

2 Answers 2

4

The BodyStyle property of your [WebInvoke] attribute specifies the style to be WrappedRequest - which means that the object which you want passed as the input must be wrapped in an object, whose member name is the same as the property name, as Steve Wilkes mentioned.

Another alternative would be to change the BodyStyle to Bare; in this case your input should work just fine. In other words, if this is your operation declaration, the input you have in your question should work.

[OperationContract(Name = "UserAuthentication")]
[WebInvoke(Method = "POST",
           ResponseFormat = WebMessageFormat.Json,
           BodyStyle = WebMessageBodyStyle.Bare,
           UriTemplate = "/UserAuthentication?username={username}")]
string UserAuthentication(string username, UserData userInfo); 
Sign up to request clarification or add additional context in comments.

Comments

1

I have not tested this, but shouldn't you be posting:

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

...?

3 Comments

He is getting the username from request url. Check UriTemplate. So, only {userInfo:{...}} of your solution should do it.
Still having same issue. It is giving me bad request Error , which is due to "Object Reference not set to an instance"
My bad on the username - thanks for pointing it out - I was too lazy to scroll :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.