gpt4 book ai didi

c# - Visual Studio 在使用未处理的异常对话框处理异常时中断

转载 作者:太空狗 更新时间:2023-10-29 23:28:48 32 4
gpt4 key购买 nike

我的代码调用当前未运行的 WCF 服务。所以我们应该期待 EndPointNotFoundException . using 语句尝试 Close()导致 CommunicationObjectFaultedException 的故障连接这是异常(exception)。此异常在 using block 周围的 try catch block 中被捕获:

class Program
{
static void Main()
{
try
{
using (ChannelFactory<IDummyService> unexistingSvc = new ChannelFactory<IDummyService>(new NetNamedPipeBinding(), "net.pipe://localhost/UnexistingService-" + Guid.NewGuid().ToString()))
{
using (IClientChannel chan = (unexistingSvc.CreateChannel() as IClientChannel))
{
(chan as IDummyService)?.Echo("Hello");
}
}
}
catch (EndpointNotFoundException ex)
{
Console.WriteLine("Expected");
}
catch (CommunicationObjectFaultedException ex)
{
Console.WriteLine("Expected: caused by closing channel that has thrown EndPointNotFoundException");
}
}
}

请注意服务 EndPoint 使用新的 Guid,因此它永远不会有服务监听。

IDummyService 是:

[ServiceContract]
interface IDummyService
{
[OperationContract]
string Echo(string e);
}

这会导致 Visual Studio 调试器 (Visual Studio Professional 2017 15.4.1) 中断并显示“异常未处理”弹出窗口: Screen shot: Debugger breaking with Exception Unhandled popupVisual Studio 中断的异常是 System.ServiceModel.CommunicationObjectFaultedException 在代码中被捕获。

步进以继续执行显示已达到 catch(CommunicationObjectFaultedException ex)。使用LinqPad运行demo也显示异常如期捕获。

我还尝试明确(两次)关闭 channel 而不是使用 using-block:

class Program
{
static void Main()
{
try
{
using (ChannelFactory<IDummyService> unexistingSvc = new ChannelFactory<IDummyService>(new NetNamedPipeBinding(), "net.pipe://localhost/UnexistingService-" + Guid.NewGuid().ToString()))
{
IDummyService chan = null;
try
{
chan = unexistingSvc.CreateChannel();
chan.Echo("Hello");
}
catch (EndpointNotFoundException ex)
{
Console.WriteLine($"Expected: {ex.Message}");
}
finally
{
try
{
(chan as IClientChannel)?.Close();
}
catch (CommunicationObjectFaultedException ex)
{
Console.WriteLine($"Caused by Close: {ex.Message}");
}
}
}
}
catch (EndpointNotFoundException ex)
{
Console.WriteLine("Expected");
}
catch (CommunicationObjectFaultedException ex)
{
Console.WriteLine("Expected: caused by closing channel that has thrown EndPointNotFoundException");
}
}
}

这仍然会导致调试器在 Close 语句处中断。

我的异常设置未选中 System.ServiceModel.CommunicationObjectFaultedException。 (当它被选中时,Visual Studio 会按预期中断,并出现“抛出异常”对话框而不是“未处理的异常”对话框)。

当我启用“选项”\“调试”\“常规”\“仅启用我的代码”时,调试器不会中断。但是,我有 async 方法,异常应该留在我的代码中,稍后我在 awaiting Task 时捕获异常。对于这些方法,我需要取消选中“仅启用我的代码”;见Stop visual studio from breaking on exception in Tasks .

禁用“使用新的异常助手”(如 Jack Zhai-MSFT 所建议)Visual Studio 仍然中断并显示 Screenshot exception dialog when "Using the New Exception Helper" is disabled该对话框提供了一些附加信息:

The exception is not caught before it crosses a managed/native boundary.

我怀疑 using block 可能引入了这个托管/ native 边界。

是什么原因导致调试器错误中断,如何使调试器既不中断也不处理 CommunicationObjectFaultedException 异常,也不处理以后的处理程序 async 异常?

最佳答案

新的异常功能是在 VS2017 中,我们可以在工具->选项->调试->常规下禁用调试选项“使用新的异常助手”,它可以为您提供旧的异常消息,您可以访问它。

旧的异常消息显示异常跨越了托管/ native 边界: Screenshot exception dialog when "Using the New Exception Helper" is disabled

在TOOLS->OPTION->Debugging->General下选中“Break when Exceptions cross AppDomain or managed/native boundaries”。禁用“当异常跨越 AppDomain 或托管/ native 边界时中断”以避免 Visual Studio 在 OP 的情况下中断(尽管要小心,因为这也会在异常跨越 AppDomain 或托管/ native 边界的其他情况下禁用中断)。

关于c# - Visual Studio 在使用未处理的异常对话框处理异常时中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46850595/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com