对应用程序性能有极端影响的 WCF 异常

WCF Exceptions with extreme impact on app performance

我有一个基于 .NET Standard 1.4 的 Xamarin.Forms 应用程序,它使用 WCF 与位于本地网络中的服务进行通信。由于有一些非常长的 运行 操作,我不得不实现非常高的超时(600 秒)。如果服务不可用,应用程序现在会在那里停留 600 秒,什么也不做,只是告诉用户服务不可用。所以我实现了一个 ping 合约,它每 5 秒调用一个函数 ping,所以我知道服务器可用,并显示给用户(根据用户的请求)。

在 Android 这很好。如果服务器可用,一切都会顺利进行,如果服务器由于某种原因离线,则会抛出、捕获异常,并通知用户服务器离线。

另一方面,在 UWP 上,异常似乎被捕获并在我捕获它之前通过 WCF 实现的所有层的调试输出重新抛出一百次。在我的 Lumia 930 上,这最多需要 9 秒,并且会严重影响设备,即使整个 ping 操作是在一个没有等待的单独任务中执行的。在 Release 模式下,这没有那么极端,但大多数时候仍然会冻结 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.NetSystem.ServiceModelMicrosoft.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 或此时正在发生的任何其他任务。

我和你有同样的问题,并从这个 post 得到了答案。这可能对您的信息有所帮助。

"What you are seeing is basically a Visual Studio debugger feature - any exception being thrown in the application is noticed by VS as a "第一次机会例外”。 参见 https://blogs.msdn.microsoft.com/visualstudioalm/2015/01/07/understanding-exceptions-while-debugging-with-visual-studio/。 当您 运行 没有 VS 的独立应用程序时,您不会看到这些消息被打印出来。

更详细一点:这些异常实际上是作为 https://github.com/grpc/grpc/pull/11021/files 的一部分在代码中抛出的,但它们是预期的并被正确处理。它们实际上只是打印出来的 VS 功能。"