【问题标题】:How to solve targetinvokationexception while consuming wcf webservices in xamarin.forms?在 xamarin.forms 中使用 wcf webservices 时如何解决 targetinvokationexception?
【发布时间】:2023-03-16 15:41:02
【问题描述】:

我正在使用 xamarin.forms 中的 WCF 肥皂网络服务。我添加了来自生成异步操作的 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");
                }
            }

当我在模拟器上测试它时,我只是坐着等待回复,当我尝试使用像安卓手机这样的手机时,就会出现错误,即 targetinvocationexception。 我应该怎么做才能解决这个问题?

【问题讨论】:

    标签: c# web-services wcf xamarin.ios xamarin.forms


    【解决方案1】:

    这不是一个好的实现有几个原因。首先,您使用async void 处理异步完成事件,该事件将默默地忽略任何引发的异常。其次,Async/Completed 模式不适合单次异步调用。第三,Async/Completed 模式产生的代码在大多数情况下(包括这种情况)真的很混乱。

    您应该改用Task.Factory.FromAsync&lt;&gt;() 帮助程序,它将大大简化您的代码并解决这些问题。对你来说它看起来像这样:

    <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 用于控件的事件处理程序(正如我在示例代码中所做的那样)。

    【讨论】:

    • 感谢基思的帮助。我尝试了您提到的更改,出现了两个问题,首先是 targetinvocation 异常的问题,其次我总是需要将 xamarin.forms 项目的配置文件切换到 4.5 #7 意味着它不支持 windows phone 8.1。你知道吗?
    • TargetInvocationException 将有一个 InnerException 指向真正的问题。 TargetInvocationException 本身只是原始崩溃的包装器。
    • 在 Xamarin Studio 下创建 Xamarin Forms 项目时的默认 PCL 配置文件是配置文件“78”。如您所见,这不包括对 Windows Phone 8.1 的支持。如果您需要 Windows Phone 8.1 支持,则需要将其切换到配置文件 259。请记住,您可能依赖的某些 nuget 包可能无法正确支持该配置文件。但是大多数主流软件包都支持它。
    • 我在答案中的评论中发布了堆栈跟踪,也许有人已将其删除。在内部异常中,我们添加新服务引用时,Visual Studio 生成的references.cs 文件中有一个异常。当响应到来时,异常来了。我现在正在做的是,我创建了一个肥皂请求,并且我正在手动使用 web 服务而不添加服务引用。它工作正常。我认为添加服务引用或其他内容存在一些错误或问题。我什至尝试在 xamarin studio 中添加服务引用,但出现了同样的问题
    • 我明白了。对于 WCF 服务代理代码,我直接使用了 Silverlight 5 Client SDK 中的 svcutil.exe 工具,并没有尝试使用 Visual Studio 中的添加服务引用。 scvutil 工具会生成一个 .cs 文件,我们可以将其包含在我们的项目中。我们通常在 Xamarin Studio 中工作(它没有“添加服务引用”),所以这样会更好。
    【解决方案2】:

    我认为,现在 Xamarin.Forms 中存在导致问题的错误。我已删除服务引用,现在手动使用该服务。我从以下链接中找到了线索,并使用相同的方式使用 Web 服务。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-26
      • 2016-04-22
      • 1970-01-01
      • 1970-01-01
      • 2018-09-28
      相关资源
      最近更新 更多