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

  • Home
  • SEARCH
  • 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 4580148
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:44:33+00:00 2026-05-21T20:44:33+00:00

I have a WCF service that can return a stream via a WebGet. This

  • 0

I have a WCF service that can return a stream via a WebGet. This is working fine as far.
But what I would like to implement is support for the Range header, so that only parts of the file are returned.
This is my code this far:

public System.IO.Stream GetStream(string mElementID)
{
        // build the filePath
        FileInfo file = GetFile(mElementID);
        try
        {
            FileStream videoStream = File.OpenRead(file.FullName);

            if (request.Headers.AllKeys.Contains("Range"))
            {
                long startRange = ...; // get the start range from the header
                long endRange = ...; // get the end range from the header
                videoStream.Position = startRange;
                // how can I set the end of the range?
                //TODO: Don't forget to add the Content-Range header to the response!
            }

            WebOperationContext.Current.OutgoingResponse.ContentType = GetMimeType(file);
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Accept-Ranges", "bytes");
            return videoStream;
        }
        catch (FileNotFoundException){}
        catch (IOException ex)
        {
            throw ex;
        }
        // throw a 404
        throw new WebFaultException(System.Net.HttpStatusCode.NotFound);
}

I just create a FileStream, and return that. Now I wonder what is the best way to get a range of that Stream.

I think I could set videoStream.Position to the start value of the Range, but what is the best way to get a part from somwehere in the file to somewhere in the file?

Do I have to create a MemoryStream and write the relevant bytes into that?
The files that are streamed here are video files, so can be quite big.

  • 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-05-21T20:44:33+00:00Added an answer on May 21, 2026 at 8:44 pm

    You can do as you’ve suggested yourself. With the filestream, set the position to the start of the range. Create a byte array set to the length of the range you want. Then do

    videoStream.Read(myByteArray, 0, myByteArray.Length)
    

    Alternatively, you can set the position to the start of the filestream, and use the second parameter when calling read to offset yourself from the beginning of the fileStream.

    Once you’ve read into the buffer (byte array) you can place it into a new memory stream (which has an overloaded constructor that accepts a byte array). You can then return the derived memoryStream.

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

Sidebar

Related Questions

I have a RESTful WCF service that can return XML, JSON, or JSONP, depending
Lets say I have a WCF service that a client can use to receive
I have a WCF service that I have to reference from a .net 2.0
I have a WCF Service that should not enter the faulted state. If there's
I have a WCF service that is hosted in a windows application. The service
I have a WCF Service that exposes a method GetCustomers(). The internals of the
I have a WCF service that I call from a windows service. The WCF
I have a WCF service that uses basicHttpbinding in development. Now in product we
I have a WCF service that uses a System.ServiceModel.Syndication.SyndicationFeed to create an RSS feed.
I have a WCF service that is using a custom ServiceAuthorizationManager . The custom

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.