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

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

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


SMILEMan posted on Wednesday, October 12, 2011

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.

JonnyBee replied on Wednesday, October 12, 2011

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 == 

2. ApplicationContext.WebContextManager  (new in Csla 4.2)

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

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; }
  }

Copyright (c) Marimer LLC