.Net remoting error “The input stream is not a valid binary format.”

I have a .Net remoting application, that often reports System.Runtime.Serialization.SerializationException “The input stream is not a valid binary format. The starting contents (in bytes) are: xx-xx-xx …”

There area few threads  about this error(e.g .Re: Error passing a large variable as a parameter to a remote method  and Remoting / Serialization problem trying to Migrate to .NET 2.0 )

As it is correctly noted in the posts,  IIS sends back an error message as plain text that the binary formatter doesn’t know how to handle.

In other words, unhandled exception on remoting server causes  (De)SerializationException on the client.

As an example, when I’ve made a mistake in web.config on remoting server, IIS sent the error to the client, but client just show hexadecimal starting content.

 If the content would be shown as text as well, it will be easy to recognize the problem. 

The good policy will be not allow to throw exception through remoting server boundary and catch them,log and return some indicator (e.g. null,false, exception as a parameter etc) to client.  Unfortunately someproblemslike invalid web.config willbe reported before your code (with try/catch) will be invoked.

I am using A custom channel sink to fix the HttpChannel/BinaryFormatter/ASP.NET host bug and it helps.