Common Custom EditorTask
Common_CustomEditorTask_VBNet\CustomEditorTaskWebApp_VBNet\CustomEditorTaskPage.aspx.vb
' Copyright 2011 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
Public Partial Class CustomEditorTaskPage
    Inherits System.Web.UI.Page

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Determine whether the MapResourceManager has non-pooled resources and store in session
        Session("HasNonPooledResources") = HasNonPooledResources()
    End Sub

#Region "Instance Methods"

    Private Sub ReleaseContext()
        ' Loop through session variables and release any that are server contexts
        Dim serverContext As ESRI.ArcGIS.Server.IServerContext
        Dim i As Integer = 0
        Do While i < Session.Count
            ' Attempt to get a reference to the current session variable as a server context
            serverContext = TryCast(Session(i), ESRI.ArcGIS.Server.IServerContext)
            If Not serverContext Is Nothing Then
                ' If the current session variable is a server context, release it.
                serverContext.RemoveAll()
                serverContext.ReleaseContext()
            End If
            i += 1
        Loop

        ' Clear session variables.  This will force creation of a new session if the user returns
        ' to the page.  Otherwise, session variables may still be present and will cause errors.
        Session.RemoveAll()

        ' Loop through the map resources referred to by MapResourceManager1 and explicitly dispose
        ' any that are ArcGIS local map resources
        Dim mapResourceLocal As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal
        For Each mapResourceItem As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem In MapResourceManager1.ResourceItems
            mapResourceLocal = TryCast(mapResourceItem.Resource, ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal)
            If Not mapResourceLocal Is Nothing Then
                mapResourceLocal.Dispose()
            End If
        Next mapResourceItem
    End Sub

    Private Function HasNonPooledResources() As Boolean
        ' Define a boolean and set it to false by default, indicating no non-pooled resources
        Dim hasNonPooledResource As Boolean = False

        ' Now go through all resources and find any non-pooled local resources
        Dim mapResourceLocal As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal = Nothing
        Dim gisDataSourceLocal As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.GISDataSourceLocal = Nothing

        ' First, check the map resourceItems
        For Each mapResourceItem As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem In MapResourceManager1.ResourceItems
            If Not mapResourceItem Is Nothing Then
                ' Get the local ArcGIS Server map resource underlying the current resource item
                mapResourceLocal = TryCast(mapResourceItem.Resource, ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal)

                If Not mapResourceLocal Is Nothing Then
                    If (Not MapResourceManager1.IsInitialized(mapResourceItem)) Then
                        MapResourceManager1.Initialize(mapResourceItem)
                    End If

                    ' Get the local ArcGIS Server GIS data source from the local map resource
                    gisDataSourceLocal = TryCast(mapResourceLocal.DataSource, ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.GISDataSourceLocal)

                    ' If the server object is not pooled, set the has non-pooled resource boolean to true
                    If (Not gisDataSourceLocal.Connection.IsServerObjectPooled(mapResourceLocal.ServerContextInfo.ServerObjectName, "MapServer")) Then
                        hasNonPooledResource = True
                    End If

                End If
            End If
        Next mapResourceItem
        Return hasNonPooledResource
    End Function

#End Region

    Protected Sub PostbackManager2_RequestReceived(ByVal sender As Object, ByVal args As PostbackManager_VBNet.AdfRequestEventArgs) Handles PostbackManager2.RequestReceived
        ' Parse the request arguments
        Dim requestArgs As System.Collections.Specialized.NameValueCollection = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackUtility.ParseStringIntoNameValueCollection(args.RequestArguments)

        ' Check the event argument and draw or clear graphics accordingly
        Select Case requestArgs("EventArg")
            Case "Dispose"
                If CBool(Session("HasNonPooledResources")) Then
                    ReleaseContext()
                End If
        End Select
    End Sub
End Class