Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8135511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:26:51+00:00 2026-06-06T10:26:51+00:00

Is it possible to return a stream which is part of a complex object

  • 0

Is it possible to return a stream which is part of a complex object as returned data from a Wcf method?

I have checked most of the msdn references on returing stream data with Wcf; such as this one. All of the examples seem to show how to return stream when the method return type is Stream (or parameter is stream).

What I wanted know is can it return the stream if the data is part of complex object property? For example, can GetData() return the Data which contains a stream as shown below:

[DataContract]
public class Data
{
    [DataMember]
    public string Info { get; set; }

    /// <summary>
    /// This is the file stream that would be returned to client.
    /// </summary>
    [DataMember]
    public Stream File { get; set; }
}

[ServiceContract()]
public interface IService
{           
    [OperationContract]
    Data GetData();
}

From my initial testing, it seems that this doesn’t work. I get exception on client side (unexpected socket closure). Result is same regardless of DataContractSerialization or XmlSerialization. I have set the required streaming mode with TransferMode.Streamed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:26:53+00:00Added an answer on June 6, 2026 at 10:26 am

    You can’t do that, see this documentation

    At least one of the types of the parameter and return value must be either Stream, Message, or IXmlSerializable.

    So the way you’ve written it isn’t going to work for TransferMode.Streamed however nothing at that link explicitly says a property of type Stream wouldn’t be serialized but from experience I would not expect that to work.

    Instead you should return a Stream and define the first x bytes as your string Info field.

     [ServiceContract()]
     public interface IService
     {           
         [OperationContract]
         Stream GetData();
     }
    

    thus when writing to the Stream (server side) you would do

     stream.Write(infoStr);//check size and truncate if appropriate
     stream.Write(fileBytes);//write your file here
    

    Then on the other side you need to read from the stream correctly to get your data out of the stream. What I would suggest is writing 2 int to the stream first. The first would be the size of your infoStr field and the second is the size of your file. On the client size you read these off first then you know how many bytes you need to read.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The documentation for ref() mentions several possible return values. I understand most of them,
Possible Duplicate: Return value from thread I want to get the free memory of
Is it possible to return a 307 Temporary Redirect from a controller in ASP.NET
I have 2 powershell scripts that I execute from c# which I'm using to
In C# it is not possible to override a static method. Nevertheless I have
In Jinja2, is it possible to have a Node from the AST render after
I have a server on which I keep track of some data. When I
I have created a function in F# to recover historical data from Yahoo (the
Is it possible to have a single stream and have more than one XmlWriter
I'm trying to implement a FUSE-driven filesystem in Python, which serves data from both

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.