0

I have a WCF service which is returning PDF files as a stream over a NetTcp endpoint. This is all working fine. However, if the file is not found, or if I can't find the related row in the database to retrieve the filepath then I return Stream.Null from the service.

However I can't seem to find a way to compare the returning stream to check and see if it is empty or not.

I have tried the following:

If myStream IsNot Stream.Null
    'Code to execute if stream isn't empty
End If

and

If Not streamPDF.Equals(Stream.Null)
    'Code to execute if stream isn't empty
End If

But neither of these work as the code just carries on into the If block.

Any thoughts?

Ira Rainey
  • 5,173
  • 2
  • 34
  • 42

3 Answers3

0

The Stream.Null is only your service scoped construct. For client you will get most probably empty stream => it will be same stream type as if you really send data.

It is strange design choice. If file doesn't exists it is "expected" exception and it should be handled with FaultContract and typed FaultException.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Yeah, I was looking at this next. I was trying to avoid using an exception for it, but I think you're right. I'll give that a whirl now. – Ira Rainey Sep 26 '11 at 10:09
  • Defining a `FaultContract` and throwing a `FaultException` sorts it out nicely. Thanks. – Ira Rainey Sep 26 '11 at 10:22
0

The Stream.Null field which redirect the stream that will not consume any operating system resources.

You can use Stream.Length property to check whether a stream is empty or not.

Community
  • 1
  • 1
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
0

If your code runs on a different machine, it's normal. The null stream ("A Stream with no backing store" from the documentation) is not marshaled differently over the wire. On the client side, it's just a stream which always returns EOF.

If you need to carry some extra information, you need to find another way (other parameters /value information, exceptions), but not use the Stream itself, or use a data convention understood by both parties. For example "if the file is only 1 byte long and contains the character 255, then, it's the 'null stream'".

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298