ArcGIS Add dynamic data
ArcGIS_AddDynamicData_CSharp\App_Code\CustomMapHandler.cs
// Copyright 2010 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See the use restrictions.
// 

namespace MyHandler
{
    public class CustomMapHandler : ESRI.ArcGIS.ADF.Web.UI.WebControls.MapHandler
    {
        ServerObjectStateModifier _serverObjectStateModifier = null;

        // Extend the MapHandler's constuctor to instanstiate a ServerObjectStateModifier to use for adding and
        // removing the dynamic layer
        public CustomMapHandler()
            : base()
        {
            _serverObjectStateModifier = new ServerObjectStateModifier();            
        }

        // Override OnResourceInit to make adding the dynamic layer part of resource item initialization if the
        // dynamic layer is currently added
        protected override void OnResourceInit(ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem mapResourceItem)
        {
            base.OnResourceInit(mapResourceItem);

            // Check whether the dynamic layer is currently added and the current resource item being initialized is
            // the target resource item for layer addition
            if ((System.Web.HttpContext.Current.Session["dynamicLayerAdded"] != null)
            && ((bool)System.Web.HttpContext.Current.Session["dynamicLayerAdded"])
            && (mapResourceItem.Name == (string)System.Web.HttpContext.Current.Session["targetResourceName"]))
            {
                // Add the dynamic layer to the map service.  Note that we have to do this every time our custom MapHandler
                // goes through its life cycle (which will happen on any map draw operations) to make the MapHandler aware
                // of the dynamic layer's presence.  The dynamic layer is not being persisted in the map service because
                // it is being explictly removed before any server context containing it is released (via 
                // MapResourceManager.ResourcesDispose in the page and OnResourcesDispose - below - in the map handler).
                // The layer is explictly removed before server context is released because it would otherwise be visible
                // to other clients, which is what we want to avoid.
                _serverObjectStateModifier.AddLayer(mapResourceItem.Resource as 
                    ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal,
                    (string)System.Web.HttpContext.Current.Session["dynamicLayerName"]);
            }

            if ((System.Web.HttpContext.Current.Session["RendererChanged"] != null)
            && (bool)System.Web.HttpContext.Current.Session["RendererChanged"])
            {
                _serverObjectStateModifier.ApplySimpleRenderers(mapResourceItem.Resource as
                    ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal,
                    ServerObjectStateModifier.RendererAction.ApplyLast);
            }
        }

        protected override void OnResourcesDispose(ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceManager mapResourceManager)
        {
            base.OnResourcesDispose(mapResourceManager);

            // Check whether the dynamic layer is currently added
            if ((System.Web.HttpContext.Current.Session["dynamicLayerAdded"] != null) &&
            ((bool)System.Web.HttpContext.Current.Session["dynamicLayerAdded"]))
            {
                // Remove the dynamic layer from the map service.  We do this so that the layer does not remain in
                // the map service to be seen by other users.
                string targetResourceName = (string)System.Web.HttpContext.Current.Session["targetResourceName"];
                ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal agsMapResourceLocal =
                    mapResourceManager.ResourceItems.Find(targetResourceName).Resource as
                    ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal;

                _serverObjectStateModifier.RemoveLayer(agsMapResourceLocal,
                    (string)System.Web.HttpContext.Current.Session["dynamicLayerName"]);
            }

            if ((System.Web.HttpContext.Current.Session["RendererChanged"] != null)
            && (bool)System.Web.HttpContext.Current.Session["RendererChanged"])
            {
                foreach (ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem mapResourceItem in mapResourceManager.ResourceItems)
                {
                    if (mapResourceItem.Resource is ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal)
                    {
                        _serverObjectStateModifier.ApplySimpleRenderers(mapResourceItem.Resource as
                            ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal,
                            ServerObjectStateModifier.RendererAction.ApplyOriginal);
                    }
                }
            }
        }
    }
}