Csla.ApplicationContext.GlobalContext Losing Value in MVC

Csla.ApplicationContext.GlobalContext Losing Value in MVC

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

WillTartak posted on Sunday, October 07, 2012

I have an MVC3 app that is generally working as expected. We use Csla 4.3.13 with it. We are running in .net 4.0.

In the Application_AcquireRequestState of the application we save a value to the Csla.ApplicationContext.GlobalContext dictionary with this code:


Csla. ApplicationContext.GlobalContext.Add("AcAppId", app.AppId); 

We then expect that value to be available to our DataPortal methods. When called by an Index method in our Controller this works as expected. Yet when triggering an ajax call, that is doing a search against a ReadOnlyList, we get a NullReferenceException for the value in the GlobalContext. The ajax call on the View calls this method on the Controller:

[ HttpPost]
public ActionResult SearchByName(string partial) {
var result = new List<string>();
var list = VendorInfoList.GetByName(partial);
foreach (var item in list)

return Json(result);

The same code on the BO works in one situation but does not in the other. Why would there be a NullReferenceException in the CslaApplicationContext.GlobalContext content in this situation? What can I do to avoid this issue?

I have tried with ClientContext as well and it has the same issue.


For completeness here is the Index method that works:

 [HasPermission(Csla.Rules.AuthorizationActions.GetObject, typeof(Vendor))]
public ActionResult Index()
var vm = new VendorVM(); 
return View(vm);


and the usage on the BO looks like this:
public static void SetAcAppId(IAppCoreAppId obj)
obj.AcAppId = 



\ ^ / i l l

RockfordLhotka replied on Sunday, October 07, 2012

Two things.

First, make sure you have Csla.Web.dll in your bin directory, or ApplicationContext won't work right.

Second, remember that web servers are designed to be stateless between client requests. The Csla.ApplicationContext values are state, so of course that state is lost between client requests.

If you want state to remain available on the web server between multiple client requests, you must use the ASP.NET Session or Application objects.

WillTartak replied on Sunday, October 07, 2012

Hi Rocky,

Thank you very much for the reply.

1. Csla.Web.dll is referenced in the MVC project, not in the Library project. Does it need to be there as well? I wouldn't think so but just checking.

2. Understood, that is why the code is in the Application_AcquireRequestState  method. At least, I understood that was the point of putting it there. Unfortunately, that method is not being triggered by the ajax call, I realized after I posted above. I'm trying to avoid using Session, any suggestions on why the AcquireRequestState is not being called? (I realize that is not your area of expertise, buy maybe you know. :) )

Any other suggestions on getting the Csla.ApplicationContext information rehydrated on an ajax call, aside from Session?


\ ^ / i l l


RockfordLhotka replied on Monday, October 08, 2012

I'm not enough of an ASP.NET expert to know the answer.

I do know that ASP.NET "optimizes" its pipeline for different types of calls, dropping a lot of events and other processing that would happen for a web page when the call is for a service. I'm pretty sure you can use attributes/settings to force ASP.NET to do the extra pipeline steps - that costs performance, but gets you more events to hook.

Copyright (c) Marimer LLC