【发布时间】:2017-12-06 13:09:12
【问题描述】:
我在 .NET Standard 1.4 上有一个 Xamarin.Forms 应用程序,它使用 WCF 与位于本地网络中的服务进行通信。由于有一些运行时间很长的操作,我不得不实现非常高的超时时间(600 秒)。如果该服务不可用,应用程序现在将在那里停留 600 秒,并且什么也不做,只是告诉用户该服务不可用。所以我实现了一个 ping 合约,它每 5 秒调用一次函数 ping,这样我就知道服务器可用,它显示给用户(根据用户的请求)。
在 Android 上这很好。如果服务器可用,则一切运行顺利,如果服务器由于某种原因离线,则抛出、捕获异常,并通知用户服务器离线。
另一方面,在 UWP 上,异常似乎被捕获并重新抛出一百次,并在我能够捕获它之前通过 WCF 实现的所有层进行调试输出。在我的 Lumia 930 上,这需要长达 9 秒的时间,并且即使整个 ping 操作是在未等待的单独任务中执行的,也会严重影响设备。在发布模式下,这不那么极端,但大多数时候仍会冻结 UI。我该如何解决这个问题?
编辑:
这是逐行打印,而不是每次执行 ping 时一个块,但服务不可用。我自己也没有打印任何东西。
Exception thrown: 'System.IO.FileLoadException' in System.Private.CoreLib.ni.dll
Exception thrown: 'System.IO.FileLoadException' in System.Private.CoreLib.ni.dll
Exception thrown: 'System.IO.FileLoadException' in System.Private.CoreLib.ni.dll
Exception thrown: 'System.IO.FileLoadException' in System.Private.CoreLib.ni.dll
Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.ServiceModel.dll
Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.ni.dll
Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.ni.dll
Exception thrown: 'System.OperationCanceledException' in System.Net.Http.dll
Exception thrown: 'System.OperationCanceledException' in System.Private.CoreLib.ni.dll
Exception thrown: 'System.TimeoutException' in System.Private.ServiceModel.dll
Exception thrown: 'System.TimeoutException' in System.Private.CoreLib.ni.dll
Exception thrown: 'System.TimeoutException' in System.Private.ServiceModel.dll
Exception thrown: 'System.TimeoutException' in System.Private.CoreLib.ni.dll
Exception thrown: 'System.TimeoutException' in System.Private.CoreLib.ni.dll
Exception thrown: 'System.Reflection.TargetInvocationException' in System.Private.CoreLib.ni.dll
Exception thrown: 'System.TimeoutException' in System.Reflection.DispatchProxy.dll
Exception thrown: 'System.TimeoutException' in System.Private.CoreLib.ni.dll
Exception thrown: 'System.TimeoutException' in System.Private.CoreLib.ni.dll
编辑2:
我现在已经更新了项目中的每个 NuGet 包(主要是 System.Net、System.ServiceModel 和 Microsoft.NETCore.UniversalWindowsPlatform 包)并且它变得更好了。无论服务的状态如何,它都会抛出异常之前。现在它只在服务不可用时抛出异常,并且异常更少:
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.Threading.dll
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.CoreLib.dll
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.ServiceModel.dll
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.CoreLib.dll
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.CoreLib.dll
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Net.Http.dll
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.CoreLib.dll
Exception thrown: 'System.TimeoutException' in System.Private.ServiceModel.dll
Exception thrown: 'System.TimeoutException' in System.Private.CoreLib.dll
Exception thrown: 'System.TimeoutException' in System.Private.ServiceModel.dll
Exception thrown: 'System.TimeoutException' in System.Private.CoreLib.dll
Exception thrown: 'System.TimeoutException' in System.Private.CoreLib.dll
Exception thrown: 'System.TimeoutException' in System.Private.CoreLib.dll
Exception thrown: 'System.TimeoutException' in System.Private.CoreLib.dll
但异常仍会冻结 GUI 或此时正在发生的任何其他任务。
【问题讨论】:
-
On UWP on the other hand the Exception seems to be caught and re-thrown up a hundred times with a debug output through all the layers of the WCF Implementation before I can catch it.你是怎么得出这个结论的? -
编辑了我的问题。
-
另一个编辑,提供更多信息
-
好的,当我在没有调试器的情况下运行应用程序时,一切似乎都很好。没有冻结,没有口吃,一切都像黄油一样光滑。
-
考虑一种更可持续的长期运营方法:stackoverflow.com/questions/2400263/…
标签: c# wcf xamarin.forms uwp .net-standard