Programmatically Adding Web Service Extensions to IIS 6.0 with C#
The product I am currently working on is written in Perl and has to support IIS 6. I needed a way to programmatically set up a Web Service Extension for Perl running as a CGI module. After extensive googling, the most accepted solution was to use iisext.vbs, a script that ships with Windows Server 2003. I found the iisext.vbs solution unacceptable because double quotes are stripped from the command line. For example, 'C:\perl\bin\perl.exe "%s" %s' becomes 'C:\perl\bin\perl.exe %s %s'. Most threads on the internet simply say there's no other way to do it. Eventually, I broke down and figured something out.
These quick examples demonstrate how to add a Web Service Extension for Perl as an ISAPI module, a CGI module, and how to Allow/Prohibit existing Web Service Extensions. Though my examples use Perl, this code can be used to set up or modify any Web Service Extensions.
Add a Web Service Extension for Perl ISAPI
using (DirectoryEntry oDE = new DirectoryEntry("IIS://localhost/W3SVC")) {
PropertyValueCollection propValues = oDE.Properties["WebSvcExtRestrictionList"];
propValues.Add("1,C:\\strawberry\\perl\\bin\\perl.exe \"%s\" %s,1,%s,Strawberry Perl CGI Extension");
oDE.CommitChanges();
}
Add a Web Service Extension for Perl CGI
using (DirectoryEntry oDE = new DirectoryEntry("IIS://localhost/W3SVC")) {
PropertyValueCollection propValues = oDE.Properties["WebSvcExtRestrictionList"];
propValues.Add("1,C:\\Perl\\bin\\perlis.dll,1,,ActivePerl ISAPI Extension");
oDE.CommitChanges();
}
Allow a Web Service Extension
using (DirectoryEntry oDE = new DirectoryEntry("IIS://localhost/W3SVC")) {
PropertyValueCollection propValues = oDE.Properties["WebSvcExtRestrictionList"];
for (int i = 0; i < propValues.Count; i++) {
if (propValues[i].ToString().Contains("C:\\Perl\\bin\\perlis.dll")) {
string[] pcs = propValues[i].ToString().Split(',');
pcs[0] = "1";
propValues[i] = String.Join(",", pcs);
oDE.CommitChanges();
break;
}
}
}
Prohibit a Web Service Extension
using (DirectoryEntry oDE = new DirectoryEntry("IIS://localhost/W3SVC")) {
PropertyValueCollection propValues = oDE.Properties["WebSvcExtRestrictionList"];
for (int i = 0; i < propValues.Count; i++) {
if (propValues[i].ToString().Contains("C:\\Perl\\bin\\perlis.dll")) {
string[] pcs = propValues[i].ToString().Split(',');
pcs[0] = "0";
propValues[i] = String.Join(",", pcs);
oDE.CommitChanges();
break;
}
}
}
P4.NET for .NET 4.0
At work, we recently upgraded the product I work on to .NET 4.0.
We use Perforce for source control, and our build scripts are written in C#.
We had been using the P4.NET library to check things in and out of Perforce when making builds, but P4.NET is only available for .NET 2.0.
I did some googling, and it seemed like several people were asking for the library in .NET 4.0, but no one had built and posted it anywhere. I went ahead and did it myself and decided to make it available for download here.
Download P4.NET for .NET 4.0 DLL's
Download P4.NET for .NET 4.0 VS2010 Solution
Note: To use the sln, you need to drop the C/C++ Perforce API in P4.NET\release\1.0\ext.
Get it here: ftp://ftp.perforce.com/perforce/r10.2/bin.ntx86/p4api_vs2010_dyn_vsdebug.zip
moc.liamg@tentod4p+llewsarb.noj