我在Xamarin.Forms标准1.4上有一个.NET应用程序,它使用.NET与位于本地网络中的服务进行通信。由于有一些非常长的运行操作,我不得不实现非常高的超时(600秒)。如果服务不可用,应用程序将在那里静坐600秒钟,什么也不做,只为了告诉用户该服务不可用。因此,我实现了ping契约,它每5秒调用一个函数ping,因此我知道服务器是可用的,它将显示给用户(根据用户的请求)。
在Android上,这很好。如果服务器可用,则一切运行顺利,如果服务器因某种原因脱机,则抛出、捕获异常,并通知用户服务器处于脱机状态。
另一方面,UWP的异常似乎被捕获,并且在我能够捕捉到它之前,通过WCF实现的所有层进行调试输出,并被重新抛出100次。在我的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.dllEdit2:
现在,我已经更新了项目中的每个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或其他正在发生的任务。
发布于 2019-01-07 21:08:28
我和你有同样的问题,并从这个帖子得到答案。这可能对你的信息有帮助。
“您所看到的基本上是Visual调试器特性--在应用程序中抛出的任何异常都会被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功能,它们被打印出来。“
https://stackoverflow.com/questions/47675098
复制相似问题