Common Custom EditorTask
Common_CustomEditorTask_CSharp\CustomEditorTaskWebApp_CSharp\CustomEditorTaskPage.aspx.cs
// 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.
// 

using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CustomEditorTaskPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Page_PreRender(object sender, System.EventArgs e)
    {
        // Determine whether the MapResourceManager has non-pooled resources and store in session
       Session["HasNonPooledResources"] = HasNonPooledResources();
    }
      #region Instance Methods

    private void ReleaseContext()    {
        // Loop through session variables and release any that are server contexts
        ESRI.ArcGIS.Server.IServerContext serverContext;
        for (int i = 0; i < Session.Count; i++)
        {
            // Attempt to get a reference to the current session variable as a server context
            serverContext = Session[i] as ESRI.ArcGIS.Server.IServerContext;
            if (serverContext != null)
            {
                // If the current session variable is a server context, release it.
                serverContext.RemoveAll();
                serverContext.ReleaseContext();
            }
        }

        // 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
        ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal mapResourceLocal;
        foreach (ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem mapResourceItem in
            MapResourceManager1.ResourceItems)
        {
            mapResourceLocal = mapResourceItem.Resource as
                ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal;
            if (mapResourceLocal != null)
                mapResourceLocal.Dispose();
        }
    }

    private bool HasNonPooledResources()
    {
        // Define a boolean and set it to false by default, indicating no non-pooled resources
        bool hasNonPooledResource = false;

        // Now go through all resources and find any non-pooled local resources
        ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal mapResourceLocal = null;
        ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.GISDataSourceLocal gisDataSourceLocal = null;

        // First, check the map resourceItems
        foreach (ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem mapResourceItem in MapResourceManager1.ResourceItems)
        {
            if (mapResourceItem != null)
            {
                // Get the local ArcGIS Server map resource underlying the current resource item
                mapResourceLocal = mapResourceItem.Resource as
                    ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal;

                if (mapResourceLocal != null)
                {
                    if (!MapResourceManager1.IsInitialized(mapResourceItem))
                        MapResourceManager1.Initialize(mapResourceItem);

                    // Get the local ArcGIS Server GIS data source from the local map resource
                    gisDataSourceLocal = mapResourceLocal.DataSource as
                        ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.GISDataSourceLocal;

                    // If the server object is not pooled, set the has non-pooled resource boolean to true
                    if (!gisDataSourceLocal.Connection.IsServerObjectPooled(
                    mapResourceLocal.ServerContextInfo.ServerObjectName, "MapServer"))
                        hasNonPooledResource = true;

                }
            }
        }
        return hasNonPooledResource;
    }

    #endregion
    protected void PostbackManager1_RequestReceived(object sender, PostbackManager_CSharp.AdfRequestEventArgs args)
    {
         // Parse the request arguments
        System.Collections.Specialized.NameValueCollection requestArgs =
            ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackUtility.ParseStringIntoNameValueCollection(args.RequestArguments);

        // Check the event argument and draw or clear graphics accordingly
        switch (requestArgs["EventArg"])
        {
            case "Dispose":
                if ((bool)Session["HasNonPooledResources"])
                    ReleaseContext();
                break;
        }
    }
}