.NET client and ASP.NET SessionStateProvider - important migration notes

I recently switched over from the older Membase namspaced .NET client and the memcached-providers .NET SessionStateProvider to the Couchbase .NET client and couchbase-aspnet. I ran into a few hiccups that might be useful for others and should maybe be mentioned in documentation somewhere.
First of all, Couchbase.Configuration.CouchbaseClientSection requires you leave the bucket name off of the URI.
What would formerly be:

<servers bucket="SessionState">
     <add uri="http://localhost:8091/pools/SessionState" />
</servers>

becomes:

<servers bucket="SessionState">
     <add uri="http://localhost:8091/pools" />
</servers>

The next thing is that there's an undocumented "section" config attribute for the custom SessionStateProvider.

<sessionState customProvider="Couchbase" mode="Custom" timeout="500">
    <providers>
        <add name="Couchbase" type="Couchbase.AspNet.SessionState.CouchbaseSessionStateProvider, Couchbase.AspNet" section="couchbaseSessionState" />
    </providers>
</sessionState>

The above config allows you to specify the config section that will apply to the SessionState. This likely doesn't matter if you just have the default "couchbase" config section, but if you're using multiple config sections for multiple buckets then this is invaluable.

2 Answers

« Back to question.

Hello,

Great to hear that you're using the Couchbase ASP.NET SessionState provider! You are correct that you should bootstrap off of /pools. Also, there's more information about multiple client configs in the development wiki at
http://www.couchbase.com/wiki/display/couchbase/Couchbase+.NET+Client+Li...

Thanks for sharing your experinces...

-- John

Thanks, the issue wasn't the client configs, but rather specifying which config to use for the SessionState provider. Perhaps the "section" attribute should be documented in couchbase-aspnet/README.md at the very least?

I see now. Thanks for the suggestion, I've updated the README @https://github.com/couchbaselabs/couchbase-aspnet/blob/master/README.md

« Back to question.

In my case I actually store our Couchbase configuration in a totally separate XML configuration file from the Web.config. The reason for this is because we have so much other 'app' configuration stuff in our Web.config file that it makes it too complicated to maintain separate copies for every site that is running on our code. So rather we keep the Web.config file common to all sites that run the app, and store the site specific stuff in a SiteConfig.xml file for the site.
So we have or own mechanism for reading the Couchbase configuration and it was not entirely clear how to use the library verbatim for session storage. But looking through the code you can easily toss in your own factory that implements the ICouchbaseClientFactory interface with the following session state section in the config file:

 <sessionState customProvider="Couchbase" mode="Custom">
      <providers>
        <add name="Couchbase" type="Couchbase.AspNet.SessionState.CouchbaseSessionStateProvider, Couchbase.AspNet" factory="Couchbase.AspNet.SessionState.CouchbaseClientFactory" />
      </providers>
    </sessionState>

This sample just points to the default CouchbaseClientFactory but you can replace that class with your own to be able to configure the client with your own configuration instead of expecting to get it from a section in the web.config file.

I do have another question though about using Couchbase to store session state. According to the documentation for Couchbase server in relation to storing values with expiration timestamps, the expiration of the items is done in a lazy fashion such that the items are not checked for expiration until someone tries to load that item again.

So what happens to all the sessions that eventually need to expire, but the user has gone away? Do they just keep batching up on disk in Couchbase until we eventually run out of disk space? Or is there some mechanism that Couchbase uses to periodically 'garabage collect' the items on disk to make sure any dead items eventually do get evicted from the disk store?

I believe ASP.NET handles it automatically.

How do you figure this is handled by ASP.NET? ASP.NET has no clue about Couchbase at all, so it has hardly going to evict data from the disk or garbage collect it. The only way that this would happen is if the Couchbase server itself does the garbage collection on stale entries on disk that have expired, but nobody ever tired to read again.

ASP.NET will not handle this automatically. When ASP.NET uses the SQL Server Session state, it has a job that runs to delete expired sessions. I have a test instance set up using Cloudbase as the backing store for session, and even though the timeout is set to "1" in the web.config, I'm not seeing my session value expire. Am I doing something wrong or is that attribute not supported? Does an external service need to be written to handle session expirations?

You should see any items marked to expire disappear after an hour as there is a background job that cleans up expired records.

"Items that have expired but have not been requested will be removed every 60 minutes by default by an automatic maintenance process."

http://www.couchbase.com/docs/couchbase-manual-1.8/couchbase-developing-...

Hello,
I'm trying to get the SessionStateProvider configured but I'm getting an null reference error in the webconfig here:

I downloaded the ASPNetSessionProvider zip, but there isn't a Couchbase.AspNet dll in it. I compiled the sample application, which did create the Couchbase.AspNet.dll and I tried to use that, but I still get ther null reference error. Here are the different web.config sections I have configured:

<configSections>
      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
      <section name="couchbase-caching" type="Couchbase.Configuration.CouchbaseClientSection, Couchbase"/>
  </configSections>
 
 
    <couchbase>
        <servers bucket="default" bucketPassword="">
            <add uri="http://127.0.0.1:8091/pools/default"/>
        </servers>
    </couchbase>
 
 
    <sessionState customProvider="Couchbase" mode="Custom">
          <providers>
              <add name="Couchbase" type="Couchbase.AspNet.SessionState.CouchbaseSessionStateProvider, Couchbase.AspNet" />
          </providers>
      </sessionState>

What am I missing? Thanks!!

Where did you download it from? I also have a fork of this project with lots of bug fixes and some new features. You would have to compile it, but all the source is on GitHub. I have submitted my changes to Couchbase but I am not sure if they have accepted any of them into their tree yet:

https://github.com/kendallb/couchbase-aspnet

Thanks so much for your reply! I downloaded both the version off of the couchbase blog post and your fork. I'm having the same issue with both. I have been having the hardest time getting any out of process session state working. Does my config look correct? I really need to get this going....thank you very much for your help!!!

Looks like you have the section name wrong in the config sections. It should be just couchbase and not couchbase-caching. Otherwise you need to rename the secton you defined for couchbase and pass in the custom section name.