Developing with IIS Express to Full IIS

Developing with IIS Express has its limitation. More you are developing and more you may have several website, web api, WCF and others system that must run together. You can increase your compilation process by only compiling and publishing the system that has changed. Visual Studio is bright enough to not recompile every libraries but it also has it pitfalls with IIS Express that suddenly have some of its references not synchronize. The result is obvious. First of all, the “start up” project will work but some of the others will not. For example, if you have a web project and a web api with the web as a startup project, you may have the web working when the web api will result to any types of error possibles.

A solution is to use IIS instead of IIS Express. This way, every compilation, only the libraries that has changed are compiled but once it is compiled, all your system will stay in a working stage (indeed it must have been in a working stage). To switch is pretty easy. Open IIS and create one website for your web project and so on. Define different ports for all your website and that’s it. Not so fast! You can have a error 500:

This configuration section cannot be used at this path. This happens when the section is locked at a parent level. Locking is either by default.

Error500ConfigurationSectionFail

This error occurs if you have not added some of IIS features. To add those features, open the Windows Feature by typing “Turn Windows features on or off” in the start menu. This open a window with some Windows feature. Select “Internet Information Services”, “World Wide Web Services” and all of them.

IISFeaturesEnabled

From there, be sure that your web application in IIS point to the web project and the other one to the web api project. Not the DLL folder, but the folder where the project is located. You just need to compile and you are up and running. If you need to debug with break point, you need to go in Visual Studio and go to Debug > Attach To Process. Click “Show Process from all user” and select the w3wp.exe process. Click attach and you are ready to debug.

IIS No Sleep (idle) and AutoStart with Values

It is important to have not only a good code but to have this one into a hosting environment that will make it looks good. One negative point that we hear with IIS is that when the application pool sleep, the user that do a query have a penalty of time on its first query. This can lead to several second of waiting. It is interesting to note that since at least 5 years that we can have IIS to auto start. It is also interesting to note that we can also make IIS not to become idle.

Changing IIS Idle Time

The first step to improve IIS performance is to remove the time out. This can be done by opening IIS, open the Application Pool you want to change the setting and to open the Advanced Setting Window. The window has a Process Model section where you will find Idle Time-out (minutes) setting. You can set this property to 0. By default, the Application Pool also recycle every few minutes. You can set it to 0 to have this one not recycle. This will cause a higher availability of IIS during the day where most client are. If your application have low period, let say that you are building for an internal website for a company, you can set a absolute time to recycle. This can be set in the same screen as before, the Advanced Setting of the Application Pool, by changing the Specific Times. For example, we can set to recycle the pool at 1 am every day.

AdvancedSettingApplicationPoolIISNoSleep

Auto Start

The auto start functionality is something that allow you to do an action when IIS start your application pool. For example, you could load in your caching system information from your database. You can also do some code that verify the availability of external webservice, etc. This require some manual editing of file on the server where reside your IIS. The file that needs to be edited is named applicationHost.config and it is located in the system folder : c:\windows\system32\inetsrv\config\.

The first section that you need to edit is the one that has your application pool name. You must ensure that you have autoStart = true and the startmode to alwaysrunning.

<add name="YourAppPoolName" 
     autoStart="true" 
     startMode="AlwaysRunning" 
     managedRuntimeVersion="v4.0">
     <processModel 
           identityType="SpecificUser" 
           userName="DefaultAppUserName"  
           idleTimeout="00:00:00" />
</add>

In the same file, the second section to find is the application path. You can find this section by searching your application name. You must have serviceAutoStartEnabled=true and to set a serviceAutoStartProvider to an unique name that we will define soon.

<application path="/YourApplicationPath" 
             applicationPool="YourAppPoolName" 
             serviceAutoStartEnabled="true" 
             serviceAutoStartProvider="YourProvider" >
             <virtualDirectory path="/" 
                               physicalPath="C:\inetpub\wwwroot\YourApplicationPath" />
 </application>

The provider name we just set needs to be defined. This can be done by adding a new section (if this one is not yet there) in the applicationHost.config file (same file that we are currently modifing the XML) under the section serviceAutoStartProviders.

<serviceAutoStartProviders>
 	    <add name="YourProvider" 
                 type="YourProvider, Full.NameSpace.To.YourProvider, version=1.0.0.0, Culture=neutral" />
</serviceAutoStartProviders>

IIS will get the DLL from your application bin folder and start running your custom code for your application when the application pool boot.

The last part is to code the custom code of your provider. This can be done by implementing a specific interface. This interface has a PreLoad method that is executed by IIS. The interface that you must implement is named IProcessHostPreloadClient.

public class YourProvider : IProcessHostPreloadClient
{
        public void Preload(string[] parameters)
        {
            
        }
}

Do not forget that this feature is available only for IIS 7.5 and above. Also, do not forget that you may have to do some additional code if you want to use your repository with your IoC container. If you are using the Http Cache that Asp.Net provides, you need to have the namespace added to your project that has the custom provider.

IIS 8.0 Application Initialization for fast startup everytime

Since IIS 8, application pool has an advanced option that allow to never sleep. Instead of being active on demand, which boot up the application pool only when someone call the website. This is problematic for performance because the first person to hit the website as a booting time that can be several seconds if not minutes. Even with the configuration that indicate to not recycle the application pool, this has the problem of the first start-up. Also, having the application pool to never reset is not a good practice. In fact, it is a good practice to reset the application pool in a schedule where your website is not in a big time window. For example, if you do not have a lot of visit at 2 am, you can specify to recycle at 2am. Still, before IIS 8.0, this was possible but the first person to hit the website will pay the price. Before going any further, you need to have installed the Application Initialization module. It is free and available by the Web Platform. A link is available if you select inside IIS the root node of your server.

IIS_Application_Initialization

With IIS 7.5 and after, it is possible to select the Start Mode. The Start mode can be “On Demand” or “Always Running”. “On demand” starts the application pool from a HTTP request. “Always running” uses the Windows Process Activation Service (WAS) to start the application pool. This was available with IIS 7.5 but IIS 8.0 does have this configuration within IIS without having to modify any applicationhost.config file. It is more convenient.
IIS_AppPool_AlwaysRunning

To enable the preloaded, a modification to the machine configuration is required. IIS 8 does not have any screen that allow you to enable it. The file is located here : C:\Windows\System32\inetsrv\config\applicationHost.config

<sites>
            <site name="Default Web Site" id="1">
                <application path="/">
                    <virtualDirectory path="/" physicalPath="%SystemDrive%\inetpub\wwwroot" />
                </application>
                <application path="/TestToDelete" applicationPool="II8NoSleep" preloadEnabled="true">
                    <virtualDirectory path="/" physicalPath="C:\inetpub\wwwroot" />
                </application>
                <bindings>
                    <binding protocol="http" bindingInformation="*:80:" />
                </bindings>
            </site>

The line 6 of the previous XML is the one you want. In fact, you need to search for you site which is under the sites XML element. Then, you see the application path that need to be modified. The “preloadedEnabled=true” is the attribute to add. This file needs to be edited with administration privileges.

At this point, any time the application pool is recycled, this one will boot it to be available.

But, since it send a fake request to your website to boot up, would it be great if you could choose which page that the WAS use? Yes and it can be configurable. This a must if you have a page that load data into the cache or to wake up manually other resources. To customize the page to be called by IIS, you need to open the web.config of your website and add a XML element.

<applicationInitialization
    remapManagedRequestsTo="Startup.htm" 
    skipManagedModules="true" >
  <add initializationPage="/default.aspx" />
</applicationInitialization>

The XML has a remapManagedRequestsTo which is the page to be displayed when the loading is executed. Instead of having blank screen with a long loading progress bar, this allow to display a static message to the visitor. The initializationPage is the page to be called by the WAS.

Installing IIS on Windows 7 Home Edition and running your Asp.Net MVC application

If you go to the administrator panel, you may not see IIS anywhere. To make sure, go to Control Panel\All Control Panel Items\Administrative Tools\

iisCongifurationPanel

If you have IIS, you will see an icon with the text “Internet Information Services (IIS) Manager”. If not, you will have to install it, which is already part of Windows 7, it’s just not enabled. To install it, go to Control Panel\All Control Panel Items\ and select Programs and Features. In the left side bar, you will see a link called “Turn Windows features on or off”. Select this open.

turningOnFeatureWindows

You have to select IIS and go into subfolder to select what you see in the following image.

IISFeatures

This will take several minutes depending of your machine. When everything is done, the control panel will now have in the administrative tools IIS.

iisOpen

So far, you have IIS, but you doesn’t have your website linked to it. If you are a developer, the best way to make it works is to link IIS to the path where your code reside. I prefer not having my code in My Document because it will cause some security problem with default configuration. In fact, the user on which IIS will run the application pool won’t have access to your file inside your My Document. Nevertheless, we can run IIS with an application pool that use your credential. First, open IIS and create a new website to which you will select the root of your web application (where the solution is). Be sure to have the website running on the good framework. I have found that it select framework 2.0 by default which should be the latest framework (4.0) if you run a MVC website. If you are using code inside your My Document, you also need to set your application pool to your account.

identityAppPool

Now, if you try to run your website, you should be able to see it. If you get this message :

IIS – this configuration section cannot be used at this path

It means that you haven’t select every options in the image above when installing IIS. The good new is that it’s not too late. You need to go back into the Windows Features and select the Application Development Features. If you get the error message : HTTP Error 500.19 – Internal Server Error – The requested page cannot be accessed because the related configuration data for the page is invalid. This mean that IIS doesn’t have access to the folder. This mean that you need to change the Identity of the application pool as instructed before.

An other point of failure may be that IIS is not running with all the new installed configuration. You can know this if you get the message :

Handler “ExtensionlessUrlHandler-Integrated-4.0” has a bad module “ManagedPipelineHandler” in its module list

To have IIS uses all new modules, open a Console as an administrator and execute the following line: c:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i

aspnetiisreg

The last problem that may arrive is that you see your website but without Javascript and CSS loading. You can notify this not only because your website won’t display correctly but also because Javascript and CSS errors will be marked in every browser tools.

websiteCssJsErrorAuthorization

To fix this, two things must be set. First, you have the have the Static Content to be installed. This has to be checked when installing IIS. If not, you can go in Control Panel\All Control Panel Items\ and select Programs and Features. In the left side bar, you will see a link called “Turn Windows features on or off” and under Commont Http function of IIS you will see the option.

Second, you need to execute not only the application pool under your credential but also the set the authentification of the website under yours. To do it, select Basic Settings > Connect as… > Specific user.

connectas

From here, your website should work and from any location (even your My Document folder).

Unable to launch the IIS Express Web server

This morning, I started a new project where the team use IIS Express instead of Visual Studio Web Server or IIS. For me, it’s new. Even if I have developed for several years with Visual Studio, I always used Visual Studio Web Server (Cassini) or the full IIS.

The install of IIS Express is quite simple with Microsoft Web Platform Installer and can be easily configured by Visual Studio (in the property window of the project) and from the configuration file inside the IISExpress folder of your My Document.

Unable to launch the IIS Express Web server

This happened when the project was launch. After checking if the port was blocked, check if projects were correctly compiled, I decided to remove IIS Express completly. I have to say that IIS 7.5 was installed and once the problem occured that I tried to install 8.0 over it. So, everything got uninstalled and I installed everything from scratch. I also did not use my team mate configuration file (which were edited to point to the correct path). I also installed directly IIS Express 8.0 and decided to let Visual Studio on the first launch to create the Virtual Directory with a new configuration file. From there, everything worked.

I suggest you to start uninstalling everything, remove the configuration file from you My Document folder, re-install and let Visual Studio on the first launch create everything.

Getting 404 error after publishing with .Net4.5 from Visual Studio 2012

After the migration to the new framework you can have the scenario where it works on your computer but not when publishing. Even publishing to the local IIS on the computer may work but not on a remote one.

The 404 page is shown without any more information. To solve this issue, I created a MVC3 web application and published. It works! I did the same with an MVC4 and it was also working.

The difference between the two projects was that the web.config file has different assemblies version in the config file.

    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    </compilation>

As you can see I have in the code above the version 1.0 for Helpers and Routing and this is problematic because the assembly I have in the project has been converted to the version 2. So two options : migrate everything with MVC4 and use the version 2 or use MVC3 with the version 1.

I hope that if you fall into this type of 404 error after migrating from Asp.Net 4 to 4.5 that you will think to check all references.

Kill all instance of IIS web server with a single Command Line

When developing with Visual Studio, when you attach to the process or use the inner server (Cassini) a new process is launched. This one doesn’t always stop when you stop debugging. You can have a list of process that is running and sometime this can cause problem when debugging because you are not accessing the right one.

To close those server, you can check in the task bar, at the right side, in the tray, and you will notice a little icon with a paper and a mechanism wheel. This is what you need to kill. You can right click and closing. The problem is that if you forget to do it every time, after some time you will have a lot of them.

To be quick, you can use a command line that will close them all for you.

taskkill /f /im WebDev.*

Taskkill with a lot of WebServer.exe

This command line give to the Windows the right to kill a process by forcing it (/f) to close all process name (/im) that start with WebDev.

IIS won’t start, the site binding is already took

IIS service start well, but all website sent an 404 error. This behaviour is strange because if you go into the IIS Manager you can see that the Sites property display that the Default Web Site is not started. If you try to start the Default Web Site, an error message tell you that it’s already taken by another process.

Default web site running

To know which process use the port, a DOS console need to be open and the use of netstat.exe is required.

netstat -a -n -p tcp -b

This will list the IP/PORT and under this information the name of the process.

In my case, Skype was running on 127.0.0.1:80. I had to close Skype, start the Default Web Site and then restart Skype and all was back to the normal.

To conclude, I have not idea why Skype.exe hook up the port 80 but for sure, if you want to use IIS or Apache with the port 80 while Skype is running, you have to start it after your web server.

How to enable CGI extension on IIS7 server?

I had to install Mercurial Server on IIS 7 recently. One of the prerequirement is to have Python installing on IIS has a CGI extension.

For this, you need to add an handler on IIS to map .cgi to Python. Unfortunately, this operation is not straight forward. If you try to add the Handlers Mapping for CGI you will get this error message.

Add Script Map error

The specified module required by this handler is not in the modules list. If you are adding a script map handler mapping, the IsapiModule or the CgiModule must be in the modules list.

To fix that, you need to open the Server Manager (%SystemRoot%\system32\CompMgmtLauncher.exe) and go to Roles and Web Server (IIS). From here you may have to wait few minutes and you will be able to add the Cgi extension.

Add Cgi extension to IIS server

Then, the last step is to Install. This also takes few minutes. Once done, restart IIS and you will be able to add the Http Handler for Python (or what ever CGI you need).