Cook Computing

customErrors and Custom Channels

January 23, 2004 Written by Charles Cook

When working with .NET Remoting it can be irritating when a client receives an exception like this:

System.Runtime.Remoting.RemotingException: Server encountered an internal error. For more information, turn on customErrors in the server's .config file.[/quote]You get this by default when the client is on a different machine to the server, and you have to use a config file on the server to change this behavior because it is not possible to configure customErrors programmatically.

It gets worse when you develop a custom channel because there is no documented way of preventing exceptions from being filtered for local as well as remote calls. However, if the server side of the channel adds a new header called "__CustomErrorsEnabled" and sets it to boolean false before calling ProcessMessage on the next sink in the chain, then exception filtering will be turned off for all calls. For example, taking the code from the named pipe channel sample:

...
    pipe.BeginReadMessage();
    ITransportHeaders headers = pipe.ReadHeaders();
    headers["__CustomErrorsEnabled"] = false;
    Stream request = pipe.ReadStream();
    pipe.EndReadMessage();
    ServerChannelSinkStack stack = new ServerChannelSinkStack();
    stack.Push(_transportSink, null);
    IMessage respMsg;
    ITransportHeaders respHeaders;
    Stream respStream;
    ServerProcessing processing 
        = _transportSink.NextChannelSink.ProcessMessage(stack,
            null, headers, request, out respMsg, out respHeaders,
            out respStream);
...

Thanks to Andy McMullan for pointing this out.