2009-12-16 75 views
1

我正在为WCF net-tcp服务创建安装项目。我遇到的一件事是我需要更改“网站 - >管理应用程序 - >高级设置 - >启用协议”。它可以通过命令行来也做:从Web项目设置中获取网站名称

%windir%\system32\inetsrv\appcmd.exe set app "[Web Site Name]/[Applicaiton Name]" /enabledProtocols:http,net.tcp 

的问题是在自定义操作,我可以得到[TARGETSITE]但它的值是“/ LM/SVC/2”(我有[TARGETVDIR]太)。问题是如何获得网站名称,或者如何使用[TARGETSITE]设置启用应用程序的协议?

回答

0

我与涉及转换metabasePath到站点名称,然后使用APPCMD结束的解决方案:

private static string GetSiteName(string metabasePath) 
{ 
    var siteIdString = metabasePath.Substring(metabasePath.LastIndexOf("/") + 1); 
    long siteId; 
    long.TryParse(siteIdString, out siteId); 

    if (siteId != 0) 
    { 
     var iisManager = new ServerManager(); 
     var config = iisManager.GetApplicationHostConfiguration(); 
     var sites = config.GetSection("system.applicationHost/sites").GetCollection(); 

     ConfigurationElement selectedSite = null; 
     foreach (var site in sites) 
     { 
      if ((long)site.GetAttribute("id").Value == siteId) 
       selectedSite = site; 
     } 

     if (selectedSite != null) 
     { 
      return selectedSite.GetAttribute("name").Value as string; 
     } 
    } 

    return null; 
} 

要使用此你会引用:

C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll 
C:\Windows\System32\inetsrv\Microsoft.Web.Management.dll 
+0

其实我发现,Web安装项目在VS2008和VS2010 Beta 2中不支持IIS7接口。他们通过IIS6兼容性扩展来部署应用程序,因此使用IIS6接口也可以安全地编写自己的扩展(直到支持IIS7) – 2010-01-15 10:18:24