ThinkGeo.com    |     Documentation    |     Premium Support

ShapeFileFeatureLayer.DrawingFeatures HttpContext.Current is null

I have a layer that I need to exclude some features from getting drawn in. I do this by setting up a DrawingFeatures event on the ShapeFileFeatureLayer, and looping through the FeaturesToDraw collection. I've noticed that I can only access the Session directly within the DrawingFeatures event. I can't access it through HttpContext.Current.Session due to HttpContext.Current being null. I'd like to be able to hit the session from a separate class that needs to access it through HttpContext.Current.Session.



Is this a bug?

  By the way, my map lives in a UserControl if that changes anything.

schoolLayer.DrawingFeatures += new EventHandler<drawingfeatureseventargs>(schoolLayer_DrawingFeatures);

void schoolLayer_DrawingFeatures(object sender, DrawingFeaturesEventArgs e)
{
  // works fine   
  string test = Session["Something"].ToString(); 
  // throws error because Current is null    
  string test = HttpContext.Current.Session["Something"].ToString();
  for (int i = e.FeaturesToDraw.Count - 1; i >= 0; i-- )
  {
    string label = e.FeaturesToDraw[i].ColumnValues["LABEL"];
    if (label == test)
    {
      e.FeaturesToDraw.RemoveAt(i);
    }
  }
}


To clarify what I am trying to accomplish, I am trying to be able to use a helper class to do all session related stuff. This works fine everywhere except within ShapeFileFeatureLayer’s DrawingFeatures event (schoolLayer_DrawingFeatures() above). 
 
public class SessionVariables
{
    public static string ActiveSchools
    {
        get 
        {
            if (HttpContext.Current.Session[“ActiveSchools”] == null)
            {
                return “|”;
            }
            else
            {
                return HttpContext.Current.Session[“ActiveSchools”].ToString();
            }
        }
        set { HttpContext.Current.Session[“ActiveSchools”] = value; }
    }
}


Rob, 
  
  The handler method schoolLayer_DrawingFeatures is executed in the HttpHandler, where you cannot access its session directly but can only use the session passed in by parameter. For your case, Session[“Something”] is the passed in Page’s session while HttpContext.Current.Session[“Something”] is HttpHandler’s Session, that’s why  HttpContext.Current is null but Session[“Something”] works fine in the handler method, that’s also why HttpContext.Current works fine in a normal method which is executed on the page.  
  
 I’m afraid we cannot do much about it, maybe we have to use the page session for that case. Please let me know if you have any good ideas. 
  
 Thanks, 
  
 Ben 


Aha, that explains it! This may be a shot in the dark, but is there some way to pass in and overwrite the HttpHandler’s Session, when you are passing in the Page’s Session as a parameter? 
  
 The equivalent to: HttpContext.Current.Session = Session 
  
 I tried to do just that, and realized that it is read only. But the Session object itself is also read-only, and you guys are passing that in. So maybe you are assigning to it some other way. 
  
 If not, is there another way to access the Page’s Session from my above SessionVariables class?

Rob, 
  
 We are using multi threading internally so we choose to use a readonly session, it may have some other limits too. If you don’t insist to use properties, we can use a static method and pass the current page as parameter, as following. 
  
 
Private void worldLayer_DrawingFeatures(object sender, DrawingFeaturesEventArgs e)
 {
   string b = SessionVariables.GetActiveSchools(this);
 }

public class SessionVariables
 {
 public static string GetActiveSchools(Page page)
 {
 return page.Session[“ActiveSchools”].ToString();
 }
 }
 
 so what do you think about that? Any suggestions? 
  
 Thanks, 
  
 Ben

That will work great. Thanks for the help. 
  
 Out of curiosity, is there any other way to exclude features from drawing, without the DrawingFeatures event? Something like 2.x’s Layer.DoNotDraw property. It would be useful to be able to exclude up front based on RecIDs or a column value condition.

Rob, 
  
 You can implement it by creating your own Style and do the filtering in the override DrawCore method. That’s not very difficult as well. 
  
 We didn’t include the DoNotDraw property. I remember one reason is we don’t want to store the status if possible,  another reason is we provide other ways (events and overwriting) to implement this function. There might have some other reason which is not there now. I will add it to our tracking system reminding the design team to review this property. Thanks for pointing it out. 
  
 Thanks, 
  
 Ben