我如何检查WCF是否已在IIS中注册

时间:2010-03-10 13:24:02

标签: wcf iis installer

我正在制作安装程序,并想检查WCF是否已在IIS中注册

2 个答案:

答案 0 :(得分:1)

检查IIS ScriptMaps中的.svc扩展名。为此,您可以使用Javascript,VBScript或C / C ++中实现的WIX自定义操作中的WMI。这是Javascript中的一个例子:

// IsWcf.js
// ------------------------------------------------------------------
//
// detect if WCF is installed; suitable for use within a WIX Custom Action.
//
// Copyright 2010, Cheeso.
//
// Licensed under the MS Public License
//   http://opensource.org/licenses/ms-pl.html
//
// To use this as a Custom Action in a WIX project, register it this way:
//
//   <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
//     <Fragment>
//       <Binary Id="B.JavaScript" SourceFile="CustomActions.js" />
//       <CustomAction Id="CA.DetectWcf"
//                     BinaryKey="B.JavaScript"
//                     JScriptCall="DetectWcf_CA"
//                     Execute="immediate"
//                     Return="check" />
//     </Fragment>
//   </Wix>
//
// And invoke it this way:
//
//    <InstallUISequence>
//      <Custom Action="CA.DetectWcf" After="CostFinalize" Overridable="yes">NOT Installed</Custom>
//    </InstallUISequence>
//
//
// =======================================================


// http://msdn.microsoft.com/en-us/library/aa371254(VS.85).aspx
var MsiActionStatus = {
    None             : 0,
    Ok               : 1, // success
    Cancel           : 2,
    Abort            : 3,
    Retry            : 4, // aka suspend?
    Ignore           : 5  // skip remaining actions; this is not an error.
};

var MsgKind = {
    Error            : 0x01000000,
    Warning          : 0x02000000,
    User             : 0x03000000,
    Log              : 0x04000000
};


// Format a number as hex.  Quantities over 7ffffff will be displayed properly.
function decimalToHexString(number) {
    if (number < 0)
        number = 0xFFFFFFFF + number + 1;
    return number.toString(16).toUpperCase();
}

function LogMessage(s) {
    LogMessage2(s,MsgKind.Log);
}

function LogMessage2(s,kind){
    if (typeof Session !== "undefined") {
        var record = Session.Installer.CreateRecord(0);
        record.StringData(0) = "CustomActions: " + msg;
        Session.Message(kind, record);
    }
    else {
        WScript.echo(s);
    }
}

function LogException(loc, exc) {
    var s = "Exception {" + loc + "}: 0x" + decimalToHexString(exc.number) + " : " + exc.message;
    if (typeof Session !== "undefined") {
        var record = Session.Installer.CreateRecord(0);
        record.StringData(0) = s;
        Session.Message(MsgKind.Error + Icons.Critical + Buttons.btnOkOnly, record);
    }
    else {
        LogMessage(s);
    }
}

function stringEndsWith(subject, end) {
    return (subject.match(end+"$") == end);
}

function DetectWcf_CA(website) {
    try {
        LogMessage("DetectWcf_CA() ENTER");

        if (website == null) {
            website = "W3SVC"; // default value
        }

        LogMessage("website name(" + website + ")");

        var query = ( website == "W3SVC" )
            ? "SELECT * FROM IIsWebServiceSetting"
            : "SELECT * FROM IIsWebServerSetting WHERE Name = '" + website + "'";

        var iis = GetObject("winmgmts://localhost/root/MicrosoftIISv2");

        var settings = iis.ExecQuery(query);
        LogMessage("WMI Query results : " + typeof settings);

        if ( settings == null ) {
            LogMessage("Cannot query IIS.");
            return MsiActionStatus.Abort;
        }

        var item;
        if ( settings.Count != 0 ) {
            for(var e = new Enumerator(settings); !e.atEnd(); e.moveNext()) {
                item = e.item();
            }
        }

        var scriptMaps = item.ScriptMaps.toArray();
        var isSvcMapPresent = false;
        var svcMapIndex = -1;
        for (var i=0; i<scriptMaps.length;  i++) {
            var map = scriptMaps[i];
            for(var e = new Enumerator(map.Properties_); !e.atEnd(); e.moveNext()) {
                item = e.item();
                if (item.Name == "Extensions" && item.Value == ".svc" && svcMapIndex == -1 ) {
                    svcMapIndex = i;
                }
                else if (i == svcMapIndex && item.Name == "ScriptProcessor" && stringEndsWith(item.Value,"aspnet_isapi.dll")) {
                    isSvcMapPresent = true;
                }
            }
        }

        LogMessage("DetectWcf_CA(): Is WCF Present?: " + isSvcMapPresent);

        // works in WIX Custom Action only:
        if (typeof Session !== "undefined") {
            Session.Property("ISWCF")         = (isSvcMapPresent) ? "1" : "0";
        }

        LogMessage("DetectWcf_CA() EXIT (Ok)");
    }
    catch (exc1) {
        LogException("DetectWcf_CA", exc1);
        return MsiActionStatus.Abort;
    }
    return MsiActionStatus.Ok;
}


// actually run it, only if being invoked outside of WIX
if (typeof Session == "undefined") {
    DetectWcf_CA("W3SVC") ;
}

答案 1 :(得分:0)