I have a requirement for a WCF service operation that accepts a large Stream, processes it and returns that stream.
I used an
https://msdn.microsoft.com/en-us/library/ms733742.aspx[
^] as a reference to what I need (unfortunately, I am not allowed to post links).
Following the advice in that article:
Details:
Because I need to accompany both the input and return streams with MetaData, I decorated the classes with MessageContract attributes, as is required.
Here is a brief run-down of my implementation:
Message Contracts:
[MessageContract]
public class InputStreamMessage
{
[MessageHeader]
public InputStreamHeader Header { get; set; }
[MessageBodyMember(Order = 1)]
public Stream Data { get; set; }
}
[MessageContract]
public class OutputStreamMessage
{
[MessageHeader]
public OutputStreamHeader Header { get; set; }
[MessageBodyMember(Order = 1)]
public Stream Data { get; set; }
}
Service Contract:
[ServiceContract]
public interface IStreamService
{
[OperationContract]
OutputStreamMessage ProcessStream(InputStreamMessage input);
}
Service Implementation:
public OutputStreamMessage DoStreamOperation(InputStreamMessage input)
{
OutputStreamMessage output = DoSomeNonBufferedProcessing(input);
return output;
}
Client-side:
On the client-side, I then generate the service reference, and call the service as below:
private void PerformStreamOperation()
{
try
{
StreamServiceReference.StreamServiceClient client = new StreamServiceReference.StreamServiceReferenceClient();
client.Open();
InputMessageHeader header = new InputMessageHeader();
var inputstream = SomeOperationToGetInputStream();
client.DoStreamOperation(header, ref inputstream);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Stream Processing Error");
}
}
The MSDN article uses almost the same contract format(without meta data) that exists in the
official WCF samples[
^]- Look at
[OperationContract]
Stream EchoStream(Stream stream)
But no example of an equivalent MessageContract implementation. The generated service reference method accepts a stream and returns a stream.
Questions:
- I would like to know why the generated service operation does not have a return type when I specified it in the contract?
- If that is expected behaviour, how should I get it to pass on a stream and return a processed stream?
- If I designed my logic wrongly, what would be the recommended way?