【问题标题】:WCF REST Service POST Error with iOS ClientiOS 客户端的 WCF REST 服务 POST 错误
【发布时间】:2014-07-28 10:24:44
【问题描述】:

我已将 WCF 服务发布到 IIS,并且可以使用 GET 方法成功取回所有数据。我没有尝试将数据发布到服务并将这些数据保存在数据库中。我得到的确切错误是:

服务器在处理请求时遇到错误。异常消息是“对象引用未设置为对象的实例。”。有关详细信息,请参阅服务器日志。

此错误消息始终在发送请求后返回给客户端的 REST 响应中返回。

我进入了 inetpub->wwwroot->logs 文件夹并环顾四周,但在日志文件中没有看到任何可以帮助我查明问题的内容。我相信问题要么是我如何构建服务器以接受响应,要么是我如何从客户端发送响应。我是 REST 新手,所以我不太确定自己做错了什么。下面是代码。任何帮助将不胜感激。

//服务器代码:

//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);
}

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

【问题讨论】:

    标签: c# ios wcf rest


    【解决方案1】:

    尝试从 WebInvoke 属性中删除 BodyStyle = WebMessageBodyStyle.WrappedRequest 并添加 RequestFormat=WebMessageFormat.Json 而不是这样。

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

    我已经测试过了,这对我有用。

    【讨论】:

    • 谢谢你,这确实解决了我遇到的问题,但现在我遇到的问题是到达服务器的数据为空。更具体地说,AddNewUser(NewUser newUser) 方法中的 newUser 模型对于 name 和 age 都具有 null 属性。知道我可能做错了什么吗?
    • 没关系,我想通了。我添加回 BodyStyle = WebMessageBodyStyle.WrappedRequest。一旦我这样做了,数据就会被服务接收并成功添加到数据库中。没有参数为空。
    猜你喜欢
    • 2013-01-19
    • 2012-05-20
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-08
    相关资源
    最近更新 更多