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 8065529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:41:39+00:00 2026-06-05T11:41:39+00:00

I am trying to save and retrieve an Image from HTTPRuntime Cache but I

  • 0

I am trying to save and retrieve an Image from HTTPRuntime Cache but I am getting an exception. I am able to save a stream to cache but when I try to retrieve it I get an exception saying:

the request was aborted. The connection was closed unexpectedly

Here is my code:

public void ProcessRequest(HttpContext context)
{   
    string courseKey = context.Request.QueryString["ck"];
    string objKey = context.Request.QueryString["file"];

    if(HttpRuntime.Cache[objKey] !=null)
    {
        using (Stream stream = (Stream)HttpRuntime.Cache[objKey]) // here is where I get an exception
        {
            var buffer = new byte[8000];
            var bytesRead = -1;
            while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
            {
                context.Response.OutputStream.Write(buffer, 0, bytesRead);
            }
        }
        return;
    }
    var response = Gets3Response(objKey, courseKey, context);           

    if (response != null)
    {
        using (response)
        {
            var MIMEtype = response.ContentType;
            context.Response.ContentType = MIMEtype;
            var cacheControl = context.Response.CacheControl;
            HttpRuntime.Cache.Insert(objKey, response.ResponseStream, null, DateTime.UtcNow.AddMinutes(20), Cache.NoSlidingExpiration);
            using (Stream responseStream = response.ResponseStream)
            {
                var buffer = new byte[8000];
                var bytesRead = -1;
                while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                   context.Response.OutputStream.Write(buffer, 0, bytesRead);
                }
            }
        }
    }
}

And here is the exception I am getting

Exception

  • 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-05T11:41:41+00:00Added an answer on June 5, 2026 at 11:41 am

    This code is pretty confusing. First, you are caching response.ResponseStream, but that has been wrapped in a using block. So by the time you get to HttpRuntime.Cache.Insert, response.ResponseStream is already disposed and closed. Hence the error.

    You should not be caching a stream. For one thing, once you put a distributed cache service in place, your approach will be impossible. You need to refactor this. Consider:

    public class CacheAsset
    {
       public string FileName { get; set; }
       public string ContentType { get; set; }
       public byte[] Content { get; set; }
    }
    
    CacheAsset GetAsset(HttpContext context)
    {
       string courseKey = context.Request.QueryString["ck"];
       string objKey = context.Request.QueryString["file"];
    
       var asset = context.Cache[objKey] as CacheAsset;
    
       if (asset != null) return asset;
    
       using (var response = Gets3Response(objKey, courseKey, context))
       using (var stream = new MemoryStream())
       { 
          var buffer = new byte[8000];
          var read = 0;
    
          while ((read = response.ReponseStream.Read(buffer, 0, buffer.Length)) > 0)
          {
             stream.Write(buffer, 0, read);
          }
    
          asset = new CacheAsset
                  {
                     FileName = objKey,
                     ContentType = reponse.ContentType,
                     Content = stream.ToArray()
                  };
           context.Cache.Insert(objKey, asset, null, DateTime.UtcNow.AddMinutes(20), Cache.NoSlidingExpiration);
       }
    
       return asset;
    }
    
    public void ProcessRequest(HttpContext context)
    {
       var asset = GetAsset(context);
    
       context.Response.ContentType = asset.ContentType;
       context.Response.BinaryWrite(asset.Content);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get an image from an URL but when I save it
I am trying to save some values from my app using NSCoding. I'm able
I'm trying to save a file (pdf or image) uploaded from a form into
I am trying to save spinner selected value, but i am getting like below
I'm trying to capture the picture I'm getting from webview.capturePicture() to save it to
Im trying to save a simple script in netbeans to htdocs, but i only
I'm trying to save an excel spreadhseet from SQL Server Integration Services 2005. Unfortunatly
Hello I am trying to save a bitmap image in a basic image editor
I'm trying to save the returned data from HTTP request into a variable. The
In my app, I am trying to save and retrieval of an image in

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.