13.11. Hot Rod C# クライアント
Hot Rod C# クライアントは、Hot Rod Java クライアントと Hot Rod C++ クライアントを含む Hot Rod クライアントのリストに新しく追加されました。Hot Rod C# クライアントでは、.NET ランタイムアプリケーションが Red Hat JBoss Data Grid サーバーに接続し、対話できます。
Hot Rod C# クライアントは、クラスタートポロジーとハッシュスキームを認識し、Hot Rod Java クライアントと Hot Rod C++ クライアントに類似した単一のホップでサーバー上のエントリーにアクセスできます。
Hot Rod C# クライアントは、.NET Framework が Microsoft によりサポートされる 32 ビットおよび 64 ビットのオペレーティングシステムと互換性があります。.NET Framework 4.0 は、Hot Rod C# クライアントを使用するサポート対象オペレーティングシステムとともに前提条件です。
13.11.1. Hot Rod C# クライアントのダウンロードとインストール
Hot Rod C# クライアントは、Red Hat JBoss Data Grid でダウンロード向けにパッケージされた .msi ファイル
jboss-datagrid-<version>-hotrod-dotnet-client.msi に含まれます。Hot Rod C# クライアントをインストールするには、以下の手順を実行してください。
手順13.3 Hot Rod C# クライアントのインストール
- 管理者として、Hot Rod C# .msi ファイルがダウンロードされた場所に移動します。.msi ファイルを実行してウィンドウインストーラーを起動し、Next (次へ) をクリックします。

図13.1 Hot Rod C# クライアントのセットアップの開始
- 使用許諾契約書の内容を確認します。I accept the terms in the License Agreement (使用許諾契約に同意します) チェックボックスを選択し、Next (次へ) をクリックします。

図13.2 Hot Rod C# クライアントの使用許諾契約
- デフォルトのディレクトリーを変更するには、Change... (変更...) または Next (次へ) をクリックしてデフォルトのディレクトリーにインストールします。

図13.3 Hot Rod C# クライアントの宛先フォルダー
- Finish (完了) をクリックして Hot Rod C# クライアントのインストールを完了します。

図13.4 Hot Rod C# クライアントのセットアップの完了
13.11.2. Hot Rod C# クライアントの設定
Hot Rod C# クライアントは ConfigurationBuilder を使用してプログラミングにより設定されます。クライアントが接続する必要があるホストとポートを設定します。
C# ファイルの設定例
以下の例は、ConfigurationBuilder を使用して RemoteCacheManager を設定する方法を示しています。
例13.8 C# の設定
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Infinispan.HotRod;
using Infinispan.HotRod.Config;
namespace simpleapp
{
class Program
{
static void Main(string[] args)
{
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.AddServer()
.Host(args.Length > 1 ? args[0] : "127.0.0.1")
.Port(args.Length > 2 ? int.Parse(args[1]) : 11222);
Configuration config = builder.Build();
RemoteCacheManager cacheManager = new RemoteCacheManager(config);
[...]
}
}
}13.11.3. Hot Rod C# クライアント API
RemoteCacheManager は、RemoteCache への参照を取得する開始点です。
以下の例は、サーバーからのデフォルトキャッシュの取得と基本的な複数の操作を示しています。
例13.9
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Infinispan.HotRod;
using Infinispan.HotRod.Config;
namespace simpleapp
{
class Program
{
static void Main(string[] args)
{
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.AddServer()
.Host(args.Length > 1 ? args[0] : "127.0.0.1")
.Port(args.Length > 2 ? int.Parse(args[1]) : 11222);
Configuration config = builder.Build();
RemoteCacheManager cacheManager = new RemoteCacheManager(config);
cacheManager.Start();
// Retrieve a reference to the default cache.
IRemoteCache<String, String> cache = cacheManager.GetCache<String, String>();
// Add entries.
cache.Put("key1", "value1");
cache.PutIfAbsent("key1", "anotherValue1");
cache.PutIfAbsent("key2", "value2");
cache.PutIfAbsent("key3", "value3");
// Retrive entries.
Console.WriteLine("key1 -> " + cache.Get("key1"));
// Bulk retrieve key/value pairs.
int limit = 10;
IDictionary<String, String> result = cache.GetBulk(limit);
foreach (KeyValuePair<String, String> kv in result)
{
Console.WriteLine(kv.Key + " -> " + kv.Value);
}
// Remove entries.
cache.Remove("key2");
Console.WriteLine("key2 -> " + cache.Get("key2"));
cacheManager.Stop();
}
}
}13.11.4. 相互運用性を維持するための文字列マーシャラー
文字列互換性マーシャラーを使用するには、サーバー側で互換性モードを有効にします。C# クライアント側で、以下のように CompatibilitySerializer のインスタンスを RemoteCacheManager コンストラクターに渡します。
[...]
RemoteCacheManager cacheManager = new RemoteCacheManager(new CompatibilitySerializer());
[...]
cache.Put("key", "value");
String value = cache.Get("key");
[...]注記
非文字列キー/値を格納または取得しようとすると、
HotRodClientException がスローされます。