0

I've published WCF Service to IIS and can successfully get all the data back with a GET method. I'm not trying to POST data to the service and save this data in the database. The exact error I'm getting is:

The server encountered an error processing the request. The exception message is 'Object reference not set to an instance of an object.'. See server logs for more details.

This error message is always returned in the REST response that comes back to the client after sending the request.

I've gone into the inetpub->wwwroot->logs folder and looked around but did not see anything in the log files that helped me pinpoint the problem. I believe the problem is either how I've structured the server to accept responses or how I'm sending the responses out from the client. I'm new to REST so I'm not quite sure what I'm doing wrong. Below is the code. Any help would be appreciated.

//Server code:

//IService.cs

    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/NewUser")]
    void AddNewUser(NewUser newUser);


[DataContract]
public class NewUser
{
    [DataMember]
    public string name { get; set; }



    [DataMember]
    public string age { get; set; }
}

//Service1.svc.cs

public  void AddNewUser(NewUser newUser)
{
    var userController = new UserController();

    userController.AddNewUser(newUser.name, newUser.age);
}

//Client Side Code on iPhone

 NSArray *propertyNames = [NSArray arrayWithObjects:@"name", @"age",nil];
    NSArray *propertyValues = [NSArray arrayWithObjects:@"Test", @"100",nil];

    NSDictionary *properties = [NSDictionary dictionaryWithObjects: propertyValues forKeys:propertyNames];

    NSMutableDictionary *newUserObject = [NSMutableDictionary dictionary];

    [newUserObject setObject:properties forKey:@"newusermodel"];

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:newUserObject options:kNilOptions error:nil];

    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

       NSString *urlString = [NSString stringWithFormat:@"http://myPublicIPAddress/MyService/Service1.svc/NewUser"];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    [request setValue:jsonString forHTTPHeaderField:@"json"];

    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:jsonData];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    NSError *error = nil;
    NSURLResponse *theResponse = [[NSURLResponse alloc]init];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&error];

    if(error)
    {
        txtData.text = [error localizedDescription];
    }

    else
    {
        NSMutableString *retVal = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    }

1 Answer 1

1

Try to Remove BodyStyle = WebMessageBodyStyle.WrappedRequest from WebInvoke attribute and add RequestFormat=WebMessageFormat.Jsoninstead like this.

  [OperationContract]
  [WebInvoke(Method = "POST",RequestFormat=WebMessageFormat.Json, ResponseFormat =        WebMessageFormat.Json, UriTemplate = "/NewUser")]
  void AddNewUser(NewUser newUser);

i had test it and this is working for me.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you manish, this did solve the problem I was getting, but now the issue I'm running into is the data reaching the server is null. More specifically, the newUser model in the AddNewUser(NewUser newUser) method has null attributes for both name and age. Any idea what I might be doing wrong on that?
Nevermind, I figured it out. I added back in BodyStyle = WebMessageBodyStyle.WrappedRequest. Once I did this, the data was being received by the service and added into the database successfully. No parameters were null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.