My code calls the WCF service, which is not working. Therefore, we must expect EndPointNotFoundException
. The using statement attempts a Close()
damaged connection that calls CommunicationObjectFaultedException
, which is thrown. This exception falls into the catch try block surrounding the use 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");
}
}
}
Please note that the EndPoint service uses the new Guid, so it will never listen to the service.
IDummyService
:
[ServiceContract]
interface IDummyService
{
[OperationContract]
string Echo(string e);
}
This causes the Visual Studio debugger (Visual Studio Professional 2017 15.4.1) to crash into the Excluded Unhandled popup: The
exception that Visual Studio breaks System.ServiceModel.CommunicationObjectFaultedException
into is where gets into the code.
, catch(CommunicationObjectFaultedException ex)
. LinqPad , , .
() 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
, , , await
Task
. " "; . .
" " ( Jack Zhai-MSFT) Visual Studio
:
, /.
, , , / .
CommunicationObjectFaultedException
async
?