Reputation: 15807
From reading this I thought that a client can´t run a IsInitiating=false service method before a IsInitiating=true service method have been executed. But in our WCF service that are a singelton I can call the service methods as I like, there is no need to call the IsInitiating=true service method first? How does this really work?
I am using the IAuthorizationPolicy Evaluate where I set a temporary context like this
evaluationContext.Properties["Principal"] = userContext;
Could this be the problem?
Instead of using the IsInitiating I now have to check the messageAction and if it is not the login service method, then check so there is a stored context else throw securityexception.
Edit :
This is what the service is set to use :
InstanceContextMode = InstanceContextMode.PerCall
oncurrencyMode = ConcurrencyMode.Multiple
SessionMode = SessionMode.Required
Upvotes: 4
Views: 354
Reputation: 107267
IsInitiating
and IsTerminating
are meant for use with InstanceContextMode.PerSession
, which demarcate when per-session state on the server needs to be retained across consecutive calls, allowing the lifespan of the service instance associated with each session to be controlled. The benefit of Session mode instancing is that state can be retained on the server without e.g. needing to rehydrate state from a database, although this will eventually limit the scalability as the number of concurrent sessions will be finite (each new session creates and holds an instance until terminated, which consumes memory).
If by singleton, you mean a WCF service with InstanceContextMode.Single
instancing, then WCF session (and the IsInitiating / IsTerminating) would not be applicable, given that a single state will be retained on the server for all calls from all clients. InstanceContextMode.Single
(assumed with ConcurrencyMode = ConcurrencyMode.Multiple
) should be used with caution as thread safety will be a concern, e.g. singleton could be used for a immutable caches (e.g. static data services) or stateless calculations.
This MSDN article here explains the relationship between Instancing modes, Sessions, and Concurrency nicely
Edit
In order for WCF Sessions to retain state between Initiating
and Terminating calls
, you'll need to change the InstanceContextMode
to InstanceContextMode.PerSession
, as PerCall
will lose all state between calls (the instance object will be eligible for collection after each call, whereas with PerSession
the instance will be held until a call to an IsTerminating
method is made). You'll also need to adopt a binding
which supports sessions. There's a sample here based on the MSDN calculator service over here. One additional thing to note with InstanceContextMode.PerSession
is the need for server affinity (sticky sessions) if you are scaled out in a Farm / Cloud environment.
Upvotes: 1