WCF service PerSession runs like its a Singelton?
Hi,
I have a service that looks like this :
[ServiceBehavior(Name = "MyAppClientService", Namespace = "http://MyApp.ServiceContracts/2007/11", InstanceContextMode = InstanceContextMode.PerSession)]
public class MyAppClientService : IMyAppClientService
[ServiceContract(Namespace = "http://MyApp.ServiceContracts/2007/11", Name = "IMyAppClientService", SessionMode = SessionMode.Required, CallbackContract = typeof(IMyAppClientServiceCallback))]
public interface IMyAppClientService
On the service I got a singelton class like this >
public class SharedContext
{
protected Dictionary<Guid, UserContextOnService> _userContextDictionary = new Dictionary<Guid, UserContextOnService>();
public static SharedContext Instance
{
get
{
if (_instance == null)
_instance = new SharedContext();
return _instance;
}
}
}
First I logon with client1, I can see that the userContextDictionary only contains one(correct). Then I logon with client2 (while client1 is running) and I can then see that the userContextDictionary have a count of 2?
How does this work? The service is set to PerCall? Should that not mean that every session has its own instance of the service?
I am looking for a singelton service solution b开发者_如何学Gout Im curious of how this works? If the service was running in diffrent instances then thay should not be able to share data?
BestRegards
Your SharedContext
singleton is quite poor because it is not ready for multithreaded environment. Check this article it describes how to make it correctly.
What are you actually asking for? You have a service which access singleton holding some dictionary - instancing mode of your service doesn't matter. You have still single dictionary for the whole application domain because singleton is only one for all your service instances.
If you want to have concurrent access to shared data in the dictionary (it looks like you want) you should also handle some synchronization there or use ConcurrentDictionary
instead.
精彩评论