我的WCF服务和iOS客户端出现了一种奇怪的情况,我不确定到底发生了什么。问题是,如果我使用WCF POST方法在客户端中添加数据,然后调用GET从该表中获取所有数据,则POST中的所有数据都不会返回给客户端。为了进一步说明:
我还注意到,如果我从iPhone中删除该应用程序,然后使用Xcode将其添加回去,然后运行GET方法,则将返回Users表中的所有数据。我不确定数据是否缓存在某个地方或什么地方。我怀疑问题可能出在我的客户端代码如何运行GET上,但我不确定,这也可能是我的服务器代码。这是下面的代码。
// WCF服务代码
//IService.cs
[OperationContract]
[WebInvoke(Method ="GET", ResponseFormat= WebMessageFormat.Json, BodyStyle= WebMessageBodyStyle.WrappedResponse, UriTemplate = "/GetUsers")]
string GetMyUsers();
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat=WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/NewUser")]
string AddNewUser(NewUser newUser);
[DataContract]
public class NewUser
{
[DataMember(Name="name")]
public string name { get; set; }
[DataMember(Name="age")]
public string age { get; set; }
}
//Service1.svc.cs
public string AddNewUser(NewUser newUser)
{
var userController = new UserController();
userController.AddNewUser(newUser.name, newUser.age);
return "Method Call Complete";
}
public string GetMyUsers()
{
var userController = new UserController();
string returnString = userController.GetAllUsers();
return returnString;
}
//UserController.cs
public string GetAllUsers()
{
using(var db = new EmployeesEntities1())
{
string returnString = string.Empty;
List<User> userList = db.Users.ToList();
JavaScriptSerializer jss = new JavaScriptSerializer();
returnString = jss.Serialize(userList);
return returnString;
}
}
// IOS客户端GET
NSString *restCallString = [NSString stringWithFormat:@"http://myPublicIP/MyService/Service1.svc/GetUsers"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:restCallString]];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSError *error = nil;
NSURLResponse *response = nil;
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSDictionary *jsonArray=[NSJSONSerialization JSONObjectWithData:result options:0 error:nil];
NSString *item = [jsonArray objectForKey:@"GetMyUsersResult"];
txtData.text = item;
// IOS客户端POST
NSArray *propertyNames = [NSArray arrayWithObjects:@"name", @"age",nil];
NSArray *propertyValues = [NSArray arrayWithObjects:@"Mike", @"23",nil];
NSDictionary *properties = [NSDictionary dictionaryWithObjects: propertyValues forKeys:propertyNames];
NSMutableDictionary *newUserObject = [NSMutableDictionary dictionary];
[newUserObject setObject:properties forKey:@"newUser"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:newUserObject options:kNilOptions error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:@"http://myPublicIP/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];
}
我还测试了在本地主机上运行的/ GetUsers方法,该方法始终将最新的用户列表从“用户”表返回到Web浏览器。问题似乎出在服务和客户端之间。我是Web服务的新手,任何帮助或建议都将不胜感激。
我相信我找到了一个解决方案,它与请求的缓存策略有关:
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
将其添加到请求后,该请求将始终从数据库中获取最新数据。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句