在xamarin.forms中使用wcf webservices时如何解决targetinvokationexception?
How to solve targetinvokationexception while consuming wcf webservices in xamarin.forms?
我在 xamarin.forms 中使用 WCF soap Web 服务。我已经从生成异步操作的 visual studio 添加了服务引用。我使用以下代码来使用 Web 服务
Service1Client dataCommunicator = new Service1Client();
dataCommunicator.GiveFeedbackCompleted += new EventHandler<GiveFeedbackCompletedEventArgs>(GiveFeedbackCallback);
dataCommunicator.GiveFeedbackAsync(editPhoneF.Text, monuments[pickerMonument.SelectedIndex], editRemarks.Text, imei);
}
private async void GiveFeedbackCallback(object sender, GiveFeedbackCompletedEventArgs e)
{
if (e.Result)
{
await DisplayAlert("Success", "Thank you for your valuable comments", "Ok");
}
else
{
await DisplayAlert("Oops!!", "Internal server error, please try again later", "Ok");
}
}
当我在模拟器上测试它时,我只是坐着等待回复,当我尝试像 android phone 一样使用 phone 时出现错误,即目标调用异常。
我应该怎么做才能解决这个问题?
我想,现在 Xamarin.Forms 中有一个错误导致了问题。我已经删除了服务引用,现在手动使用该服务。我从下面找到了线索 link 并使用相同的方式来使用网络服务。
由于多种原因,这不是一个好的实施。首先,您正在使用 async void
来处理异步完成事件,该事件将默默地忽略引发的任何异常。其次,Async/Completed 模式不适合单次异步调用。第三,Async/Completed 模式产生的代码在大多数情况下(包括这个)真的很乱。
您应该改用 Task.Factory.FromAsync<>()
助手,它将大大简化您的代码并解决这些问题。它看起来像这样:
<Button Click="Button_Click" Text="Click Me"/>
...
async void Button_Click(object sender, EventArgs eventArgs) {
Service1Client dataCommunicator = new Service1Client();
try {
bool result =
await Task.Factory.FromAsync<string, Monument, string, IMEI, bool>(
dataCommunicator.BeginGiveFeedback,
dataCommunicator.EndGiveFeedback,
editPhoneF.Text,
monuments[pickerMonument.SelectedIndex],
editRemarks.Text,
imei);
if (e.Result) {
await DisplayAlert("Success", "Thank you for your valuable comments", "Ok");
} else {
await DisplayAlert("Internal server error", "Please try again later.", "Ok");
}
} catch (Exception ex) {
await DisplayAlert("Server communication error", "Please try again later. ERROR: " + ex.GetType().Name, "Ok");
}
}
请注意,我在这里使用 async void
,您可能会认为我这样做是自相矛盾。当您在该处理程序中手动捕获异常时,可以使用 async void
作为控件的事件处理程序(正如我在示例代码中所做的那样)。
我在 xamarin.forms 中使用 WCF soap Web 服务。我已经从生成异步操作的 visual studio 添加了服务引用。我使用以下代码来使用 Web 服务
Service1Client dataCommunicator = new Service1Client();
dataCommunicator.GiveFeedbackCompleted += new EventHandler<GiveFeedbackCompletedEventArgs>(GiveFeedbackCallback);
dataCommunicator.GiveFeedbackAsync(editPhoneF.Text, monuments[pickerMonument.SelectedIndex], editRemarks.Text, imei);
}
private async void GiveFeedbackCallback(object sender, GiveFeedbackCompletedEventArgs e)
{
if (e.Result)
{
await DisplayAlert("Success", "Thank you for your valuable comments", "Ok");
}
else
{
await DisplayAlert("Oops!!", "Internal server error, please try again later", "Ok");
}
}
当我在模拟器上测试它时,我只是坐着等待回复,当我尝试像 android phone 一样使用 phone 时出现错误,即目标调用异常。 我应该怎么做才能解决这个问题?
我想,现在 Xamarin.Forms 中有一个错误导致了问题。我已经删除了服务引用,现在手动使用该服务。我从下面找到了线索 link 并使用相同的方式来使用网络服务。
由于多种原因,这不是一个好的实施。首先,您正在使用 async void
来处理异步完成事件,该事件将默默地忽略引发的任何异常。其次,Async/Completed 模式不适合单次异步调用。第三,Async/Completed 模式产生的代码在大多数情况下(包括这个)真的很乱。
您应该改用 Task.Factory.FromAsync<>()
助手,它将大大简化您的代码并解决这些问题。它看起来像这样:
<Button Click="Button_Click" Text="Click Me"/>
...
async void Button_Click(object sender, EventArgs eventArgs) {
Service1Client dataCommunicator = new Service1Client();
try {
bool result =
await Task.Factory.FromAsync<string, Monument, string, IMEI, bool>(
dataCommunicator.BeginGiveFeedback,
dataCommunicator.EndGiveFeedback,
editPhoneF.Text,
monuments[pickerMonument.SelectedIndex],
editRemarks.Text,
imei);
if (e.Result) {
await DisplayAlert("Success", "Thank you for your valuable comments", "Ok");
} else {
await DisplayAlert("Internal server error", "Please try again later.", "Ok");
}
} catch (Exception ex) {
await DisplayAlert("Server communication error", "Please try again later. ERROR: " + ex.GetType().Name, "Ok");
}
}
请注意,我在这里使用 async void
,您可能会认为我这样做是自相矛盾。当您在该处理程序中手动捕获异常时,可以使用 async void
作为控件的事件处理程序(正如我在示例代码中所做的那样)。