AppFabric Cmdlet - 无法连接到本地集群
-
12-12-2019 - |
题
我一直在尝试编写一个简单的小 Cmdlet 来允许我设置/获取/删除缓存项。我遇到的问题是我无法弄清楚如何连接到本地缓存集群。
我尝试添加通常的 app.config 内容,但这似乎没有被采纳......
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="dataCacheClient" type="Microsoft.ApplicationServer.Caching.DataCacheClientSection, Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" allowLocation="true" allowDefinition="Everywhere" />
</configSections>
<dataCacheClient>
<hosts>
<host name="localhost" cachePort="22233" />
</hosts>
</dataCacheClient>
</configuration>
我宁愿根本没有那个配置。所以我真正要问的是以下 powershell 的等效 C# 代码是什么...
Use-CacheCluster
据我所知 Use-CacheCluster
如果未提供参数,则连接到本地集群
解决方案
我刚刚使用 Reflector 对 AppFabric Powershell 代码进行了一些探索,以了解其幕后工作原理。如果你打电话 Use-CacheCluster
没有参数,例如对于本地集群,代码从注册表项读取连接字符串和提供程序名称 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\AppFabric\V1.0\Configuration
. 。不幸的是,它然后使用这些值来构建一系列类(ClusterConfigElement
, CacheAdmin
和 ClusterHandler
)它们都被标记为内部,因此您不能使用它们来获取 Powershell 正在使用的当前集群上下文(因为需要一个更好的词)。
为了让你的 Cmdlet 工作,我认为你需要传入一个主机名(这将是你的集群中的服务器之一,也许你可以将其默认为本地计算机名称)和一个端口号(你可以默认它)到 22233),并使用这些值构建 DataCacheServerEndpoint
传递给你的 DataCacheFactory
例如
[Cmdlet(VerbsCommon.Set,"Value")]
public class SetValueCommand : Cmdlet
{
[Parameter]
public string Hostname { get; set; }
[Parameter]
public int PortNumber { get; set; }
[Parameter(Mandatory = true)]
public string CacheName { get; set; }
protected override void ProcessRecord()
{
base.ProcessRecord();
// Read the incoming parameters and default to the local machine and port 22233
string host = string.IsNullOrWhiteSpace(Hostname) ? Environment.MachineName : Hostname;
int port = PortNumber == 0 ? 22233 : PortNumber;
// Create an endpoint based on the parameters
DataCacheServerEndpoint endpoint = new DataCacheServerEndpoint(host, port);
// Create a config using the endpoint
DataCacheFactoryConfiguration config = new DataCacheFactoryConfiguration();
config.Servers = new List<DataCacheServerEndpoint> { endpoint };
// Create a factory using the config
DataCacheFactory factory = new DataCacheFactory(config);
// Get a reference to the cache so we can now start doing useful work...
DataCache cache = factory.GetCache(CacheName);
...
}
}
其他提示
问题是呼叫: DatacacheFactoryConfiguration Config= New DatacacheFactoryConfiguration();
内部cmdlet mothods生成错误声音,如“无法初始化datacacheactoryconfiguration”。