【问题标题】:Creating Virtual directory in IIS with c#使用 c# 在 IIS 中创建虚拟目录
【发布时间】:2009-08-07 08:30:24
【问题描述】:

这个需要解释一下……

我想自动设置 IIS,以便我们的集成测试(使用 Watin)可以在任何环境中运行。为此,我想创建一个 Setup 和 Teardown,分别创建和销毁一个虚拟目录。

我想到的解决方案是使用 MSBuild Community Tasks 通过模拟的 IBuildEngine 在代码中自动化 IIS。

但是,当我尝试创建虚拟目录时,我收到以下错误代码:0x80005008。 编辑:我从早期尝试中删除了工件,现在我得到了 IndexOutOfRangeException

我在使用 IIS7 的 Vista 上。 这是我用来运行任务的代码:

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();

有人知道这里发生了什么吗?或者有人知道更好的方法吗?

【问题讨论】:

  • 您是否准确地隔离了您获得异常的位置?它在你的 MockedBuildEngine 中吗?
  • 这是任务本身的一个例外。我很确定。

标签: c# iis msbuild


【解决方案1】:

这是我的应用程序使用的引用 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;
    }

【讨论】:

  • 在我们的 LAN 服务器上的另一个 IIS 中执行相同操作需要什么适应。假设当前冒充用户已经拥有权限?
  • 对不起,我不知道。
【解决方案2】:

这可能有点牵强,因为它不是 C# 或 MSBuild,但是使用一个简单的 vbscript 来做呢?

http://msdn.microsoft.com/en-us/library/ms951564.aspx#autoadm_topic5

这可以在 MSBuild 任务中进行配置。

【讨论】:

  • 在我的场景中,我需要在 c# 中初始化 vbscript。不漂亮,但它可以工作......
【解决方案3】:

如果直接在 C# 中使用 Dylan 建议的相同方法会怎样?

请参阅:Creating Sites and Virtual Directories Using System.DirectoryServices 了解起点。

【讨论】:

  • 这基本上就是 MSBuild 人员的工作方式。这里还有另一个例子:stackoverflow.com/questions/266026/…(查看未测试的答案)。有趣的是,关于该答案的另一个建议是使用 WiX。我在我编写的几个安装程序中使用了 WiX IIS 功能,它也是开源的,所以你可以在那里看看。但我可能只是从我提到的答案开始。
  • 感谢 msdn 链接。他们会很有帮助!
猜你喜欢
  • 2020-07-26
  • 1970-01-01
  • 2012-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-20
  • 1970-01-01
  • 2017-01-02
相关资源
最近更新 更多