开发者

Modifying scriptmaps/handlermappings programmatically

I have some code to creat开发者_开发问答e a Virtual Directory programmatically.

I need to edit handler mappings of framework 2.0 to use .net 4.0 (basically run .net 2.0 code under .net 4.0 CLR).

The code below works fine under win 2003 server, but on Windows 2008 server webservicefactoryHandler2.0 is renamed as webservicefactoryHandler4.032_1245.

I don't know how to access/edit this name or infact retain the name and just change its value to \Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll from \Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll.

Dim scriptMapVals As PropertyValueCollection = VDir.Properties("ScriptMaps")
Dim objScriptMaps As ArrayList = New ArrayList()
Dim frameworkVersion As String = "4.0.30319"
Dim versionRegex As Text.RegularExpressions.Regex = New Text.RegularExpressions.Regex("(?<=\\v)\d{1}\.\d{1}\.\d{1,5}(?=\\)")
''Assuming the version will always be something like n.n.nnnnn

Dim scriptMapVal As String
For Each scriptMapVal In scriptMapVals
  If scriptMapVal.Contains("Framework") AndAlso scriptMapVal.Contains("asmx") Then
      objScriptMaps.Add(Text.RegularExpressions.Regex.Replace(scriptMapVal, versionRegex.ToString, frameworkVersion))
  Else
      objScriptMaps.Add(scriptMapVal)
  End If
Next

VDir.Properties("ScriptMaps").Value = objScriptMaps.ToArray()
VDir.CommitChanges()

Update: @kev : So basically you are saying to use different code for different version of IIS right? Well while using import "Microsoft.Web.Administration.dll" , do i need to pack it up in my build ? or this is part of all IIS7.0 installables? as Some poeple could have IIS7.0 on Windows XP.

Thanks gauls


First of all, don't use the IIS6 compatibility API to manipulate IIS7 handler mappings. Use the Microsoft.Web.Administration managed API instead.

However, you're going about this in completely the wrong way. You shouldn't be touching the handler mappings but instead change the managed runtime version for the application pool in in which the application resides.

Add a reference to:

C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll

Then in your code:

using Microsoft.Web.Administration;    
...

using (ServerManager serverManager = new ServerManager())
{
  ApplicationPool appPool = 
      serverManager.ApplicationPools.Where(a => a.Name.Equals(appPoolName))
      .Single();
  appPool.ManagedRuntimeVersion = "v2.0";
  serverManager.CommitChanges();
}

I would also recommend NOT using the ADSI compatibility API's at all to manipulate IIS7's configuration. Learn to love and embrace <applicationHost> and forget about thinking in terms of metabase objects in IIS7:

http://www.iis.net/ConfigReference/system.applicationHost

Update:

Further to your question about having code that works with both IIS6 and IIS7 I would thoroughly recommend NOT using the IIS6 compatibility layer for the convenience of having to maintain just one codebase.

The reason for this is that features of IIS7 objects such as HandlerMappings (the equivalent of Script Mappings) are handled differently. The compatibility shim creates what's known as ABO Custom Map objects which lose some of the benefits of proper HandlerMappings. Whilst they work they will create an administrative nightmare in the longer term. Trust me, I've been there.

If you need to detect which version of IIS your running then here is a handy function to do that:

public static int GetIIsMajorVersion()
{
  string regKey = @"SOFTWARE\Microsoft\InetStp";
  using(RegistryKey key = Registry.LocalMachine.OpenSubKey(regKey, true))
  {
    return Convert.ToInt32(key.GetValue("MajorVersion"));
  }
}

If the return value is 6 then it's IIS6, if it's 7 then you're on IIS 7.x.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜