Session issue in WCF
-
Sunday, May 06, 2012 9:09 AM
Hi,
why it always complicate ???!!!
I have WCF, inside the WCF I have an object that include some functionality and a string token.
Once my client connect WCF create new instance for the object, the object init the token, so any other call from client this object will check its token, it works perfect when I used windows sophistication, but once I moved to security mode="none" and network service (in the IIS application), it stop work.
But as I already know, we can use the session["cccc"] option, so I tried to implement it, but it not works, why?
here what I did:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] public class MiniDB_WCFService : IMiniDB_WCFService { public MiniDB_WCFService() { //var wrapperApi = HttpContext.Current.Session["WrapperAPI"] as DAL_MiniDB_Wrraper_API ?? new DAL_MiniDB_Wrraper_API(); //this.mDAL_MiniDB_Wrraper_API = wrapperApi; if (this.mDAL_MiniDB_Wrraper_API == null) { this.mDAL_MiniDB_Wrraper_API = new DAL_MiniDB_Wrraper_API(); ; } } /// <summary> /// This handle the internal private data for each session /// </summary> /// /// private DAL_MiniDB_Wrraper_API tDAL_MiniDB_Wrraper_API { get; set; } DAL_MiniDB_Wrraper_API mDAL_MiniDB_Wrraper_API { get { if ((HttpContext.Current != null) && (HttpContext.Current.Session != null)) { var intSession = HttpContext.Current.Session["WrapperAPI"] as DAL_MiniDB_Wrraper_API; if (intSession == null) { intSession = HttpContext.Current.Session["WrapperAPI"] as DAL_MiniDB_Wrraper_API ?? new DAL_MiniDB_Wrraper_API(); } return intSession as DAL_MiniDB_Wrraper_API; } else { if (this.tDAL_MiniDB_Wrraper_API == null) { this.tDAL_MiniDB_Wrraper_API = new DAL_MiniDB_Wrraper_API(); } return tDAL_MiniDB_Wrraper_API; } } set { if ((HttpContext.Current != null) && (HttpContext.Current.Session != null)) { HttpContext.Current.Session["WrapperAPI"] = value; } else { this.tDAL_MiniDB_Wrraper_API = value; } } } public UserInfo GetCurrentUserInfo(string token) { return this.mDAL_MiniDB_Wrraper_API.GetCurrentUserInfo(token); } }And also IMiniDB_WCFService define:
[ServiceContract(SessionMode = SessionMode.Allowed)] public interface IMiniDB_WCFService { .... }
All Replies
-
Sunday, May 06, 2012 9:46 AM
Hi, be aware that IIS sessions and WCF sessions are not the same thing. When you use a session with WCF, WCF will hold the instance of the service open until you call an operation that is marked with IsTerminating=true.
Debug.Print(OperationContext.Current.SessionId) Debug.Print(HttpContext.Current.Session.SessionID)
Add the lines of code above to one of your operations and you will notice that the session id's differ.
You can use a InstanceContextMode=PerCall and still have access to the IIS session through HttpContext.
- Edited by Dragan Radovac Sunday, May 06, 2012 9:47 AM
-
Sunday, May 06, 2012 9:53 AM
Thanks a lot,
I must note that it works when access from web application, if not work when run from window client application.
Just to be sure , you asking to change it to:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
This will keep the IIS / web application session as is?, mean will work OK?, what this mean (any performance issue...?)
-
Sunday, May 06, 2012 9:56 AMit still not work, the object recreated each call, I want it to create once for each session
-
Sunday, May 06, 2012 10:10 AM
No, I was using InstanceContextMode=PerCall as an example of how WCF and IIS sessions differ and that IIS sessions are still available even though WCF is not configured to use session.
Could you please clarify what it is that you are trying to do?
Thanks
-
Sunday, May 06, 2012 10:26 AM
I have an object (DAL_MiniDB_Wrraper_API), when application connect to WCF, WCF initialize the object which connect to other service A, that service return to the object token, so any access to that service A, the object must pass the token, so the object should handle the token.
So when an application connect to WCF, an instance of the object with the token will be initialize. I want to be able to save this instance for all the calles from the application until it disconnect.
It works when connect from web application.
It works when I used windows authentication in my window application.
But remove the security to mode = none, cause each call to recreate the object.
I can't understand why the session not saved!, what should done instead of
HttpContext.Current.Session["WrapperAPI"] = value;
-
Sunday, May 06, 2012 10:32 AM
When using a WCF session, you can create a member variable on the service class and assign your value there. WCF will hold the service class open for the duration of your session and will be available to the calling application on subsequent requests.
When using HttpContext you are using the ASP.NET session. WCF and ASP.NET sessions are completly different and have nothing to do with each other
- Edited by Dragan Radovac Sunday, May 06, 2012 10:35 AM
-
Sunday, May 06, 2012 10:42 AMWhat I should do?
-
Sunday, May 06, 2012 11:02 AM
Let's say that the service your client connects to is ServiceA and the service that ServiceA connects to for the token is ServiceB.
ServiceA is a session based wcf service and has a member variable called token. ServiceA has three operations.
- InitializeSessionToken (IsInitiating=True)
- GetData
- CloseSession (IsTerminating=True)
You connect to ServiceA from the client by calling InitializeSession. This operation calls ServiceB which returns a value for the token. You assign the return value from ServiceB to token member variable in ServiceA.
You then call GetData from the client. You can then access the token member variable from ServiceA (We stored it there when we called InitializeSessionToken) and make another call to ServiceB using that token.
You then Call CloseSession to have WCF dispose of ServiceA instance. Once close session is called, a new instance of the proxy must be created on the client so that a new session can be created in ServiceA
I hope this helps
-
Sunday, May 06, 2012 11:25 AM
Thanks alot, I don't understand, I did the same what the deferent between :
string token
and
DAL_MiniDB_Wrraper_API object
?
both have an instance and data, I dealing with an issue that in any function call by the client new instance is done, mean all data not availableafter each call,
-
Sunday, May 06, 2012 11:30 AM
I must note again, when using :
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None" realm=""/>
<message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default"/>
</security>it works, without any issue, but when change it to:
<security mode="none"/>
Stop Work WHY??????
-
Sunday, May 06, 2012 11:46 AMWhat binding are you using???
-
Sunday, May 06, 2012 11:51 AM
thanks for the replay, the following work since I return back the security ..
<wsHttpBinding> <binding name="WSHttpBinding_IMiniDB_WCFService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483646" maxReceivedMessageSize="2147483646" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> <readerQuotas maxDepth="32" maxStringContentLength="2147483646" maxArrayLength="2147483646" maxBytesPerRead="2147483646" maxNameTableCharCount="2147483646" /> <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" /> <security mode="Message"> <transport clientCredentialType="Windows" proxyCredentialType="None" realm=""/> <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default"/> </security> </binding> </wsHttpBinding>
-
Sunday, May 06, 2012 11:55 AM
When you set <security mode="none"> you must set <reliableSession enabled="true">. This is because wsHttpBinding uses secure sessions by default.
Please read this article before you continue:
- Marked As Answer by Markos_King Sunday, May 06, 2012 3:15 PM
-
Sunday, May 06, 2012 3:16 PM
you are the man!!!!!
"set <reliableSession enabled="true">"
Thanks a lot, it solve the problem :)

