HttpContext.Current.Session is null when using PollingDuplexServiceHostFactory

Answered HttpContext.Current.Session is null when using PollingDuplexServiceHostFactory

  • Monday, May 25, 2009 3:02 AM
     
     

    I'm trying to get WCF Duplex services working in Silverlight 3.0 Beta 1.  I've been following the directions at http://msdn.microsoft.com/en-us/library/dd470105(VS.96).aspx, and I've run into a bit of an issue.  One of the things I want to do is to store the user's login information in a session variable server-side, i.e., something like this:

            public bool Login(string userID, string password)
            {
                RegisteredUser user = (RegisteredUser)GetUser(userID);
                if (user != null)
                {
                    if (user.Authenticate(password))
                    {
                        HttpContext.Current.Session.Add("user", user);
                        return true;
                    }
                }
                return false;
            }
      

    That bit of code worked fine initially, once I added this line to my web.config:

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

    But then I mucked things up by enabling duplex callbacks to Silverlight (3) clients.  The problem being that when you're using duplex services under Silverlight, you need to write your own service factory.  And as best as I can tell, when the service instance is created using the sample code that MS provides (http://msdn.microsoft.com/en-us/library/dd470106(VS.96).aspx), although ASP.NET compatibility is supposedly maintained (i.e., ServiceHostingEnvironment.AspNetCompatibilityEnabled returns true), HttpContext.Current.Session is always null.

    Any suggestions on how to enable session state in this scenario, other than to write my own session provider?

All Replies

  • Sunday, August 16, 2009 8:49 AM
     
     

    I have same problem.

    Are there anyone help about this?

     

    Thanks

  • Sunday, August 16, 2009 11:21 PM
     
     

    For what it's worth, my workaround was to forget about using the built-in HTTP cache, and use my own implementation. That'll work if the only thing you're interested in is caching; it's a bit more difficult if you need to access other portions of the ASP.NET stack. Sorry I don't have a better answer -- I'd be curious if anyone else does.

    And for what it's worth, I eventually had to abandon hosting my WCF service in IIS altogether -- see this post here for details:

    http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/ade0dd72-96ae-445c-8e72-b2e625571d51
  • Tuesday, October 27, 2009 9:28 AM
     
     

    Hi,

    i have the same problem. On server-side service implementation i need to retrieve information stored in session variables, but HttpContext.Current.Session is always null. I did a simple test with a WCF service, and using PollingDuplexHttpBinding the session is always null whereas if i remove it i can access the session.

    Does anyone know if there's a way of building duplex service, using PollingDuplexHttpBinding, and be able to use the session state in the service methods?

    Thanks!

  • Monday, January 11, 2010 3:26 PM
     
     

    I'm having the same problem.  At first HttpContext.Current was null.  I figured out all the flags and settings (aspNetCompatibilityEnabled and so on) to gain access to it only to find that HttpContext.Current.Session is null.

    Is this a bug that MS will be addressing or is there some technical reason we can't access Session when using the PollingDuplexBindingElement?

  • Friday, April 16, 2010 8:56 PM
     
     

    This is expected right now because some implementation detail of polling duplex channel. Basically we will reply the client application message with an empty http 200 before the service code is invoked. When the service code is invoked, the session property of httpcontext.current becomes null because the http request has been replied. I'm still investigating possible workarounds and will update here soon.

     

    thanks

    Joe Zhou

    WCF silverlight team

  • Sunday, May 09, 2010 9:14 AM
     
     

    Mine is not just Session that is null but HttpContext.Current IS null !
    and I'm just using a sinple WCF service, OperationContract

     

  • Tuesday, May 18, 2010 9:02 AM
     
     Answered

    See http://blogs.msdn.com/silverlightws/archive/2010/05/05/the-case-of-null-httpcontext-current-session.aspx

    Regards,

    Kasimier

  • Wednesday, May 19, 2010 5:32 PM
     
     

    By adding :

     

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

    as attribute of my WCF service class and

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

     

    in the Web.Config, in System.serviceModel section it solved the problem.  Thank you

     

  • Monday, August 29, 2011 5:37 AM
     
     

    T_T

    I encountered the same problem!