CSLA .NET

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

Business Rules - Determining Async for Clients, Sync for ASP.NET ?

Answered (Not Verified) This post has 0 verified answers | 1 Reply | 0 Followers

Not Ranked
4 Posts
SMILEMan posted on Wed, Oct 12 2011 1:51 PM

On page 105 of Rocky's Using CSLA 4: Creating Business Objects, a code sample is given to set IsAsync based on whether the code is on the Client or ASP.NET (ie want async server access in business rules on smart clients (Silverlight, WPF, Metro, etc but not code on  ASP.NET servers):

#if SILVERLIGHT
    IsAysnce = true;
#else
    if (HttpContext.Current != null)
        IsAsync = false;
    else
        IsAsync = true;
#endif

Is there a way without using HttpContext which is in the System.Web.dll and is not available in the .NET 4.0 Client Profile.   Would prefer not to deploy the full .NET Framework to all users just for this one feature.

Ray.

All Replies

Top 10 Contributor
1,770 Posts

A couple of ways that I use (and I mainly do rich client development):

  1. ApplicationContext.ExecutionLocation  
  2. ApplicationContext.WebContextManager 
  3. ApplicationContext.ContextManager.GetType()
  4. Custom

1. ApplicationContext.ExecutionLocation == 

  • EcxecutionLocations.Client when on the client side of DataPortal (including the "logical serverside") when using LocalPortal.
  • EcxecutionLocations.Server when on the Server side of the DataPortal.

2. ApplicationContext.WebContextManager  (new in Csla 4.2)

  • If ApplicationContext.WebContextManager is not null then you have Csla.Web in your binaries folder (and supposedly in a WebApp).
    This was added in 4.2 to support background threads and switching back to use HttpContext when worker has completed. 

3. ApplicationContext.ContextManager.GetType().FullName == "Csla.Web.ApplicationContextManager"

  • When running in a web application. NOTE: This test will fail if your app uses background threads on the web application. (see above)

4. Custom

Add code in your bootstrapper/startup of app to set your own context variable indication whether on server or client.

var type = Type.GetType("Csla.Web.CslaDataSource, Csla.Web");
if (type == null)
{
    // we are running  on a client
}
else
{
    // we now have Csla.Web in the binaries folder and are running a web app
}

I'd probable create my own Context like this:

  public static class MyContext
  {
    static MyContext()
    {
      System.Type serverType = null;
      try
      {
        serverType = Type.GetType("Csla.Web.CslaDataSource, Csla.Web");
      }
      catch (Exception) { }

      if ((ApplicationContext.ExecutionLocation == ApplicationContext.ExecutionLocations.Server) ||
          (serverType != null))
      {
        IsServer = true;
      }
    }


    public static bool IsServer { get; private set; }
  }

Jonny Bekkum, Norway CslaContrib Coordinator

Page 1 of 1 (2 items) | RSS

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