When using Couchbase as Session State Provider, Session_onStart does not get hit upon first session start, why?

When hitting debug and the application is starting up, Session_OnStart() will not get hit when the default browser goes to the web.

When waiting for the application to startup and then deleting all cookies and hitting refresh - (OR using other browsers) - then Session_OnStart() gets called again as usual.

Session_OnStart() always get hit when using the default InProc state provider.

Code:

		protected void Session_OnStart()
		{
			Session["SessionStart"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
			Session["RemoteHost"] = Request.ServerVariables["REMOTE_HOST"];
		}

Web.Config

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

Why?

And I also get this exception at random times when using Couchbase session provider:
----

n asynchronous module or handler completed while an asynchronous operation was still pending.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
 
Exception Details: System.InvalidOperationException: An asynchronous module or handler completed while an asynchronous operation was still pending.
 
Source Error:
 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
 
Stack Trace:
 
 
[InvalidOperationException: An asynchronous module or handler completed while an asynchronous operation was still pending.]

2 Answers

« Back to question.

Hi,

I just created a new MVC 4 application with the "Basic" template. After that I installed the NuGet package for the Couchbase provider and added configuration according to this blog post: http://blog.couchbase.com/introducing-couchbase-aspnet-sessionstate-prov...

In my Global.asax i added the following:

protected void Session_Start(object sender, EventArgs e)
{
    Session["Test"] = DateTime.Now;
}

then I added a simple HomeController with the following "Index" action:

public ActionResult Index()
{
    return new ContentResult {Content = Session["Test"].ToString()};
}

Works. To get the session to be renewed, I had to go in and delete the session documents in the bucket.

You can also test this by adding e.g. this action to the HomeController:

public ActionResult Kill()
{
    Session.Abandon();
    return RedirectToAction("Index");
}

Cheers,

//Daniel

« Back to question.

I am getting the exact same errors as rille111 in his response. It is random.

A little more information:

When testing locally with couchbase on my local machine this does not happen. It only happens on the production server.

Also if I switch to using the enyim memcached-providers on production going against the couchbase server (default bucket) it also works fine.

Any ideas?

Thanks.

Hi,
I'm in no way an expert on Couchbase but just want to share my idea on this issue since on our application we encounter somewhat similar issue.
I think the cause of the problem is that Couchbase client has an asynchronous call on this class on its constructor "DefaultHttpClient".

They call this function "client.DownloadStringAsync(baseUri);", this cause the problem since sometimes the web request finished before this call get the chance to finish executing,
The way I solve it or should I say patch it is :

private static bool _synchronouseCall = false;
if (_synchronouseCall)
{
client.DownloadString(baseUri);
_synchronouseCall = false;
}
else
{
client.DownloadStringAsync(baseUri);
}

Please check and comment on it if I'm doing correctly or you have a better way.. thanks in advance

one way to solve is to put this on config