c# - iOS 客户端的 WCF REST 服务 POST 错误

标签 c# ios wcf rest

我已经将 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];
    }

最佳答案

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

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

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

关于c# - iOS 客户端的 WCF REST 服务 POST 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24093858/

相关文章:

c# - 在 C# 中找出我的 SQL Server 实例是否支持快照?

c# - Xamarin Forms 从 sqlite 数据库中删除

c# - 如何使用 XAudio2 重复播放相同的声音?

ios - 给定一个网页 url,我如何获取它的内容数据以生成链接预览,就像在 Facebook 和 Slack 中一样?

html - iOS 版 Chrome 和 Safari 上的背景图片

c# - 字符串查找和替换

ios - Collection View 单元格边框颜色在 Swift 3 中取消选择

c# - 使用 c# 代码在 Header 中使用 Assertion Saml2 创建 SOAP 消息

wcf - 为什么 DataContractSerializer 不支持属性?

c# - 通过 Web 服务调用启动长时间运行的作业