"The underlying connection was closed: The connection was closed unexpectedly" when Sendreply has instance of Stream RRS feed

  • Question

  • i have Workflow service. The content of the SendReply  activity is set to "Response" class

    public class Response
     public Stream TheFile{get;set;}

    if the Stream is null then evrything works fine, but if the stream is not null ( even if the length of the stream is zero) i get the following exception

    "The underlying connection was closed: The connection was closed unexpectedly

    Thursday, February 9, 2012 6:18 PM


All replies

  •  We have a requirement where client wants to download a file from the server. Here how the current architecture is setup:

    Client is Silverlight application. The URL to the file is provided using REST service. The REST service internally calls WF service using proxy, and WF service actually does the work of reading the file.

    Below is the pseudo code.

    public class SilverLightClient
            // on button click SilverLight client invoke the REST service URL
        public class RESTService
            [WebHelp(Comment = "Download a file")]
            [WebGet(UriTemplate = "DownloadFile?fileID={fileID}")]
            System.IO.Stream DownloadFile(long fileID)
                WorkflowProxy proxy = new WorkflowProxy();
                Response response = proxy.DownloadFile(fileID);
                // code here Reads response.TheFile write into AdapterStream and returns the adapter stream
                return AdapterStream;
        public class WFServiceDownloadServiceActivity : CodeActivity
            public InArgument<long> FileID { get; set; }
            public OutArgument<Response> TheResponse { get; set; }
            public void Execute(CodeActivityContext context)
                Response response = new Response();
                System.IO.Stream theSourceFile = null; // this is the actual file. This could be memory stream or FileStream based on the size.
                // Question: 
                //should i assign the file here
                response.TheFile = theSourceFile;
                // or should i write the file
                theSourceFile.CopyTo(response.TheFile, 4096);
                TheResponse.Set(context, response);            
        // NOTE: Response is set as content for SendReply in the work flow
        public class Response
            public System.IO.Stream TheFile {get;set;}


    My question here

    Should I assign the instance of source file to response or should I write the source file into response.?

    If I assign the source instance to response, when the stream will get dispose?

    • Merged by LeoTang Tuesday, February 14, 2012 10:56 AM
    Thursday, February 9, 2012 4:15 PM
  • Hi,

    I'd suggest you transfer data of stream into a data structure. For example, string or array of bytes.

    You can convert steam to a string in your rest service, then convert it back to stream in your client application.

    StreamReader..::.ReadToEnd Method


    Encoding..::.GetBytes Method (String)


    Hope this helps, thanks.

    Leo Tang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by LeoTang Sunday, February 19, 2012 9:20 AM
    Wednesday, February 15, 2012 3:25 AM