2009-08-07 24 views
3

這人會需要一點點的解釋在IIS虛擬目錄...創建與C#

我要設置IIS自動地使我們的集成測試(使用華廷)可以在任何環境中運行。爲此,我想創建一個Setup和Teardown,分別創建和銷燬一個虛擬目錄。

我想到的解決方案是使用MSBuild Community Tasks在代碼中使用嘲笑的IBuildEngine自動化IIS。

但是,當我嘗試創建虛擬目錄時,出現以下錯誤代碼:0x80005008。 編輯:我刪除了從早期attemps文物,現在我得到一個IndexOutOfRangeException

我在Vista上IIS7。 這是我用來運行任務的代碼:

var buildEngine = new MockedBuildEngine(); 
buildEngine.OnError += (o, e) => Console.WriteLine("ERROR" + e.Message); 
buildEngine.OnMessage += (o, e) => Console.WriteLine("MESSAGE" + e.Message); 
buildEngine.OnWarning += (o, e) => Console.WriteLine("WARNING: " + e.Message); 

var createWebsite = new MSBuild.Community.Tasks.IIS.WebDirectoryCreate() 
        { 
         ServerName = "localhost", 
         VirtualDirectoryPhysicalPath = websiteFolder.FullName, 
         VirtualDirectoryName = "MedicatiebeheerFittests", 
         AccessRead = true, 
         AuthAnonymous = true, 
         AnonymousPasswordSync = false, 
         AuthNtlm = true, 
         EnableDefaultDoc = true, 
         BuildEngine = buildEngine 
        }; 

createWebsite.Execute(); 

有人知道這是怎麼回事?還是有人知道這樣做的更好方法?

+0

你有隔離正是你所得到的例外呢?它在你的MockedBuildEngine中嗎? – 2009-08-12 00:34:44

+0

這是來自任務本身的例外。我很確定。 – Lodewijk 2009-08-12 13:01:49

回答

6

這是我的代碼應用程序使用引用的System.DirectoryServices DLL:

using System.DirectoryServices; 
    using System.IO; 
    /// <summary> 
    /// Creates the virtual directory. 
    /// </summary> 
    /// <param name="webSite">The web site.</param> 
    /// <param name="appName">Name of the app.</param> 
    /// <param name="path">The path.</param> 
    /// <returns></returns> 
    /// <exception cref="Exception"><c>Exception</c>.</exception> 
    public static bool CreateVirtualDirectory(string webSite, string appName, string path) 
    { 
     var schema = new DirectoryEntry("IIS://" + webSite + "/Schema/AppIsolated"); 
     bool canCreate = !(schema.Properties["Syntax"].Value.ToString().ToUpper() == "BOOLEAN"); 
     schema.Dispose(); 

     if (canCreate) 
     { 
      bool pathCreated = false; 
      try 
      { 
       var admin = new DirectoryEntry("IIS://" + webSite + "/W3SVC/1/Root"); 

       //make sure folder exists 
       if (!Directory.Exists(path)) 
       { 
        Directory.CreateDirectory(path); 
        pathCreated = true; 
       } 

       //If the virtual directory already exists then delete it 
       IEnumerable<DirectoryEntry> matchingEntries = admin.Children.Cast<DirectoryEntry>().Where(v => v.Name == appName); 
       foreach (DirectoryEntry vd in matchingEntries) 
       { 
        admin.Invoke("Delete", new[] { vd.SchemaClassName, appName }); 
        admin.CommitChanges(); 
        break; 
       } 

       //Create and setup new virtual directory 
       DirectoryEntry vdir = admin.Children.Add(appName, "IIsWebVirtualDir"); 

       vdir.Properties["Path"][0] = path; 
       vdir.Properties["AppFriendlyName"][0] = appName; 
       vdir.Properties["EnableDirBrowsing"][0] = false; 
       vdir.Properties["AccessRead"][0] = true; 
       vdir.Properties["AccessExecute"][0] = true; 
       vdir.Properties["AccessWrite"][0] = false; 
       vdir.Properties["AccessScript"][0] = true; 
       vdir.Properties["AuthNTLM"][0] = true; 
       vdir.Properties["EnableDefaultDoc"][0] = true; 
       vdir.Properties["DefaultDoc"][0] = 
        "default.aspx,default.asp,default.htm"; 
       vdir.Properties["AspEnableParentPaths"][0] = true; 
       vdir.CommitChanges(); 

       //the following are acceptable params 
       //INPROC = 0, OUTPROC = 1, POOLED = 2 
       vdir.Invoke("AppCreate", 1); 

       return true; 
      } 
      catch (Exception) 
      { 
       if (pathCreated) 
        Directory.Delete(path); 
       throw; 
      } 
     } 
     return false; 
    } 
1

如何使用Dylan建議的相同方法,但直接在C#中?

請參閱:Creating Sites and Virtual Directories Using System.DirectoryServices作爲起點。

+0

這基本上是MSBuild人員如何做到的。這裏還有另一個例子:http://stackoverflow.com/questions/266026/iis-api-create-virtual-directories(看看沒有測試過的答案)。 有趣的是,有關該答案的另一個建議是使用WiX。我在我編寫的幾個安裝程序中使用了WiX IIS功能,並且它又是開源的,所以你可以在那裏看看。但我可能只是從我提到的答案開始。 – Dylan 2009-08-12 15:44:49

+0

感謝您的msdn鏈接。他們會非常有幫助! – Lodewijk 2009-08-17 10:36:31