Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether ASP.NET 4.0 registered on IIS 7.5

Is there any reliable way how to check whether ASP.NET 4.0 registered on IIS 7.5 programmatically? I need to test it in the installer as prerequisite, before ASP.NET application installation start.

If ASP.NET 4.0 not registered on the IIS, later during the installation just installed application cannot be run and returns 500 internal server error (and it is too late to solve the problem). Instead, I want to show some warning (and hint how to solve the issue) before any installation steps started. But no reliable solution found yet.

AFAIK, registry entries reading sometimes may not work correctly. So now, I run aspnet_regiis.exe -lv to list versions (as suggested here) and parse the output. But even if .NET not registered correctly my test (falsely) succeeds, because the output is (contains version 4.0):

2.0.50727.0     C:\Windows\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll
4.0.30319.0     C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll

(Win7 32bit)

Running aspnet_regiis.exe -ir can repair it in this case.

It's similar issue as this question, but I need test it programmatically.

Do you have any ideas or experiences?

like image 301
zbynour Avatar asked Mar 28 '12 08:03

zbynour


People also ask

How do I know if ASP.NET is registered with IIS?

To check that ASP.NET is installed and registered with the correct version, enter the command aspnet_regiis.exe -lv at the command prompt.

How do I know what version of ASP.NET I am using?

From PowerShell: Launch PowerShell as an administrator. Run the command import-module servermanager. ASP.NET 4.5: Run the command get-windowsfeature Net-Framework-45-Core. The output indicates the ASP.NET 4.5 install state ("Installed" or "Available").


1 Answers

Summary: The problem described above occurs on non-server operating system (Win7). The .NET 4.0 is not registered on the IIS even if you install IIS before .NET 4.0 (and so .NET should be registered on IIS correctly). This causes unexpected problems during any ASP.NET application installation -- until aspnet_regiis.exe -ir is ran from the commandline. There is no problem with Win 2008 (i.e. when IIS installed before .NET 4.0 then .NET is registered correctly on IIS and everything works as expected).

So finally my colleague told me what possibly could be solution of the problem. I've verified that following solution works fine (also on Win7). ServerManager from Microsoft.Web.Administration namespace can be employed easily:

public static bool IsAspNetRegistered()
{
    using (var mgr = new ServerManager())
    {
        return mgr.ApplicationPools.Any(pool => pool.ManagedRuntimeVersion == "v4.0");
    }
}

In case of successful .NET registration on IIS, there is at least one application pool which runtime version is set to "v4.0" so this fact was used for the check.

Of course, if anybody deletes all application pools, this method can work incorrectly. But this is bit pathological situation I don't care. The main issue is to prevent that although everything is done according our installation recommendations, still not possible to install the application on the machine.

like image 189
zbynour Avatar answered Oct 01 '22 19:10

zbynour