Reputation: 335
I have fall under the same situation as per this question however, this is the difference is that the existing question has the access to server code to throw FaultException if Stream is null while I'm consuming a supplier web service whereby it returns Stream.Null
is the web service is null.
We did try to request supplier to change the method to throw fault however the reason given by supplier is that there are existing other system that consume this and changing from returning Stream.Null
might potentially break other system instead.
I was wondering if anyone would be able to teach me how to perform Null Checking on Stream.Null please?
I have tried == null
and it seems not working. The webservice is bind using Web Reference towards WCF.
Update
I have found that returning Stream.Null via WCF will result CanSeek = false (as per what is mentioned in one of the comment as below (Well, at least it is what being returned from my supplier web service.). Does this mean that every stream returned from WCF is CanSeek = false and unable to find the length?
Upvotes: 3
Views: 5658
Reputation: 731
I know this question is old, but this code worked for me
if (stream == Stream.Null)
{
// Do something
}
https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.null
Upvotes: 1
Reputation: 424
Since you're using WCF, checking Length without reading from the stream is not possible, so instead you can read a byte from the stream to see if it's empty.
var isEmpty = streamFromWcf.ReadByte() == -1;
It doesn't tell you whether it's a Stream.Null stream though - only that it's empty. Depending on your requirements you may instead want to send a boolean along with the stream that says whether it's actually a null stream.
Upvotes: 4
Reputation: 6455
You might want to check the Length
of the stream instead. If it is 0, then the stream is empty.
Upvotes: 1