Getting the underlying stream from a wcf streaming webservice
I am building a streaming webservice in WCF and have an issue getting access to the underlying stream.
I am implementing an existing contract based on a wsdl but I would like to make the service streaming. Since I cannot change the contract, I cannot use the Stream as a parameter option (because this changes the contract).
So I have settled on using a Message as parameter and return. This works flawlessly.
But I would like to have access to the Stream object (which must be somewhere inside the Message object or its properties). But the only thing that the Message gives me is a XmlDictionaryReader and this does not allow access to the Stream.
public Message GetMessage(Message message)
{
// We need some way to access the underlying stream from the message - the GetReaderAtBodyContents is not usable
XmlDictionaryReader reader = message.GetReaderAtBodyContents();
CopyReaderToFile(reader);
return Message.CreateMessage(MessageVersion.Soap11, "http://tempuri.org/IStreamingService/GetMessageResponse", "Hej med dig fra GetMessage. MessageLe开发者_运维知识库ngth ");
}
The above shows my service implementation. The major problem with the XmlDictionaryReader is that it does not give me access to the precise data sent by the client - in other words I cannot reproduce the content from the client on the service. This is bad because I then cannot verify the signature which is present in the MessageHeaders.
So is there a way of accessing the underlying stream? Can this be achieved by behaviours?
Thanks in advance
The problem is that streaming is not provided by message itself but by underlaying message encoder. As you can see in two methods of abstract MessageEncoder class:
- public abstract Message ReadMessage(Stream stream, int maxSizeOfHeaders, string contentType)
- public abstract void WriteMessage(Message message, Stream stream)
The stream is available only on channel level not in message. Another problem is that streaming cannot be combined with message security - so you can't use any built-in signing etc.
精彩评论