Ok some background. I have something similar to this:
class ConnectionFactory { public IConnection Connect() { if (User.IsAuthenticated) { return InternalConnect(User.Username, null); } return null; } public IConnection Connect(string username, string password) { return InternalConnect(username, password); } private IConnection InternalConnect(string username, string password) { IConnection connection; var cacheKey = Session[CacheKeySessionKey] as string; if (!string.IsNullOrEmpty(cacheKey)) { connection = HttpCache[cacheKey] as IConnection; } if (!IsGoodConnection(connection) { connection = MakeConnection(username, password);
I am currently running a concurrency task when this Connect() is called several times and creates several IConnection for each request. I only need one. It is injected using the IoC container into various instances. MakeConnnection is very expensive as it spins the WCF channel.
My question is: how to block InternalConnect calls per session? I donβt think that blocking a request is the right way, as several requests may appear for each user. Of course, I do not want to block every call, as this will give poor performance.
I think this is a bad idea:
lock(Session.SessionID) {
Note. Overloading the username and password is what I only call when I log in.
Daniel A. White
source share