Windows故障转移群集API仍然有效吗?

问题描述 投票:0回答:1

我正在寻找部署选项。 2个Windows故障转移群集实例下的Net Windows Service。但是我坚持使用Windows Failure Cluster API在Windows服务实例之间进行通信。我看不到任何参考。使用它。它仍然有效吗?

c# windows windows-services failover failovercluster
1个回答
0
投票

首选方法是从您的代码中调用Powershell Cmdlets

这是我编写的用于群集虚拟机的类。它应该给您足够的基础,以便您可以到达想要去的地方。

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;


public static class FailoverClustering
{
    public static List<string> GetClusterNode(string cluster)
    {
        List<string> nodes = new List<string>();

        PowerShell powerShell = PowerShell.Create();

        powerShell.AddCommand("Get-ClusterNode");
        powerShell.AddParameter("Cluster", cluster);
        foreach (PSObject result in powerShell.Invoke())
            nodes.Add(result.Members["Name"].Value.ToString());

        if (powerShell.Streams.Error.Count > 0)
            throw new Exception(powerShell.Streams.Error[0].Exception.Message);

        powerShell.Dispose();

        return nodes;
    }

    public static ClusteredVirtualMachine AddClusterVirtualMachineRole(string cluster, string virtualMachine)
    {
        PowerShell powerShell = PowerShell.Create();

        powerShell.AddCommand("Add-ClusterVirtualMachineRole");
        powerShell.AddParameter("Cluster", cluster);
        powerShell.AddParameter("VirtualMachine", virtualMachine);

        Collection<PSObject> result = powerShell.Invoke();

        if (powerShell.Streams.Error.Count > 0)
            throw new Exception(powerShell.Streams.Error[0].Exception.Message);

        powerShell.Dispose();

        return new ClusteredVirtualMachine(
            result[0].Members["Name"].Value.ToString(),
            result[0].Members["OwnerNode"].Value.ToString(),
            result[0].Members["State"].Value.ToString()
            );
    }
}

public class ClusteredVirtualMachine
{
    public string Name { get; }
    public string OwnerNode { get; }
    public State State { get; }

    public ClusteredVirtualMachine(string name, string ownerNode, string state)
    {
        Name = name;
        OwnerNode = ownerNode;
        switch (state)
        {
            case "Offline": State = State.Offline; break;
            case "Online": State = State.Online; break;
        }
    }
}

public enum State
{
    Online,
    Offline
}
© www.soinside.com 2019 - 2024. All rights reserved.