-1

I am writing a RouteHandler as part of my MVC3 solution. It purpose is to get images and files from my cloud storage and deliver them to the browser while masking the cloud storage urls.

Everything from the "media" sub-domain gets routed to my MediaRouteHandler where I have implemented the logic to get the images.

I am struggling to get an asynchronous implementation for the HttpWebRequest. At best it behaves erratically. Sometimes bringing down the images correctly, sometimes not.

Does a standard browser load images synchronously or asynchronously? Or am I trying to do something that even the browsers don't generally do (and just wasting my time)?

i.e. If the default way a browser gets an image is from a synchronous thread, then I am happy just doing that.

Is that the case?

A bit of testing:

This is the result of my synchronous route handler. You will see that the image requests overlap, and by using fiddler to mimic modem download speeds, I can see them coming down at the same time at different speeds.

Synchronous Download Speeds

13
  • 2
    They work asynchronously. Have you ever been on a connection slower than infinite speed? You'll see them load in. Commented Aug 13, 2012 at 22:50
  • But is it the browser doing that or the framework? The browser could dispatch a thread for each image? Commented Aug 13, 2012 at 22:52
  • Think about that for a second, does that sound like something a browser would do? Consider that pages may have hundreds of images. That's not even considering all the resources that are non-images. Commented Aug 13, 2012 at 22:54
  • @shenku most "serious" IO work is done async; and I fully expect most browsers to be pretty serious about their IO. Commented Aug 13, 2012 at 22:54
  • arent MediaRouteHandler being requested one-by-one (I mean: one image file at once?) Commented Aug 13, 2012 at 22:55

2 Answers 2

1

If images are requested one-by-one then actually each MediaRouteHandler runs in its own thread. You can therefore simply perform a synchronous HttpWebRequest for each image as nothing is blocked (client browser needs full image file anyway).

Please note that asynchronous calls can be worth considering due to performance benefits. If image data is long - you can start pushing data to client browser although not whole data was yet downloaded from your cloud storage.

Sign up to request clarification or add additional context in comments.

3 Comments

This is my thinking as well, but wanted to get some confirmation on it as I wasn't sure. It would then be up to the browser to load multiples of this if it wanted to, right?
Browser simply request data from URL and does not need to know how the data is provided. It won't display image however until all data is collected (equivalent to Content-length) and proper HTTP status was given... It's up to you to i.e. provide some cache for frequently reused images.
Making HttpWebRequest calls asynchronously will significantly improve scalability of your web app if you also implement your MediaRouteHandler as IAsyncHttpHandler. With implementation like that, there will be NO blocking thread while the image is downloading from the cloud. ASP.Net will reuse the thread pool thread that otherwise would be blocked waiting for completion of a synchronous HttpWebRequest. Better thread economy -> better scalability.
1

Browsers load images asynchronously, but actually the main reason for asynchronous load is not to block thread (and some parallelism as well). If you really struggle with server side asynchronous requests, then try wrapping all calls as synchronous in some background thread. This way you do not block main thread and still load images synchronously. I am not sure though if this scenario will work properly in ASP.NET MVC. ;]

1 Comment

The browser does not care what you do on the server. It always reads asynchronously from the TCP stream.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.