我的代碼調用當前未運行的WCF服務。所以我們應該期待EndPointNotFoundException
。使用聲明嘗試Close()
故障的連接,導致除外的CommunicationObjectFaultedException
。該例外被捕獲在使用塊周圍try catch塊:Visual Studio在處理異常異常時出現異常,處理異常對話框
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");
}
}
}
注的服務端點使用新鮮的Guid所以它永遠不會有一個服務的聆聽。
IDummyService
是:
[ServiceContract]
interface IDummyService
{
[OperationContract]
string Echo(string e);
}
這將導致Visual Studio調試器(Visual Studio的專業2017年15.4.1)與 「異常未處理」 彈出打破: 異常上的Visual Studio休息是
System.ServiceModel.CommunicationObjectFaultedException
其中是在代碼中捕獲。
步進繼續執行表明已到達catch(CommunicationObjectFaultedException ex)
。使用LinqPad運行演示也表明,異常被捕獲如預期。
我也試過明確(雙)關閉通道,而不是使用using
- 阻塞的:
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按預期中斷並且使用「Exception Thrown」對話框而不是「Exception Unhandled」對話框)。
當我啓用「選項」\「調試」\「常規」\「啓用只是我的代碼」調試器不會中斷。然而,我有async
方法,其中異常應該離開我的代碼,我後來發現 await
。對於這些方法,我需要「啓用只是我的代碼」取消選中;見Stop visual studio from breaking on exception in Tasks。
繼續「使用新的異常助手」已禁用(由Jack Zhai-MSFT建議)的Visual Studio仍然打破了,它顯示 對話框提供了一些額外的信息:
的異常沒有被捕獲它穿過前一個管理/本地邊界。
我懷疑使用塊可能會引入此託管/本機邊界。
是什麼導致調試器錯誤地中斷以及如何使調試器既不中斷也不處理CommunicationObjectFaultedException
s也不對後來的處理器async
異常?
如果在VS2017的TOOLS-> OPTION-> Debugging-> General下禁用調試選項「使用新的異常幫助程序」,那麼結果如何? –
@ JackZhai-MSFT我使用「使用新的例外助手」進行了測試,並將結果包括在問題中。舊的例外對話爲解決問題提供了一些新的途徑。 –
@ JackZhai-MSFT謝謝,您的建議幫助我通過正確的設置解決問題。現在發佈答案。 –