.net - 如何通过 Powershell 在 App Fabric 上创建区域

标签 .net caching powershell appfabric

我认为标题很清楚,我上网冲浪了大约一个小时,但每个页面都讨论使用 .net 动态创建区域。我确信我们有一个可以在 powershell 上执行的命令。你知道吗?

提前致谢,

最佳答案

没有现成的 Powershell commandlet 用于创建/管理区域。

解决方案 - 写一个!

正如 Daniel Richnak 在评论中所说,Powershell 本质上是 .NET,这意味着您可以编写额外的 Powershell commandlet 来填补空白。

命令行开关是一个继承自System.Management.Automation.Cmdlet 的常规类,并且它也使用System.Management.Automation.Cmdlet 属性进行修饰。使其工作只需重写 ProcessRecord 方法即可。命令行参数作为类的属性实现,并用 System.Management.Automation.Parameter 属性修饰。因此,用于创建区域的命令行开关将类似于:

using System.Management.Automation;
using Microsoft.ApplicationServer.Caching;

[Cmdlet(VerbsCommon.New, "CacheRegion")]
public class NewCacheRegion : Cmdlet
{
    [Parameter(Mandatory = true, Position = 1)]
    public string Cache { get; set; }
    [Parameter(Mandatory = true, Position = 2)]
    public string Region { get; set; }

    protected override void ProcessRecord()
    {
        base.ProcessRecord();

        DataCacheFactory factory = new DataCacheFactory();
        DataCache cache = factory.GetCache(Cache);

        try
        {
            cache.CreateRegion(Region);
        }
        catch (DataCacheException ex)
        {
            if (ex.ErrorCode == DataCacheErrorCode.RegionAlreadyExists)
            {
                Console.WriteLine(string.Format("There is already a region named {0} in the cache {1}.", Region, Cache));
            }
        }
    }
}

关于.net - 如何通过 Powershell 在 App Fabric 上创建区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9593193/

相关文章:

java - 使用哪个 Ehcache 导入?

powershell - 编写脚本,以按月从LastModificationDate属性对文件进行排序

.net - 将Dispose/Close方法编写为异步方法不是一个好主意吗?

.net - 创建仅 jquery/.NET 服务的网站是否可能或有意义?

c# - FormattedText.BuildGeometry 删除字符

c# - 为什么我的 Fluent NHibernate 子类映射会生成冗余列?

java - JCache 的 Spymemcached 的完全限定名称

c# - 使用对象作为键时.Net MemoryCache Miss

powershell - 在 psm1 模块内的导入模块上使用 'hiding' 时绕过 PowerShell 模块 '-Force'

powershell 将文件添加到 zip