Common Custom EditorTask
Common_CustomEditorTask_VBNet\CustomEditorTaskWebApp_VBNet\CustomEditorTaskPage.aspx.vb
' Copyright 2007 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
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports ESRI.ArcGIS.Server

Public Partial Class CustomEditorTaskPage
  Inherits System.Web.UI.Page
  Implements ICallbackEventHandler
  #Region "Release server context for non-pooled services"
  Public m_closeOutCallback As String = ""
  Private m_callbackArg As String

  Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs)
    Session("HasNonPooledResources") = HasNonPooledResources()
  End Sub

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
     m_closeOutCallback = Page.ClientScript.GetCallbackEventReference(Page, "argument", "CloseOutResponse", "context", True)
  End Sub

  #Region "ICallbackEventHandler Members"

  Private Function GetCallbackResult() As String Implements ICallbackEventHandler.GetCallbackResult
    Return m_callbackArg
  End Function

  Private Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements ICallbackEventHandler.RaiseCallbackEvent
    m_callbackArg = eventArgument

    ' parse the callback request
    Dim keyValuePairs As String() = eventArgument.Split("&".ToCharArray())
    If keyValuePairs.Length > 0 Then
      Dim keyValue As String() = keyValuePairs(0).Split("=".ToCharArray())

      If keyValue(0) = "EventArg" AndAlso keyValue(1) = "Dispose" Then
        ' Release server context for non-pooled services               
        If CBool(Session("HasNonPooledResources")) Then
          ReleaseContext()
        End If
      End If
    End If
  End Sub

  #End Region

  Private Sub ReleaseContext()
    Response.BufferOutput = True

    ' Close out session and quit application
    Dim context As IServerContext
    Dim i As Integer = 0
    Do While i < Session.Count
      context = TryCast(Session(i), IServerContext)
      If Not context Is Nothing Then
        context.RemoveAll()
        context.ReleaseContext()
      End If
      i += 1
    Loop
    Session.RemoveAll()

    ' clear out response since client is closed or has already gone elsewhere
    Response.Clear()
    Response.End()
  End Sub

  Private Function HasNonPooledResources() As Boolean
    ' define a boolean and set it to false by default... no non-pooled resourceitems
    Dim hasNonPooledResource As Boolean = False
    ' Now go through all resources and find any non-pooled local resources
    Dim mapResource As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal = Nothing
    Dim localDataSource As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.GISDataSourceLocal = Nothing
    ' First, check the map resourceitems
    For Each mri As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem In MapResourceManager1.ResourceItems
      If Not mri Is Nothing Then
        mapResource = TryCast(mri.Resource, ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal)

        If Not mapResource Is Nothing Then
          If (Not MapResourceManager1.IsInitialized(mri)) Then
            MapResourceManager1.Initialize(mri)
          End If

          Dim localRes As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal = TryCast(mapResource, ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal)
          localDataSource = TryCast(mapResource.DataSource, ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.GISDataSourceLocal)
          If (Not localDataSource.Connection.IsServerObjectPooled(mapResource.ServerContextInfo.ServerObjectName, "MapServer")) Then
          hasNonPooledResource = True
          End If

        End If
      End If
    Next mri
    Return hasNonPooledResource
  End Function
  #End Region
End Class