0

キャッシュアイテムを設定/取得/削除できるようにするための簡単な小さなコマンドレットを作成しようとしています。私が抱えている問題は、ローカルキャッシュクラスターに接続する方法がわからないことです。

通常の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ローカルクラスタに接続します

4

2 に答える 2

1

リフレクターを使用してAppFabricPowershellコードを調べて、内部でどのように機能するかを確認しました。Use-CacheClusterローカルクラスターなどのパラメーターを指定せずに呼び出す場合、コードはレジストリキーから接続文字列とプロバイダー名を読み取りますHKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\AppFabric\V1.0\Configuration。残念ながら、これらの値を使用して一連のクラス(、、および)を構築します。これらのクラスClusterConfigElementCacheAdminすべてClusterHandler内部としてマークされているため、Powershellが機能している現在のクラスターコンテキスト(より適切な言葉が必要)を取得するためにそれらを使用することはできません。と。

コマンドレットを機能させるには、ホスト名(クラスター内のサーバーの1つであり、デフォルトでローカルマシン名にすることができます)とポート番号(デフォルトで使用できます)を渡す必要があると思います。に22233)、そしてそれらの値を使用して、あなたのegDataCacheServerEndpointに渡すためのを構築します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);
        ...
    }
}
于 2012-07-26T12:09:04.420 に答える
0

問題は、次の呼び出しです。DataCacheFactoryConfiguration config = new DataCacheFactoryConfiguration();

Cmdlet mothods内で、「DataCacheFactoryConfigurationを初期化できません」のようなエラーが発生します。

于 2013-08-08T05:09:00.267 に答える