omkar patade
omkar patade

Reputation: 1591

Start/Stop BizTalk Send port in C# with WMI

Is it possible to start/stop the send the port using MSBTS_SendPort class through C#?

I have got the port details and the status using Windows WMI and trying to Start/Stop the port

  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 documentation suggest there is an method as MSBTS_SendPort.Stop to stop the port. Is it possible?

Upvotes: 1

Views: 260

Answers (1)

Paul G
Paul G

Reputation: 1229

You can invoke the "stop" or "start" method in your foreach loop while iterating through the instances.

        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[]{ });
         
        }

Upvotes: 1

Related Questions