使用 MSDeploy API 获取 Web 服务器的依赖项

发布于 2024-10-21 21:07:21 字数 350 浏览 5 评论 0 原文

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

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

msdeploy.exe -verb:getDependencies -source:webServer

我尝试过 文档,但我没有运气。有人能指出我正确的方向吗?

I'm just getting to grips with the C# API for MSDeploy (Microsoft.Web.Deployment.dll), but I'm struggling to find a way that I can determine the dependencies for a given web server.

Basically, I'd like the C# equivalent of the following MSDeploy command line call:

msdeploy.exe -verb:getDependencies -source:webServer

I've tried the documentation, but I've had no luck. Can anybody point me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪是无色的血 2024-10-28 21:07:21

检查了 Reflector 中的 MSDeploy 可执行文件后,发现 API 并未公开 getDependency 操作(该方法是内部方法)。

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

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;
    }

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

Having examined the MSDeploy executable in Reflector, it seems that the getDependencies operation is not exposed by the API (the method is internal).

So instead I've had to fall back on calling out to the command line, and processing the results:

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;
    }

Hopefully this is useful to anybody else that ever tries to do the same thing!

淡看悲欢离合 2024-10-28 21:07:21

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

当“getDependency”用于methodName时,该方法返回一个XPathNavigator对象:

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

There actually is a way of getting there via the public API by using DeploymentObject.Invoke(string methodName, params object[] parameters).

When "getDependencies" is used for methodName, the method returns an XPathNavigator object:

    DeploymentObject deplObj = DeploymentManager.CreateObject(DeploymentWellKnownProvider.WebServer, String.Empty);
    var result = deplObj.Invoke("getDependencies") as XPathNavigator;
    var xml = XDocument.Parse(result.InnerXml);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文