WCF GET方法无法获取iOS客户端的最新数据
本文关键字:客户端 最新 数据 iOS 获取 GET 方法 WCF | 更新日期: 2023-09-27 18:07:11
我的WCF服务和iOS客户端遇到了一个奇怪的情况,我真的不知道发生了什么。问题是,如果我在客户端中使用WCF POST方法添加数据,然后调用GET从该表中获取所有数据,则POST中的所有数据都不会返回给客户端。进一步说明:
- 假设表Users有一个用户John
- 我调用GET方法从表中获取所有用户。
- John出现在我的iOS客户端。
- 现在我从我的iOS客户端POST,添加用户Mike
- Mike出现在数据库中,POST操作成功。
- 我再次为所有用户调用GET方法,但这次Mike没有出现在
我还注意到,如果我从iPhone上删除应用程序,然后用Xcode将其添加回来,然后运行GET方法,从Users表返回所有数据。我不确定数据是否被缓存在某个地方或什么。我怀疑问题可能是在我的客户端代码如何运行GET,但我不完全确定,它可能是我的服务器代码。下面是我的代码。
//服务代码
//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方法,该方法总是从users表返回最新的用户列表到web浏览器。问题似乎在服务和客户端之间的某个地方。我是新的网络服务,任何帮助或建议将不胜感激。
我相信我找到了一个解决方案,它与请求的缓存策略有关:
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
一旦这个被添加到请求中,请求总是从数据库中获取最新的数据。