個人的には、データベース オプションは、現在のユーザーを保存するという概念に基づいているだけでは、やり過ぎのように思えます。実際にそれ以上を保存している場合は、データベースを使用するのが理にかなっています。しかし、単に WCF サービスの両方のインスタンスから現在のユーザーのリストが必要だと仮定すると、静的な汎用辞書のようなインメモリ ソリューションを使用します。サービスを一意に識別できる限り、一意のサービス ID をディクショナリのキーとして使用し、各キーをそのサービスのユーザー名 (または適切なユーザー データ構造) の一般的なリストとペアにするだけです。何かのようなもの:
private static Dictionary<Guid, List<string>> _currentUsers;
このディクショナリは 2 つの WCF サービス間で共有されるため、アクセスを同期する必要があります。これが例です。
public class MyWCFService : IMyWCFService
{
private static Dictionary<Guid, List<string>> _currentUsers =
new Dictionary<Guid, List<string>>();
private void AddUser(Guid serviceID, string userName)
{
// Synchronize access to the collection via the SyncRoot property.
lock (((ICollection)_currentUsers).SyncRoot)
{
// Check if the service's ID has already been added.
if (!_currentUsers.ContainsKey(serviceID))
{
_currentUsers[serviceID] = new List<string>();
}
// Make sure to only store the user name once for each service.
if (!_currentUsers[serviceID].Contains(userName))
{
_currentUsers[serviceID].Add(userName);
}
}
}
private void RemoveUser(Guid serviceID, string userName)
{
// Synchronize access to the collection via the SyncRoot property.
lock (((ICollection)_currentUsers).SyncRoot)
{
// Check if the service's ID has already been added.
if (_currentUsers.ContainsKey(serviceID))
{
// See if the user name exists.
if (_currentUsers[serviceID].Contains(userName))
{
_currentUsers[serviceID].Remove(userName);
}
}
}
}
}
特定のサービスに対してユーザーを 2 回リストしたくない場合は、 を に置き換えるのがおそらく理にかなってList<string>
いHashSet<string>
ます。