简体   繁体   中英

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

I'm consuming WCF soap web services in xamarin.forms. I've added the service reference from the visual studio which generated the asynchronous operations. I've used the following code for consuming the web service

    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. 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. 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. 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. 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).

I think, there is a bug in the Xamarin.Forms right now which is creating the problem. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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