When reading normally from an SslStream using the Read(byte[] buffer, int offset, int count) method, I get the expected results.
However, if I move the SslStream object into a new AppDomain, the read still appears to function correctly (i.e. the correct number of bytes read is returned), but the buffer array is empty.
Why is this?
After some investigation, it appears that the contents of parameter arrays are not marshalled across AppDomains (probably for performance reasons).
Therefore, the data in the
bufferparameter is only passed one way. Modifications to the array in the remote AppDomain will not be seen by the caller in the local AppDomain.The way to force data in array parameters to be returned is to add the
[Out]attribute to the parameter.To solve the problem stated in the question, create a wrapper class for the
SslStreamand use that instead:The class has the
[Serializable]attribute, allowing it to be passed between AppDomains, and the implicit[In]parameter is included for consistency with other Stream classes.Many other .NET classes inheriting from
Stream(such asMemoryStreamandBufferedStream– and evenStreamitself) include the[In, Out]attributes for thebufferparameter in theRead()method.I wonder if it was a deliberate choice to omit them for
SslStream… This goes for all versions of .NET.