tags:

views:

235

answers:

1

I'm very new to WCF, so I apologize in advance if I misstate something.

This is using .NET 4.0 RC1.

Using WCF, I am trying to deserialize a response from the server. The base response has a Stream as its only MessageBodyMember.

public abstract class StreamedResponse
{
  [MessageBodyMember]
  public Stream Stream { get; set; }

  public StreamedResponse()
  {
    this.Stream = Stream.Null;
  }
}

The derived versions of this class are actually what's serialized, but they don't have a MessageBodyMember attribute (they have other base types such as int, string, etc listed as MessageHeader values).

[MessageContract]
public class ChildResponse : StreamedResponse
{
  [DataMember]
  [MessageHeader]
  public Guid ID { get; set; }

  [DataMember]
  [MessageHeader]
  public string FileName { get; set; }

  [DataMember]
  [MessageHeader]
  public long FileSize { get; set; }

  public ChildResponse() : base()
  {
  }
}

The Stream is always a FileStream, in my specific case (but may not always be).

At first, WCF said FileStream was not a known type, so I added it to the list of known types and now it serializes. It also appears, at first glance, to deserialize it on the client's side (it's the FileStream type).

The problem is that it doesn't seem to be usable. All the CanRead, CanWrite, etc are false, and the Length, Position, etc properties throw exceptions when being used. Same with ReadByte().

What am I missing that would keep me from getting a valid FileStream?

+1  A: 

The short answer is that you can't get a FileStream instance. In WCF, you are working across app-domain boundaries (you don't have to, but it's assumed that you are). Because of this, you can't serialize a FileStream into a value and transport it across the app-domain barrier (the FileStream is app-domain-specific, most notably because it works with unmanaged file handles which don't have meaning outside of the current app-domain).

That being said, if you really need information about the stream as well as the contents, then you probably will want to add the information about the stream as message headers and then retrieve those headers, as the Stream instance that you get on the either side will never be the actual stream type that was set on the calling/callee side.

casperOne