c# - Windows 故障转移群集 API 仍然有效吗?
问题描述
我正在寻找部署的选项。2 个 Windows 故障转移群集实例下的 Net Windows 服务。但我坚持使用 Windows 故障集群 API 来跨 Windows 服务实例进行通信。我看不到任何参考。用它。它仍然有效吗?
解决方案
首选方法是从您的代码中调用Powershell Cmdlet 。
这是我编写的用于集群虚拟机的类。它应该为您提供足够的基础以到达您想去的地方。
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
}
推荐阅读
- java - 为什么我在使用 hibernate.hbm2ddl.auto 和 H2 时遇到异常,但使用 PostgreSQL 时它可以工作?
- reporting-services - 需要在 SSRS 中编写表达式,需要根据应用的条件过滤我的记录?
- indexing - PyQt5 - 索引使用 QStringListModel 列表的 QListView
- python - 使用传递给 Django 中 URL 的值提交后如何手动填写表单字段?
- python - 查找三角形区域会导致错误:'float' object is not callable
- spring-boot - JPA 在一列中单独搜索多个单词
- r - Create a list based on pattern in string vector
- r - 如何使用 flexsurvreg 执行模型诊断?
- html - 使用绝对位置 div 在 body 上启用滚动
- vb.net - 程序无法识别最近添加的列