Windows phone 8 检测何时发生web服务发布
我刚刚编写了一个简单的Windows8表单,可以发布到web服务api。它很好用。但我的挑战是能够确定手术后的成功与失败。我不知道如何返回值,因为aysnc任务不允许返回类型Windows phone 8 检测何时发生web服务发布,windows-phone-8,Windows Phone 8,我刚刚编写了一个简单的Windows8表单,可以发布到web服务api。它很好用。但我的挑战是能够确定手术后的成功与失败。我不知道如何返回值,因为aysnc任务不允许返回类型 //This class does the post to web service public class B2cMobileuserService : IB2cMobileuserService { private string RegisterUserUrl = RestfulUrl.Re
//This class does the post to web service
public class B2cMobileuserService : IB2cMobileuserService
{
private string RegisterUserUrl = RestfulUrl.RegisterMobileUser;
private readonly HttpClient _client = new HttpClient();
public async Task RegisterMobileUser(B2cMobileuserView user)
{
var jsonString = Serialize(user);
var content = new StringContent(jsonString, Encoding.UTF8, "application/json");
var result = await _client.PostAsync(RegisterUserUrl, content);
}
}
//This class calls the one above
public class WebserviceProcessor
{
//declaring all the service objects that would be used
IB2cMobileuserService mobileuserService = null;
public WebserviceProcessor() {
mobileuserService = new B2cMobileuserService();
}
//This method is going to post values to the web serever
public async void RegisterUser(B2cMobileuserView mobileuser) {
mobileuserService.RegisterMobileUser(mobileuser);
}
}
//Then the code below is from my .xaml user interface that calls the class that sends to webservice
private void Button_Click(object sender, RoutedEventArgs e)
{
B2cMobileuserView user = new B2cMobileuserView();
user.Name = name.Text;
user.Email = email.Text;
user.PhoneType = "Windows Mobile";
user.BrowserType = "None";
user.CountryName = "Nigeria";
user.UserPhoneID = phone.Text;
Serviceprocessor.RegisterUser(user);
progressBar.Visibility = Visibility.Visible;
}
Please I dont know how to return a value cos when I try I get the error that says async method must be void.
I need to set a way to know when the post was a success based on the return value from the web service.
要确保POST成功,请调用HttpResponseMessage.EnsureResessAccessStatusCode:
public async Task RegisterMobileUser(B2cMobileuserView user)
{
var jsonString = Serialize(user);
var content = new StringContent(jsonString, Encoding.UTF8, "application/json");
var result = await _client.PostAsync(RegisterUserUrl, content);
result.EnsureSuccessStatusCode();
}
如果要返回值,请使用任务
返回类型,而不是任务
另一方面,要避免异步无效;使用async Task
而不是async void
,除非编译器强制您编写async void
:
//This method is going to post values to the web serever
public Task RegisterUser(B2cMobileuserView mobileuser) {
return mobileuserService.RegisterMobileUser(mobileuser);
}
另外,您应该将异步方法命名为*Async
:
//This method is going to post values to the web serever
public Task RegisterUserAsync(B2cMobileuserView mobileuser) {
return mobileuserService.RegisterMobileUserAsync(mobileuser);
}
您可能会发现我的帮助很大。请在这里帮助我。我是这样返回任务的。或者你能给我一个示例方法吗。返回并测试您的响应。我真的很感激。只需要一个样品,你想要什么就给什么。我认为解析出服务器响应并返回比
HttpResponseMessage
更有意义的内容是最有意义的。我读了这篇文章。我现在的挑战是无法在按钮事件处理程序中调用该方法。它说等待运算符只能在asyc方法中使用。事件处理程序可以是async void
。