CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

Forum has moved

New location: CSLA .NET forum


CSLA .NET Resources:
  • CSLA .NET forum
  • CSLA .NET home page
  • Csla.ApplicationContext.GlobalContext Losing Value in MVC

    rated by 0 users
    Answered (Not Verified) This post has 0 verified answers | 3 Replies | 1 Follower

    Not Ranked
    8 Posts
    WillTartak posted on Sun, Oct 7 2012 12:42 AM

    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)
       result.Add(item.Name); 

    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 = 
    int.Parse(Csla.ApplicationContext.GlobalContext["AcAppId"].ToString());

    }

    Thanks,

    \ ^ / i l l

    All Replies

    Top 10 Contributor
    9,475 Posts

    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.

    Rocky

    Not Ranked
    8 Posts

    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?

    Thanks,

    \ ^ / i l l

     

    Top 10 Contributor
    9,475 Posts

    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.

    Rocky

    Page 1 of 1 (4 items) | RSS

    Copyright (c) 2006-2014 Marimer LLC. All rights reserved.
    Email admin@lhotka.net for support.
    Powered by Community Server (Non-Commercial Edition), by Telligent Systems