zic10
zic10

Reputation: 2330

WCF REST Service POST Error with iOS Client

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

Upvotes: 0

Views: 676

Answers (1)

Manish Parakhiya
Manish Parakhiya

Reputation: 3798

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.

Upvotes: 1

Related Questions