Speeding up your ASP.NET application
2013-08-03 02:22
316 查看
First request to your ASP.NET application seems to take much longer than others?That’s because to maximize resources IIS doesn’t keep all sites on an instance running at all times.A website on IIS is not loaded until its needed and, by default, will eventually
be unloaded after a period of inactivity.In this article we’ll see how we can enable Auto-Start feature either through IIS Manager or configuration files and even how we can perform more advanced application warm-up routines, such as priming a cache.
Configuring Auto-Start with IIS
1、Open Internet Information Services (IIS) Manager.
2、In the Connections pane, select theApplication Pools node, revealing the Application Pools pane in the main view.
3、Select the application pool for which you wish to enable Auto-Start.
4、Click Advanced Settings…
5、Locate the Start Mode option under the General group and set it toAlwaysRunning.
That’s it! Now the website hosted by the application pool you just modified will always be instantly available, even after extended periods of activity.
Configuration Auto-Start with applicationHost.config
ApplicationHost.config can be found at <Windows>\System32\inetsrv\config\applicationHost.config.Inside of the applicationHost.config file, locate the
Inside of the
Advanced Application Warm Up with Auto-Start
In the past it wasn’t unheard of for developers to prime their caches with these expensive data requests in the
For example, let’s imagine that the home page of your application contains a rather expensive call. In the fact, the call is so expensive that we’ve elected to cache the results improve the performance of subsequent requests. However, this still lays the
bulk of the work at the feet of the site’s first visitor.
While this technique does improve the performance for subsequent visitors, it still lays the bulk of the work at the feet of the site’s first. However, IIS now provides a better place to perform these types of warm-up tasks.
Preload clients, which implement the IProcessHostPreloadClient interface, contain a single method Preload(…) which can be used to cleanly perform these warm-up tasks.
However, this class will not be loaded automatically. We need to tell IIS both about the existence of this class as well as which website it applies to. To do this, we’ll need to return to our applicationHost.config file.
First, we’ll need to add an element called
This element registers our custom preload client with the name
Finally, we’ll locate the entry for our site under the sites node and register our preload client in our site’s application node.
This is done by adding the
<img onclick="javascript:alert('11')''/>
be unloaded after a period of inactivity.In this article we’ll see how we can enable Auto-Start feature either through IIS Manager or configuration files and even how we can perform more advanced application warm-up routines, such as priming a cache.
Configuring Auto-Start with IIS
1、Open Internet Information Services (IIS) Manager.
2、In the Connections pane, select theApplication Pools node, revealing the Application Pools pane in the main view.
3、Select the application pool for which you wish to enable Auto-Start.
4、Click Advanced Settings…
5、Locate the Start Mode option under the General group and set it toAlwaysRunning.
That’s it! Now the website hosted by the application pool you just modified will always be instantly available, even after extended periods of activity.
Configuration Auto-Start with applicationHost.config
ApplicationHost.config can be found at <Windows>\System32\inetsrv\config\applicationHost.config.Inside of the applicationHost.config file, locate the
<applicationPools>node located under
<configuration>/<system.applicationHost>.
Inside of the
<applicationPools>node, locate the entry whose name attribute corresponds to your application pool, and add a
startModeattribute with the value of “
AlwaysRunning”.
<applicationPools> <add name="MvcMusicStore" startMode="AlwaysRunning" /> ... </applicationPools>
Advanced Application Warm Up with Auto-Start
In the past it wasn’t unheard of for developers to prime their caches with these expensive data requests in the
Application_Start()function of Global.asax.cs, IIS now provides a cleaner way through the use of Auto-Start Providers.
For example, let’s imagine that the home page of your application contains a rather expensive call. In the fact, the call is so expensive that we’ve elected to cache the results improve the performance of subsequent requests. However, this still lays the
bulk of the work at the feet of the site’s first visitor.
public ActionResult Index() { var albums = HttpRuntime.Cache["TopSellingAlbums"]; if (albums == null) { var service = new TopSellingAlbumsService(); albums = service.GetTopFiveSellingAlbums(); HttpRuntime.Cache["TopSellingAlbums"] = albums; } return View(albums); }
While this technique does improve the performance for subsequent visitors, it still lays the bulk of the work at the feet of the site’s first. However, IIS now provides a better place to perform these types of warm-up tasks.
Preload clients, which implement the IProcessHostPreloadClient interface, contain a single method Preload(…) which can be used to cleanly perform these warm-up tasks.
public class MvcMusicStoreApplicationPreloadClient : IProcessHostPreloadClient { public void Preload(string[] parameters) { var service = new TopSellingAlbumsService(); HttpRuntime.Cache["TopSellingAlbums"] = service.GetTopFiveSellingAlbums(); } }
However, this class will not be loaded automatically. We need to tell IIS both about the existence of this class as well as which website it applies to. To do this, we’ll need to return to our applicationHost.config file.
First, we’ll need to add an element called
<serviceAutoStartProviders>to the
<configuration>/<system.applicationHost>node.
<serviceAutoStartProviders> <add name="MvcMusicStoreAutoStartProvider" type="MvcMusicStore.MvcMusicStoreApplicationPreloadClient, MvcMusicStore" /> </serviceAutoStartProviders>
This element registers our custom preload client with the name
MvcMusicStoreAutoStartProvider. Be sure to fully qualify the type name in the type attribute so the assembly loader can locate it.
Finally, we’ll locate the entry for our site under the sites node and register our preload client in our site’s application node.
<site name="MvcMusicStore" id="1" serverAutoStart="true"> <application path="/" applicationPool="MvcMusicStore" serviceAutoStartEnabled="true" serviceAutoStartProvider="MvcMusicStoreAutoStartProvider"> </application> . . . </site>
This is done by adding the
serviceAutoStartEnabledattribute, set to true, and by adding the
serviceAutoStartProviderattribute, set to the name of the
serviceAutoStartProviderentry we added in the previous step.
<img onclick="javascript:alert('11')''/>
<img onclick="javascript:alert('11')''/>
<img onclick="javascript:alert('11')"/>
相关文章推荐
- How To Implement Forms-Based Authentication in Your ASP.NET Application by Using C# .NET
- Debug your ASP.NET Application while Hosted on IIS
- Managing Your ASP.NET Application[3]->Using the Management API
- Debug your ASP.NET Application while Hosted on IIS
- Managing Your ASP.NET Application[4]->Using Management Tools
- Managing Your ASP.NET Application[5]->Configuring the Process Model
- How to use AspnetUpload™ in your web application
- How to get your ASP.NET application working in IE11
- Why your ASP.NET MVC Application needs its own framework
- Speed Up Your Site with the Improved View State in ASP.NET 2.0
- [ASP.NET] - 提示错误:The server has encountered an error while loading an application during the processing your request
- Test your ASP.NET MVC or WebForms Application on IIS 7 in 30 seconds
- Speeding Up FireFox When Using the ASP.NET Development Server from Localhost
- Managing Your ASP.NET Application
- Remote IIS Debugging : Debug your ASP.NET Application which is hosted on "Remote IIS Server"
- Your First ASP.NET 5 Application on a Mac
- Managing Your ASP.NET Application[2]->Retrieving Configuration
- Remote IIS Debugging : Debug your ASP.NET Application which is hosted on "Remote IIS Server"
- IIS 8.0 Asp.net warm up Auto Start & Application Initialization
- High Performance ASP.NET application (1) – Optimizing state management