views:

18

answers:

1

Dear Gurus.

Note Pls do not tell me regarding alternative to custom session, Pls answer only relative to the Pattern

Scenario

I have Done Custom Session Management in my application(WCF Service)

for this I have a Dictionary shared to all thread. When a specific function Gets called I add a New Session and Issue SessionId to the client so it can use that sessionId for rest of his calls until it calls another specific function, which terminates this session and removes the session from the Dictionary.

Due to any reason Client may not call session terminator function so i have to implement time expiration logic so that i can remove all such sessions from the memory.

For this I added a Timer Object which calls ClearExpiredSessions function after the specific period of time. which iterates on the dictionary.

Problem:

As this dictionary gets modified every time new client comes and leaves so i can't lock the whole dictionary while iterating over it. And if i do not lock the dictionary while iteration, if dictionary gets modified from other thread while iterating, Enumerator will throw exception on MoveNext().

So can anybody tell me what kind of Design i should follow in this case. Is there any standard pattern available.

A: 

I don't see another way to do it, your clients will just have to wait while you lock and iterate over the dictionary.

On that note, it sounds like you have a problem that you haven't encountered yet... if you're not locking on access to that dictionary, and you have multiple clients accessing it, eventually you'll have a race condition between clients.

SnOrfus
I am locking on Add/Remove but not on iteration therefore facing Exception some times if collection gets modified after retrieving Enumerator. However there is a retry logic on it so the workaround is there but i was looking for a known pattern if there is any. :(
Mubashar Ahmad