Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Query 1 - Is it possible to change "IP Address and Domain Restrictions" on IIS 7.5 without restarting IIS? Query 2 - How to invoke some batch file (or executable) every time an ASP.NET worker process is restarted?

We have an ASP.NET MVC site, that we are hosting on an IIS 7.5 server. We need to warm up the site by walking through many of the pages and functionality of the site.

The idea is to implement something like below:

  • On every re-start of ASP.NET worker process or IIS, execute a batch file (or executable).
  • In the batch file, configure IIS's "IP Address and Domain Restrictions" functionality to serve requests from localhost or 127.0.0.1 only. Deny requests from all other clients.
  • Next, in the batch file, fire HTTP requests to perform login, navigation, search, add-edit etc. functionality to perform the actual warm up.
  • Once the warm up is over, configure IIS's "IP Address and Domain Restrictions" functionality to serve requests from all clients. Ensure that this change does not trigger an IIS reset.

PS: Please note that the IIS warm up module that used to be available at http://forums.iis.net/t/1176740.aspx has been removed since quite some time.

PS: Please see http://stackoverflow.com/questions/7387123/how-to-warm-up-an-asp-net-mvc-application-on-iis-7-5/7387528#7387528 for developer focused query.

Update 1: The following code was built using IIS as per Scott's answer. Unfortunately, calling these methods restart the ASP.NET worker process. How to stop this? BTW, am not posting code for FindElement() routine, as it seems generic in nature.

    internal static void AllowOnlyLocalUsers()
    {
        using (ServerManager serverManager = new ServerManager())
        {
            Configuration config = serverManager.GetApplicationHostConfiguration();

            ConfigurationSection ipSecuritySection = config.GetSection("system.webServer/security/ipSecurity", "MyApplication");
            ipSecuritySection["allowUnlisted"] = false;

            ConfigurationElementCollection ipSecurityCollection = ipSecuritySection.GetCollection();

            ConfigurationElement addElement = ipSecurityCollection.CreateElement("add");
            addElement["ipAddress"] = @"127.0.0.1";
            addElement["allowed"] = true;
            ipSecurityCollection.Add(addElement);

            serverManager.CommitChanges();
        }
    }

    internal static void AllowAllUsers()
    {
        using (ServerManager serverManager = new ServerManager())
        {
            Configuration config = serverManager.GetApplicationHostConfiguration();

            ConfigurationSection ipSecuritySection = config.GetSection("system.webServer/security/ipSecurity", "MyApplication");
            ipSecuritySection["allowUnlisted"] = true;

            ConfigurationElementCollection ipSecurityCollection = ipSecuritySection.GetCollection();

            ConfigurationElement addElement = FindElement(ipSecurityCollection, "add", "ipAddress", @"127.0.0.1", "subnetMask", @"255.255.255.255", "domainName", @"");
            if (addElement != null)
            {
                ipSecurityCollection.Remove(addElement);
            }

            serverManager.CommitChanges();
        }
    }
share|improve this question
add comment

2 Answers

You can set IP restrictions in applicationHost.config in a location tag for your site. If you do that, then it won't cause an appdomain recycle. URL Rewrite at the global level is another option.

Week 12 and week 18 of my video series cover AppDomains and editing apphost.config.

To script it, use appcmd. The best way to do that is to start with Configuration Editor in IIS, make the change you want, then "Generate Script" from the actions pane on the right. The appcmd command will be there.

As for the 2nd question, you'll need to get a handle on your application startup within your project and have that trigger an event. An async call will allow it to start your startup script while allowing the page to continue, otherwise you'll run into a locking issue.

share|improve this answer
    
Sorry for the late reply and thank you for this solution. I tried to update the IP restrictions with code posted in Update 1 in above query, unfortunately, it re-set's my ASP.NET worker process - thus negating the warm-up. I was not able to watch the videos, but was able to hack togather the code based on your reply. Can you please let me know what I am doing wrong or how I can stop ASP.NET / IIS from restarting? –  Dhwanil Shah Sep 19 '11 at 6:57
    
Hi Dhwanil, it looks like this causes a restart even though it's applied in apphost (ip security isn't delegated by default so it always applies to apphost). That means that IIS purposefully restarts the app pool, probably to inject some security checks into it. That being the case, you may need to depend on URL Rewrite which does not cause a restart when applied. –  Scott Forsyth - MVP Sep 19 '11 at 15:26
add comment

Microsoft has released a module that does exactly what you ask for. The Application Initialization Module for IIS 7.5 improves the responsiveness of Web sites by loading the Web applications before the first request arrives.

While the application is starting the module will redirect all traffic to a html-page that you provide. So you dont have to fiddle with ip-restrictions at all.

You can specify a series of Urls that IIS will preload before accepting requests from real users. I don't think you can get a true user login expereince, but maybe you can set up simulated pages that does not require login that fulfills the same warmup you ask for?

The feature I think is most compelling is that this module also enables overlapped process recycling. The following tutorial from IIS 8.0 include a step-by-step approach on how to enable overlapped process recycling.

When IIS detects that an active worker process is being recycled, IIS does not switch active traffic over to the new recycled worker process until the new worker process finishes running all application initialization Urls in the new process. This ensures that customers browsing your website don't see application initialization pages once an application is live and running.

This IIS Application Initialization module is built into IIS 8.0, but is available for download for IIS 7.5.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.