【问题标题】:How to create regions on App Fabric via Powershell如何通过 Powershell 在 App Fabric 上创建区域
【发布时间】:2012-03-24 11:48:12
【问题描述】:

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

提前致谢,

【问题讨论】:

  • 链接到 .NET 中的示例? PowerShell 基于 .NET,并且可以导入 .NET 命名空间,因此从 C# 示例进行这种转换通常很简单。
  • 关于 appfabric 的区域是什么?从来没有听说过。 appfabric 缓存还是 wcf 托管?

标签: .net caching powershell appfabric


【解决方案1】:

没有用于创建/管理区域的开箱即用的 Powershell 命令行开关。

解决方案 - 写一个!

正如 Daniel Richnak 在 cmets 中所说,Powershell 是 .NET,这意味着您可以编写额外的 Powershell 命令行开关来填补空白。

命令行开关是一个继承自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));
            }
        }
    }
}

【讨论】:

  • 感谢您提供详细而详尽的答案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-08
  • 2020-11-09
相关资源
最近更新 更多