ApplicationContext.LocalContext is empty between calls from the Silverlight Client ot the DataPortal hosted in ASP.NET

ApplicationContext.LocalContext is empty between calls from the Silverlight Client ot the DataPortal hosted in ASP.NET

Old forum URL: forums.lhotka.net/forums/t/9765.aspx


Jaans posted on Monday, November 15, 2010

I've just run into something unexpected. I'm caching an expensive resource in the ApplicationContext.LocalContext.

I don't want to transfer this between server and client.

I have some server-side code that checks if the item is in the ApplicationContext.LocalContext and if not, makes the expense and places in the local context. I expected this item to remain in the LocalContext for subsequent calls to the Data Portal from the Silverlight client, but this is not the case.

Is my expectation of it wrong?

RockfordLhotka replied on Tuesday, November 16, 2010

LocalConext exists for the length of a given user interaction.

On a smart client a user interaction is the entire length of the app's runtime.

But on a web server a user interaction is the entire length of each single, individual call. Typically measured in milliseconds.

LocalContext is not a replacement for Session, which is probably what you are actually looking for.

Jaans replied on Tuesday, November 16, 2010

Thanks for that Rocky

I have it wrong then, and what your saying does make sense: Statelessness of Http, WCF Per-Call activation, etc. -> all these things all mean "we are going let go of those resources when we're done with that request and rebuild it from scratch for the next". The contrast is a data portal host like a windows service which will keep the resources for the life of the services.

I was hoping I could write data portal host agnostic code and not have to reference System.Web from within my BusinessLogic layer in order to get to the HttpContext. Just thinking about that, could I alternatively use some Global.asax HttpApplication events to save/restore the LocalContext to and from the HttpApplication.Application state?

That way I could extend the life of the LocalContext beyond the stateless call to the life of the application (ie. App Pool)?

Jaans replied on Tuesday, November 16, 2010

OK, scratch the above.

I now reference the System.Web assembly in my BusinessLogic layer and have made my calls to access the local context go through a central point. Here I can check if we have an HttpContext and then use the Application State (Context.Application) instead of the Context.Items which seems to only live for the life ot the request anyway (will need to verify that still).

Thanks again.

Jaans replied on Tuesday, November 16, 2010

Interesting.

HttpContext.Current.Items only lives for the life of the request. Ie. LocalContext is emptied out between service calls

HttpContext.Current.Application lives for the life of the application pool *

* In the typical Silverlight scenario where the ASP.NET application hosts a WCF service the HttpContext.Current is always null, unless you set the following in the <system.serviceModel ... /> section of your web.config file.

<serviceHostingEnvironment aspNetCompatibilityEnabled="True"
 />

This may or may not be suitable for your project.

Jaans replied on Tuesday, November 16, 2010

Interesting MSDN article noting the differences of runnig WCF and ASP.NET side by side, or alternatively setting aspNetCompatibilityEnabled to true to run them together.

Also explains what to use instead of HttpContext when running side by side.

Good read for those so inclined.

http://msdn.microsoft.com/en-us/library/aa702682.aspx

swegele replied on Thursday, November 18, 2010

THANKS Jaans!!  Great article with concise and thorough content.

Sean

Copyright (c) Marimer LLC