Passing multiple streams in WCF

What I have?

I have a WCF contract which accepts a Stream object.

What I need?

I want to convert the contract to accept multiple streams. But, there is some problem with passing multiple streams in WCF. Now I have two options,

(1) Call the WCF method multiple times

(2) Change contract to accept a two dimensional byte array with all file contents

Can somebody tell me which option is better?

Thanks in advance!

-------------Problems Reply------------

We solved this problem by building a Stream wrapper implementation on the client that concatenates a list of streams together, end to end (eg, .Read delegates to the first stream- when it hits EOF on stream 1, it starts reading stream 2, and so on). It has a metadata property that has the names of the streams and their positions, which we include on the call in a [MessageHeader] attributed arg that says what's in the stream and the offsets of each (the client streams must support .Length). The consumer of the streams on the server knows how to read this metadata object and hand out a list of streams in order. The only restriction is that they must be processed in order. Works great!

In WCF, you cannot have multiple stream parameters in a single service method. It's just a system restriction.

See the MSDN documentation on WCF streaming:

Restrictions on Streamed Transfers

Using the streamed transfer mode causes the run time to enforce additional restrictions.

Operations that occur across a streamed transport can have a contract with at most one input or output parameter. That parameter corresponds to the entire body of the message and must be a Message, a derived type of Stream, or an IXmlSerializable implementation. Having a return value for an operation is equivalent to having an output parameter.

So for now, all you can do is call your method several times.


In my system, I need to send a FileStream and a generic List of custom data to WCF service. This is how I do it to combine both into a single stream and send it to the service which returns a result stream to the client:

Service Contract:

public interface IMyService
ResultClass MyOperation(Parameters param);

Return Stream:

public class ResultClass
public System.IO.Stream Stream { get; set; }

Input Stream:

public class Parameters
public long Length1 { get; set;}
public long Length12 { get; set;}
public Stream Stream { get; set;}

At the WCF client:

public static void BuildStream(string filePath, List<CustomData> otherParams, ref Parameters param)
BinaryFormatter formatter = new BinaryFormatter();
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
MemoryStream allStreams = new MemoryStream();
MemoryStream dataStream = new MemoryStream();
formatter.Serialize(dataStream, otherParams);
dataStream.Seek(0, SeekOrigin.Begin);
fs.Seek(0, SeekOrigin.Begin);
param.TemplateLength = fs.Length;
param.DataLength = dataStream.Length;
param.Stream = allStreams;
param.Stream.Seek(0, SeekOrigin.Begin);

In the service implementation, pull out the first and second streams from the combined stream:

List<CustomData> dataSheets;
using (MemoryStream ms = new MemoryStream())
MemoryStream dataStream;
BinaryFormatter formatter = new BinaryFormatter();
byte[] templatebuffer, databuffer;
filebuffer = new byte[m_param.Length1];
databuffer = new byte[m_param.Length2];
// Get the whole stream
ms.Seek(0, SeekOrigin.Begin);
// Get the first stream
ms.Read(templatebuffer, 0, (int)m_param.TemplateLength);
// Get the custom data
ms.Read(databuffer, 0, (int)m_param.DataLength);
using (dataStream = new MemoryStream(databuffer))
dataStream.Seek(0, SeekOrigin.Begin);
dataSheets = (List<DataSheet>) formatter.Deserialize(dataStream);

You might consider MTOM binding and have a data contract with multiple byte[] properties.

Category:wcf Views:0 Time:2009-10-05
Tags: wcf

Related post

  • Multiple streams in wcf 2010-01-14

    I am trying upload multiple files using WCF. Since WCF only supports one stream I need to join multiple file streams together on the client and pass this to the service. The files might be huge and cannot be buffered in memory. How do I do this? Plea

  • How to pass multiple parameters in WCF Rest Service? 2012-02-21

    I'm developing WCF REST service in C#. It works fine for a single parameter. Now I need to extend it to support multiple parameters. Please help me on this issue. Thanks in advance... Use following declaration in interface: [OperationContract] [WebIn

  • Pass multiple parameters into WCF Web API-service 2011-10-28

    I'm wondering about the behind the scenes magic that's happening when you create a WCF-Web service. In one old project I got methods that I can call from JavaScript that look like this [OperationContract] [WebInvoke(Method = "POST", ResponseFormat =

  • How to pass multiple parameter in DomainService - WCF 2011-01-08

    Let's say I have a window which should submit 3 model in client side (Silverlight Client Application). My problem is each time I submit the form, data on the server side which I passed them from client are empty. I've used nested class which contains

  • How pass multiple body parameters in wcf rest using webinvoke method(Post or PUT) 2011-03-12

    I have written a REST Service in WCF in which I have created a method(PUT) to update a user. for this method I need to pass multiple body parameters [WebInvoke(Method = "PUT", UriTemplate = "users/user",BodyStyle=WebMessageBodyStyle.WrappedRequest)]

  • Returning multiple files in a stream in WCF 2011-11-29

    I have a initiative where I need to take in documents and convert them into images in a WCF application. When a file such as word document is passed through the stream, I will get multiple images for the document, i.e one image for each page in the d

  • Passing a stream to Restfull Wcf 2010-12-21

    I am passing a stream to rest full wcf service.I would be setting up this stream as a payload and not in the uri.In this case what would be my uri template.. my contract looks like this : [ServiceContract] public interface IRjisProxyWcfService { [Ope

  • Passing large files to WCF service 2008-09-05

    We have an encryption service that we've exposed over net.tcp. Most of the time, the service is used to encrypt/decrypt strings. However, every now and then, we the need to encrypt large documents (pdf, jpg, bmp, etc). What are the best endpoint sett

  • DirectShow on multiple machines with wcf 2009-10-22

    Is it any how possible to run DirectShow on multiple machines with wcf? --------------Solutions------------- DirectShow and WCF are completely unrelated. If your question is if you can stream a DirectShow graph using WCF to other computers then the a

  • How to pass multiple parameters to a thread function 2010-09-09

    I have created a function for a thread, but I want to pass multiple parameters to the function. Here's my source code : #include "work.h" #include <stdio.h> #include <unistd.h> #include <pthread.h> // compile with -lpthread int coun

  • "System.ServiceModel.CommunicationException" when trying to pass a bitmap through WCF 2011-04-15

    I'm trying to pass a Bitmap through WCF, but it throws System.ServiceModel.CommunicationException was unhandled by user code Message=The socket connection was aborted. This could be caused by an error processing your message or a receive timeout bein

  • Bulk calls vs multiple calls in WCF using MSMQ 2011-08-30

    I am calling a WCF service which contains the business logic to process the message objects. I need to pass the id of the message to WCF service. We are using MSMQ for queuing up the requests. There could be multiple messages that WCF service need to

  • How to pass multiple parameters to Eval()? 2009-01-26

    i have a bit of aspx code that uses Eval to generate a call to a javascript function: ASP.NET (wrapped for readability): <asp:LinkButton runat="server" OnClientClick='<%# Eval( "NodeGUID", "return DoStuff(this, \"{0}\");") %>' Text="Do stuff

  • How to pass multiple parameters in json format to a web service using jquery? 2009-02-02

    I'm trying to execute a webservice using jquery. When I pass only one input parameter it works fine: $.ajax({ type: "POST", url: url, data: "{'Id1':'2'}", contentType: "application/json; charset=utf-8", dataType: "json", success: callback });

  • Passing multiple values for a single parameter in Reporting Services 2009-02-04

    I have several Multi-Select parameters in my report. I am trying to find a way to pass in multiple values for a single parameter in the web query string? If I pass in a single value, it works fine. The report runs fine selecting multiple choices for

  • Passing multiple Eval() to a JavaScript function from ASPX 2009-04-14

    I am trying to pass multiple Eval() arguments to a JavaScript function from an .aspx file, but I keep getting compiler errors. I am new to JavaScript and have never really used Eval() before. Where am I going wrong? NB: The line shown below is actual

  • Passing Multiple Arguments from Panels into Views ( Drupal) 2009-04-21

    Anyone knows how to pass multiple arguments from Panels into a view in Drupal 6? Edit: I know how to pass one argument from Panels into Views, but not multiple arguments. --------------Solutions------------- In the panels argument field, separate the

  • how to Pass multiple parameters via Command Parameter in WPF? 2009-05-07

    how to Pass multiple parameters via Command Parameter in WPF?? Do we have to create an object and pass if so can someone help me with an example --------------Solutions------------- Maybe you can use MultiBinding with a converter that will create an

  • How to pass multiple checkboxes using jQuery ajax post 2009-05-26

    How to pass multiple checkboxes using jQuery ajax post this is the ajax function function submit_form(){ $.post("ajax.php", { selectedcheckboxes:user_ids, confirm:"true" }, function(data){ $("#lightbox").html(data); }); } and this is my form <form

Copyright (C), All Rights Reserved.

processed in 0.069 (s). 11 q(s)