【问题标题】:WCF Exceptions with extreme impact on app performance对应用程序性能有极大影响的 WCF 异常
【发布时间】: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.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 或此时正在发生的任何其他任务。

【问题讨论】:

  • 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


【解决方案1】:

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

“您所看到的基本上是 Visual Studio 调试器功能 - 应用程序中抛出的任何异常都会被 VS 视为“第一次机会异常”。 见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 功能。”

【讨论】:

  • 感谢您提供此信息。很高兴知道 Visual Studio “只是做它的工作”,但它应该更快一点。即使只是在调试中,也要花费几秒钟的时间来处理异常,这是不可接受的。但自从我改用 ASP.NET Core WebAPI 之后,我就再高兴不过了。它比 WCF 更快、更灵活且更易于维护。此外,它在 iOS 和 UWP 上无需预编译代理类解决方法即可工作。
猜你喜欢
  • 1970-01-01
  • 2016-11-06
  • 1970-01-01
  • 1970-01-01
  • 2010-09-22
  • 1970-01-01
  • 2012-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多