locked
ASP.NET Session Timeout vs. WCF Session Timeout RRS feed

  • Question

  • Hello,

      I have a WCF Service using basicHTTPBinding that I enabled aspNetCompatibilityEnabled so I could use some traditional ASP.NET Features like sessions (with cookies - not the WCF Sessions grouping messages).  For example between each message to the service, instead of looking up a users name we're caching it in an ASP.NET session using a cookie that's passed back and forth from the client to the server in each message the WCF service handles.

      I have a method "logout" which ends the conversation between service and client and ends the ASP.NET session.  When this gets called the service does the final cleanup I need to do which is some database work.  There is always a chance that users don't call "logout".  Is there any way in a WCF Service using basicHttpBinding to detect when the ASP.NET Session time out like how you can do so in a traditional ASP.NET application in the global.asax file?  This way I can perform the database work on ASP.NET Session time out.

    Thanks.

    Wednesday, August 1, 2012 12:22 PM

Answers

  • I ended up figuring this out - if anyone else has this same issue and are using basicHttpBinding, the following can be done:

    1) On the service's web.config file, set the aspNetCompatibilityEnabled = true

      <system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
      </system.serviceModel>

    2) Also in the service's web.config file, enable session state.

      <system.web>
        <sessionState cookieless="false" mode="InProc" timeout="10" />
      </system.web>

    3) On the web service's project, add a global.asax file (just like if it was an ASP.NET application)  perform the cleanup code in Session_Start and Session_End.

    • Marked as answer by Ryan_Ha Monday, August 6, 2012 8:29 PM
    Monday, August 6, 2012 8:29 PM

All replies

  • WCF session is different than ASPNet session. ASPnet session is server initiated whether as WCF session are maintained from client.

    Please have a look at this  MSDN Post. Also in wcf you need a binding that supports session. basichttpbinding doesnt support session.


    Tanvir Huda Application Architect/Consultant http://thetechnocrate.wordpress.com/

    Thursday, August 2, 2012 12:12 AM
  • Thanks Tanvir, since we need to have our service be cross compatible between different operating systems, we need to use basicHttpBinding, so we don't really have the ability to do a WCF session like if we could use wsHttpBinding so our solution to storing data for that "session" was to use ASP.NET sessions in IIS.  I just need to find a way to do cleanup when the ASP.NET session ends or times out in my WCF Service.  Not sure if there is a way to do that?

    Thanks.

    Thursday, August 2, 2012 12:07 PM
  • I ended up figuring this out - if anyone else has this same issue and are using basicHttpBinding, the following can be done:

    1) On the service's web.config file, set the aspNetCompatibilityEnabled = true

      <system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
      </system.serviceModel>

    2) Also in the service's web.config file, enable session state.

      <system.web>
        <sessionState cookieless="false" mode="InProc" timeout="10" />
      </system.web>

    3) On the web service's project, add a global.asax file (just like if it was an ASP.NET application)  perform the cleanup code in Session_Start and Session_End.

    • Marked as answer by Ryan_Ha Monday, August 6, 2012 8:29 PM
    Monday, August 6, 2012 8:29 PM