Content Proxy ASP.NET

Content proxy is a web service on your server that can fetch data from external URLs and return it to the browser (see Figure ).

The browser makes a call to the proxy to fetch data from an external URL

The browser makes a call to the proxy to fetch data from an external URL

The proxy can cache the response on the server for some period and thus save repeated calls to same external URL.For example, if a hundred users subscribe to the same RSS feed and the feed does not change for days, the proxy can cache the response from the feed source for one day and serve hundreds and thousands of users directly from its server-side cache (see Figure).

Server-side caching greatly improves load time for users because there ’s only one network roundtrip.The server doesn’t have to go to the external source.The proxy can also produce response headers that will tell the browser to cache the response on the browser for a period of time. During this time,the same call to the proxy will be served from the browser cache and complete blazingly fast.There’ll be no net work round trip at all on sub sequent calls to same data from the proxy (see Figure ).

The proxy caches data on the server and prevents repeatedly calling the same external source for multiple users

The proxy caches data on the server and prevents repeatedly calling the same external source for multiple users

When a response is cached on the browser, it does not make call to the proxy and thus there’s no network roundtrip at all,

When a response is cached on the browser, it does not make call to the proxy and thus there’s no network roundtrip at all,

This means if we use a proxy to fetch RSS feeds and cache the response in the browser for an hour,and the user goes to another web site and comes back, the RSS wid gets will load instantly with out making a call to the server. So,if you have only RSS widgets on your page, the whole page will load in just one call to Default.aspx. Every thing else is already cached on the browser.Chapter 9 explains how client-side caching can be used to speed up RSS loading.

Content Proxy Web Service

Proxy.asmx is the content proxy web service. It has three methods:

GetString( url, cacheDuration )
Returns data from the URL in string format and caches on the browser for a specified duration

GetXml(url,cacheDuration )
Returns XML from a specified URL and caches the response on the browser for a specified duration

GetRss( url, count, cacheDuration)
Returns the RSS feed converted to a LINQ projection (discussed in Chapter 3) from a specified URL and caches the feed on the server for 15 minutes and on the client for a specified cache duration

GetString and GetXml are simple; they just use WebClient to get data from the original URL and cache the response (see Example ).

GetString and GetXml methods of the proxy web service
The only difference between Get String and Get Xml is that Get String returns a JSON representation of string content,whereas GetXml returns XML content as a string. The Response Format attribute on the GetXml method tells ASP.NET AJAX to emit XML in plain-text format instead of delivering JSON

However, GetRss is a bit more complicated.It downloads the feed and caches it using ASP.NET cache for 15 minutes.So, subsequent calls to the same feed are returned from the ASP.NET cache.GetRss also generates a proper response cache header to cache the response on the browser for a specified duration.So, the widget consuming RSS via proxy can control how long to cache the response on the browser.

Example shows the same RSS loading and parsing code that was in the RSS widget in Chapter.

GetRss on a proxy web service

GetRss on a proxy web service(continued)

GetRss on a proxy web service(continued)

Challenges with the Proxy Web Service

The proxy web service will become your most frequently used web service once you start making client-side widgets.Anytime the browser’s JavaScript needs to fetch content from an external domain,it will have to call the proxy service.As a result, you will hit scalability challenges with the proxy web service. Thousands of widgets making calls to this service, which, in turn, results in external web service calls, will create a significant load on the ASP.NET process.

The external services’ response time is unpredictable and unreliable.Sometimes a heavily loaded external service might take 20 to 30 seconds to complete which means the call to the proxy will get stuck for 20 to 30 seconds. If that happens to 100 incoming requests within a 20- second period, all of the available ASP.NET worker thread will be used. Your web application will no longer serve any requests until the proxy requests either complete or timeout and release the ASP.NET worker thread.

This will create a slowloading web site or a completely nonresponsive web site. Chapter discusses the scalability challenges web applications have that heavily depend on web services and fetch a majority of the content from external services.

Now we have the necessary methods to download data from external domains directly from the browser using the proxy.The next step is to make a JavaScript client-side widget that uses the proxy to download and cache data on the browser.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

ASP.NET Topics