【问题标题】:Using MSDeploy API to get a web server's dependencies使用 MSDeploy API 获取 Web 服务器的依赖项
【发布时间】:2011-03-13 17:30:15
【问题描述】:

我刚刚开始掌握用于 MSDeploy (Microsoft.Web.Deployment.dll) 的 C# API,但我正在努力寻找一种方法来确定给定 Web 服务器的依赖关系。

基本上,我想要以下 MSDeploy 命令行调用的 C# 等效项:

msdeploy.exe -verb:getDependencies -source:webServer

我试过the documentation,但没有运气。谁能指出我正确的方向?

【问题讨论】:

    标签: c# iis msdeploy


    【解决方案1】:

    检查了 Reflector 中的 MSDeploy 可执行文件后,似乎 API 没有公开 getDependencies 操作(该方法是内部的)。

    因此,我不得不求助于调用命令行并处理结果:

    static void Main()
        {
            var processStartInfo = new ProcessStartInfo("msdeploy.exe")
                {
                    RedirectStandardOutput = true,
                    Arguments = "-verb:getDependencies -source:webServer -xml",
                    UseShellExecute = false
                };
    
            var process = new Process {StartInfo = processStartInfo};
            process.Start();
    
            var outputString = process.StandardOutput.ReadToEnd();
    
            var dependencies =  ParseGetDependenciesOutput(outputString);
    
        }
    
        public static GetDependenciesOutput ParseGetDependenciesOutput(string outputString)
        {
            var doc = XDocument.Parse(outputString);
            var dependencyInfo = doc.Descendants().Single(x => x.Name == "dependencyInfo");
            var result = new GetDependenciesOutput
                {
                    Dependencies = dependencyInfo.Descendants().Where(descendant => descendant.Name == "dependency"),
                    AppPoolsInUse = dependencyInfo.Descendants().Where(descendant => descendant.Name == "apppoolInUse"),
                    NativeModules = dependencyInfo.Descendants().Where(descendant => descendant.Name == "nativeModule"),
                    ManagedTypes = dependencyInfo.Descendants().Where(descendant => descendant.Name == "managedType")
                };
            return result;
        }
    
        public class GetDependenciesOutput
        {
            public IEnumerable<XElement> Dependencies;
            public IEnumerable<XElement> AppPoolsInUse;
            public IEnumerable<XElement> NativeModules;
            public IEnumerable<XElement> ManagedTypes;
        }
    

    希望这对尝试做同样事情的其他人有用!

    【讨论】:

      【解决方案2】:

      实际上有一种方法可以通过公共 API 使用 DeploymentObject.Invoke(string methodName, params object[] parameters)

      methodName使用“getDependencies”时,该方法返回一个XPathNavigator对象:

          DeploymentObject deplObj = DeploymentManager.CreateObject(DeploymentWellKnownProvider.WebServer, String.Empty);
          var result = deplObj.Invoke("getDependencies") as XPathNavigator;
          var xml = XDocument.Parse(result.InnerXml);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-10
        • 2014-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多