Skip to content

Commit 4cc3c62

Browse files
committed
Astn#11 added methods to let your unregister a service, or a single method from the service.
1 parent 0ab8183 commit 4cc3c62

1 file changed

Lines changed: 31 additions & 18 deletions

File tree

Json-Rpc/Handler.cs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class Handler
1515
#region Members
1616

1717
//private static Handler current;
18-
private static Dictionary<string, Handler> _sessionHandlers;
18+
private static ConcurrentDictionary<string, Handler> _sessionHandlers;
1919
private static string _defaultSessionId;
2020
#endregion
2121

@@ -25,8 +25,8 @@ static Handler()
2525
{
2626
//current = new Handler(Guid.NewGuid().ToString());
2727
_defaultSessionId = Guid.NewGuid().ToString();
28-
_sessionHandlers = new Dictionary<string, Handler>();
29-
_sessionHandlers.Add(_defaultSessionId, new Handler(_defaultSessionId));
28+
_sessionHandlers = new ConcurrentDictionary<string, Handler>();
29+
_sessionHandlers[_defaultSessionId]= new Handler(_defaultSessionId);
3030
}
3131

3232
private Handler(string sessionId)
@@ -53,19 +53,7 @@ private Handler(string sessionId)
5353
/// <returns></returns>
5454
public static Handler GetSessionHandler(string sessionId)
5555
{
56-
// double check lock. We only lock if we think we are going to add one.
57-
if (_sessionHandlers.ContainsKey(sessionId) == false)
58-
{
59-
lock (_sessionHandlers)
60-
{
61-
if (_sessionHandlers.ContainsKey(sessionId) == false)
62-
{
63-
_sessionHandlers.Add(sessionId, new Handler(sessionId));
64-
}
65-
}
66-
}
67-
68-
return _sessionHandlers[sessionId];
56+
return _sessionHandlers.GetOrAdd(sessionId, new Handler(sessionId));
6957
}
7058

7159
/// <summary>
@@ -74,13 +62,32 @@ public static Handler GetSessionHandler(string sessionId)
7462
/// <returns>The default Session Handler</returns>
7563
public static Handler GetSessionHandler()
7664
{
77-
return _sessionHandlers[_defaultSessionId];
65+
return GetSessionHandler(_defaultSessionId);
66+
}
67+
68+
/// <summary>
69+
/// Removes and clears the Handler with the specific sessionID from the registry of Handlers
70+
/// </summary>
71+
/// <param name="sessionId"></param>
72+
public static void DestroySession(string sessionId)
73+
{
74+
Handler h;
75+
_sessionHandlers.TryRemove(sessionId,out h);
76+
h.Handlers.Clear();
77+
h.MetaData.Services.Clear();
78+
}
79+
/// <summary>
80+
/// Removes and clears the current Handler from the registry of Handlers
81+
/// </summary>
82+
public void Destroy()
83+
{
84+
DestroySession(SessionId);
7885
}
7986

8087
/// <summary>
8188
/// Gets the default session handler
8289
/// </summary>
83-
public static Handler DefaultHandler { get { return _sessionHandlers[_defaultSessionId]; } }
90+
public static Handler DefaultHandler { get { return GetSessionHandler(_defaultSessionId); } }
8491

8592
/// <summary>
8693
/// The sessionID of this Handler
@@ -161,6 +168,12 @@ public bool Register(string key, Delegate handle)
161168
return result;
162169
}
163170

171+
public void UnRegister(string key)
172+
{
173+
this.Handlers.Remove(key);
174+
MetaData.Services.Remove(key);
175+
}
176+
164177
/// <summary>
165178
/// Invokes a method to handle a JsonRpc request.
166179
/// </summary>

0 commit comments

Comments
 (0)