Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

HttpContext.Current.Session is null when using PollingD... RSS

9 replies

Last post Aug 29, 2011 09:37 AM by WoooXVi

(0)
  • ken.smith

    ken.smith

    Member

    282 Points

    119 Posts

    HttpContext.Current.Session is null when using PollingDuplexServiceHostFactory

    May 25, 2009 07:02 AM | LINK

    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?

    wcf duplex Silveright 3 session state

    Ken Smith
  • shidongdong

    shidongdong

    Member

    2 Points

    2 Posts

    Re: HttpContext.Current.Session is null when using PollingDuplexServiceHostFactory

    Aug 16, 2009 12:49 PM | LINK

    I have same problem.

    Are there anyone help about this?

     

    Thanks

  • ken.smith

    ken.smith

    Member

    282 Points

    119 Posts

    Re: HttpContext.Current.Session is null when using PollingDuplexServiceHostFactory

    Aug 17, 2009 03:21 AM | LINK

    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
    Ken Smith
  • fmtp

    fmtp

    Member

    2 Points

    3 Posts

    Re: Re: HttpContext.Current.Session is null when using PollingDuplexServiceHostFactory

    Oct 27, 2009 01:28 PM | LINK

    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!

  • Igilima

    Igilima

    Member

    2 Points

    1 Post

    Re: HttpContext.Current.Session is null when using PollingDuplexServiceHostFactory

    Jan 11, 2010 07:26 PM | LINK

    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?

  • xiaoyongaz

    xiaoyongaz

    Member

    24 Points

    7 Posts

    Microsoft

    Re: HttpContext.Current.Session is null when using PollingDuplexServiceHostFactory

    Apr 17, 2010 12:56 AM | LINK

    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

    Joezhou
  • GearWorld

    GearWorld

    Participant

    1784 Points

    2078 Posts

    Re: Re: HttpContext.Current.Session is null when using PollingDuplexServiceHostFactory

    May 09, 2010 01:14 PM | LINK

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

     

  • Casimodo72

    Casimodo72

    Participant

    1054 Points

    464 Posts

    Re: HttpContext.Current.Session is null when using PollingDuplexServiceHostFactory

    May 18, 2010 01:02 PM | LINK

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

    Regards,

    Kasimier

  • GearWorld

    GearWorld

    Participant

    1784 Points

    2078 Posts

    Re: HttpContext.Current.Session is null when using PollingDuplexServiceHostFactory

    May 19, 2010 09:32 PM | LINK

    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

     

  • WoooXVi

    WoooXVi

    Member

    2 Points

    1 Post

    Re: HttpContext.Current.Session is null when using PollingDuplexServiceHostFactory

    Aug 29, 2011 09:37 AM | LINK

    T_T

    I encountered the same problem!