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

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ESRI.ArcGIS.Server;

public partial class CustomEditorTaskPage : System.Web.UI.Page, ICallbackEventHandler
{
    #region Release server context for non-pooled services
    public string m_closeOutCallback = "";
    private string m_callbackArg;

    protected void Page_PreRender(object sender, EventArgs e)
    {
        Session["HasNonPooledResources"] = HasNonPooledResources();        
    }

    protected void Page_Load(object sender, EventArgs e)
    {       
       m_closeOutCallback = Page.ClientScript.GetCallbackEventReference(Page, "argument", "CloseOutResponse", "context", true);
    }

    #region ICallbackEventHandler Members

    string ICallbackEventHandler.GetCallbackResult()
    {
        return m_callbackArg;
    }

    void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
    {
        m_callbackArg = eventArgument;

        // parse the callback request
        string[] keyValuePairs = eventArgument.Split("&".ToCharArray());
        if (keyValuePairs.Length > 0)
        {
            string[] keyValue = keyValuePairs[0].Split("=".ToCharArray());

            if (keyValue[0] == "EventArg" && keyValue[1] == "Dispose")
            {
                // Release server context for non-pooled services               
                if ((bool)Session["HasNonPooledResources"])
                    ReleaseContext();
            }
        }
    }

    #endregion

    private void ReleaseContext()
    {
        Response.BufferOutput = true;

        // Close out session and quit application
        IServerContext context;
        for (int i = 0; i < Session.Count; i++)
        {
            context = Session[i] as IServerContext;
            if (context != null)
            {
                context.RemoveAll();
                context.ReleaseContext();
            }
        }
        Session.RemoveAll();

        // clear out response since client is closed or has already gone elsewhere
        Response.Clear();
        Response.End();
    }

    private bool HasNonPooledResources()
    {
        // define a boolean and set it to false by default... no non-pooled resourceitems
        bool hasNonPooledResource = false;
        // Now go through all resources and find any non-pooled local resources
        ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal mapResource = null;
        ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.GISDataSourceLocal localDataSource = null;
        // First, check the map resourceitems
        foreach (ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem mri in MapResourceManager1.ResourceItems)
        {
            if (mri != null)
            {
                mapResource = mri.Resource as ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal;

                if (mapResource != null)
                {
                    if (!MapResourceManager1.IsInitialized(mri))
                        MapResourceManager1.Initialize(mri);

                    ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal localRes = mapResource as ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal;
                    localDataSource = mapResource.DataSource as ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.GISDataSourceLocal;
                    if (!localDataSource.Connection.IsServerObjectPooled(mapResource.ServerContextInfo.ServerObjectName, "MapServer")) hasNonPooledResource = true;

                }
            }
        }
        return hasNonPooledResource;
    }
    #endregion
}