【问题标题】:Start/Stop BizTalk Send port in C# with WMI使用 WMI 在 C# 中启动/停止 BizTalk 发送端口
【发布时间】:2020-07-21 17:58:43
【问题描述】:

是否可以通过 C# 使用 MSBTS_SendPort 类启动/停止发送端口?

我已使用 Windows WMI 获取端口详细信息和状态并尝试启动/停止端口

  public bool CheckSendPorts()
    {
        bool returnValue = true;
        UserName = "";
        Password = "";
        ServerName = "testserver";
        using (ManagementObjectSearcher searcher = GetWmiSearcher(UserName,Password,ServerName, WMI_SCOPE, $"SELECT * FROM MSBTS_SendPort where Name = 'testSendPort'"))
        {
            if (searcher == null)
            {
                //WriteOutput($"No Send Ports found.", true);
                return false;
            }

            foreach (ManagementObject instanceObject in searcher.Get())
            {
                string portName = instanceObject["Name"] as string;
                uint portState = (uint)instanceObject["Status"];
                string portStatus = GetPortStatus((uint)instanceObject["Status"]);
                bool ignoreLocation = false;

                
            }
        }

        return returnValue;
    }

    internal ManagementObjectSearcher GetWmiSearcher(string username,string password,string servername, string wmiScope, string wmiQuery)
    {

        ConnectionOptions connectionOptions = new ConnectionOptions();

        connectionOptions.Authentication = AuthenticationLevel.PacketPrivacy;

        if (!string.IsNullOrEmpty(username))
        {
            connectionOptions.Username = username;
            connectionOptions.Password = password;
        }
        else
        {
            connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
        }

        ManagementScope scope = new ManagementScope($@"\\{servername}{wmiScope}");
        scope.Options = connectionOptions;

        EnumerationOptions enumOptions = new EnumerationOptions();
        enumOptions.ReturnImmediately = false;

        SelectQuery query = new SelectQuery(wmiQuery);

        return new ManagementObjectSearcher(scope, query, enumOptions);
    }

  private string GetPortStatus(uint code)
    {
        switch (code)
        {
            case 1:
                return "Bound";

            case 2:
                return "Stopped";

            case 3:
                return "Started";

            default:
                return "Unknown";
        }
    }

Microsoft 文档建议有一种方法为MSBTS_SendPort.Stop 来停止端口。有可能吗?

【问题讨论】:

  • 当您尝试该方法时会发生这种情况?
  • @Dijkgraaf 我无法使用它,因为它需要 microsoft.biztalk.explorerom 但我的系统上没有安装 biztalk

标签: c# wmi biztalk


【解决方案1】:

您可以在遍历实例时在 foreach 循环中调用“stop”或“start”方法。

        foreach (ManagementObject instanceObject in searcher.Get())
        {
            string portName = instanceObject["Name"] as string;
            uint portState = (uint)instanceObject["Status"];
            string portStatus = GetPortStatus((uint)instanceObject["Status"]);
            bool ignoreLocation = false;

            //invoke method stop with an empty object array for parameters (because this method doesn't take any parameters.
            instanceObject.InvokeMethod("stop", new object[]{ });
         
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多