I have a very odd problem which I hope someone can help me with. I have written a very simple test program as an excercise in using a custom sink - just a simple server with a single method called GetReply() and a simple client. After configuring the remoting the code in the client looks like this:
try
{
responseTextBox.Text = server.GetReply();
}
catch (Exception MyErr)
{
MessageBox.Show(MyErr.Message, MyErr.GetType().ToString());
}
Now, if I run the client without running the server I expect to get an exception, which indeed I do - a SocketException with the message "No connection could be made because the target machine actively refused it", and without the custom sink this exception is caught in the above code and the message box displayed, just as you would expect.
However, when I add in the custom sink the behaviour changes - but ONLY when I am debugging in Visual Studio. Now the exception is flagged up in my IClientChannelSink.ProcessMessage() method by the debugger as an unhandled exception when it calls the ProcessMessage() method on the next sink. If I continue execution after the exception the message box is shown as before. Also, if I run the program outside the development environment it behaves as expected.
The custom sink will eventually be used in a 'real'application, in which it is quite acceptable for the client to run without the server, catch the exception and react accordingly. However, it is going to make debugging very difficult if, whenever I run the client in the development environment, it halts at this exception.
If anyone can shed any light on this I would be most grateful. I am using C# in Visual Studio 2005 with .NET2.0.

Exception in custom sink not handled when debugging
juanborde
Hi Mahajayar
thanks for that, but I have tried it and it doesn't help. I catch and rethrow the exception thus:
try
{
nextClientChannelSink.ProcessMessage(msg, requestHeaders, requestStream, out responseHeaders, out responseStream);
}
catch (SocketException e)
{
throw e;
}
but all that happens is that the unhandled exception error occurs on my throw instead of on the call to nextClientChannelSink.ProcessMessage.
This is a really annoying problem and I would greatly appreciate any help in solving it.
Encoad
Hi Dave,
You see this behavior because TcpClientTransportSink internally catches SocketException and then rethrows it to the calling app. You can do the same in your sink implementation and it no longer will be a unhandled exception.
dave.dolan
shriny
Thanks for getting back to me again Mahjayar. (I would have replied sooner but I *cannot* get the MSDN message boards to alert me to replies as they say they should).
The "Thrown" checkbox under CLR Exceptions is definitely unchecked, so that's not the cause of the problem.
I have discovered that I can work around the problem by unchecking the "User-unhandled" flag for the exception I am getting (System.Net.Sockets.SocketException), but this is not a very satisfactory workaround as it presumably means that the debugger will never break on this exception, even when there is no handler and it should break.
Any further suggestions would be appreciated. I am wondering if this is a bug that should be reported to Microsoft.