ArcGIS Add dynamic data
ArcGIS_AddDynamicData_VBNet\App_Code\CustomMapHandler.vb
' 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.
' 

Imports Microsoft.VisualBasic
Imports System
Namespace MyHandler
  Public Class CustomMapHandler
    Inherits ESRI.ArcGIS.ADF.Web.UI.WebControls.MapHandler
    Private m_serverObjectStateModifier As ServerObjectStateModifier = Nothing

    ' Extend the MapHandler's constuctor to instanstiate a ServerObjectStateModifier to use for adding and
    ' removing the dynamic layer
    Public Sub New()
      MyBase.New()
      m_serverObjectStateModifier = New ServerObjectStateModifier()
    End Sub

    ' Override OnResourceInit to make adding the dynamic layer part of resource item initialization if the
    ' dynamic layer is currently added
    Protected Overrides Sub OnResourceInit(ByVal mapResourceItem As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem)
      MyBase.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 (Not System.Web.HttpContext.Current.Session("dynamicLayerAdded") Is Nothing) AndAlso (CBool(System.Web.HttpContext.Current.Session("dynamicLayerAdded"))) AndAlso (mapResourceItem.Name = CStr(System.Web.HttpContext.Current.Session("targetResourceName"))) Then
        ' 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.
                m_serverObjectStateModifier.AddLayer(TryCast(mapResourceItem.Resource, ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal), CStr(System.Web.HttpContext.Current.Session("dynamicLayerName")))
      End If

      If (Not System.Web.HttpContext.Current.Session("RendererChanged") Is Nothing) AndAlso CBool(System.Web.HttpContext.Current.Session("RendererChanged")) Then
        m_serverObjectStateModifier.ApplySimpleRenderers(TryCast(mapResourceItem.Resource, ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal), ServerObjectStateModifier.RendererAction.ApplyLast)
      End If
    End Sub

    Protected Overrides Sub OnResourcesDispose(ByVal mapResourceManager As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceManager)
      MyBase.OnResourcesDispose(mapResourceManager)

      ' Check whether the dynamic layer is currently added
      If (Not System.Web.HttpContext.Current.Session("dynamicLayerAdded") Is Nothing) AndAlso (CBool(System.Web.HttpContext.Current.Session("dynamicLayerAdded"))) Then
        ' 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.
        Dim targetResourceName As String = CStr(System.Web.HttpContext.Current.Session("targetResourceName"))
                Dim agsMapResourceLocal As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal = TryCast(mapResourceManager.ResourceItems.Find(targetResourceName).Resource, ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal)

        m_serverObjectStateModifier.RemoveLayer(agsMapResourceLocal, CStr(System.Web.HttpContext.Current.Session("dynamicLayerName")))
      End If

      If (Not System.Web.HttpContext.Current.Session("RendererChanged") Is Nothing) AndAlso CBool(System.Web.HttpContext.Current.Session("RendererChanged")) Then
        For Each mapResourceItem As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem In mapResourceManager.ResourceItems
          If TypeOf mapResourceItem.Resource Is ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal Then
            If (Not mapResourceItem.Resource.Initialized) Then
              mapResourceItem.InitializeResource()
            End If

            m_serverObjectStateModifier.ApplySimpleRenderers(TryCast(mapResourceItem.Resource, ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal), ServerObjectStateModifier.RendererAction.ApplyOriginal)
          End If
        Next mapResourceItem
      End If
    End Sub
  End Class
End Namespace