简体   繁体   English

如何在xamarin.forms中使用WCF Web服务时解决targetinvokationexception?

[英]How to solve targetinvokationexception while consuming wcf webservices in xamarin.forms?

I'm consuming WCF soap web services in xamarin.forms. 我在xamarin.forms中使用WCF soap Web服务。 I've added the service reference from the visual studio which generated the asynchronous operations. 我添加了来自Visual Studio的服务参考,该参考生成了异步操作。 I've used the following code for consuming the web service 我使用以下代码来使用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");
                }
            }

When I test it on simulator, I just sit and wait for the reply and when I try to use a phone like an android phone then there is an error ie targetinvocationexception. 当我在模拟器上对其进行测试时,我只是坐着等待答复,而当我尝试使用像Android手机这样的手机时,就会出现错误,即targetinvocationexception。 What should I do to resolve the issue? 我应该怎么做才能解决这个问题?

That is not a good implementation for several reasons. 由于多种原因,这不是一个好的实现。 First, you are using async void to handle the async completion event which will silently ignore any exceptions raised. 首先,您正在使用async void处理异步完成事件,该事件将静默忽略引发的任何异常。 Second, the Async/Completed pattern is not appropriate for single-shot async calls. 其次,异步/完成模式不适用于单次异步调用。 Third, the code that results from Async/Completed pattern is just really messy for most situations (including this one). 第三,对于大多数情况(包括此情况),由异步/完成模式产生的代码确实很混乱。

What you should be using instead is the Task.Factory.FromAsync<>() helper, which will greatly simplify your code and resolve those problems. 相反,您应该使用Task.Factory.FromAsync<>()帮助器,它将大大简化您的代码并解决这些问题。 It would look something like this for you: 对于您来说,它看起来像这样:

<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");
    }
}

Note that I'm using async void here, and you might think that I was contradicting myself by doing so. 请注意,我在这里使用async void ,您可能会认为这样做与我自己矛盾。 It is OK to use async void for the event handler of a control when you manually trap exceptions within that handler (as I have done in the example code). 当您在该处理程序中手动捕获异常时,可以将async void用于控件的事件处理程序(就像我在示例代码中所做的那样)。

I think, there is a bug in the Xamarin.Forms right now which is creating the problem. 我认为Xamarin.Forms中存在一个错误,这正在引起问题。 I've removed the service reference and now consuming the service manually. 我删除了服务参考,现在手动使用该服务。 I found the clue from the below link and using the same way to consume the web service. 我从下面的链接中找到了线索,并使用相同的方式来使用Web服务。

https://stackoverflow.com/questions/14336414/httpclient-soap-c/20108971#20108971 https://stackoverflow.com/questions/14336414/httpclient-soap-c/20108971#20108971

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM