Network Analyst routing
ArcGIS_Routing_CSharp\App_Code\ServerObjectStateModifier.cs
// 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.
// 

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;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using System.IO;
using ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer;
using ESRI.ArcGIS.ADF.Web.UI.WebControls;
using ESRI.ArcGIS.NetworkAnalyst;
using ESRI.ArcGIS.Server.Web.NetworkAnalyst;

public class ServerObjectStateModifier
{
    public void ApplySessionNAContext(GISResourceItem resource)
    {
        ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal gisresource = (ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal)resource.Resource;
        IServerContext mapContext = gisresource.ServerContextInfo.ServerContext;

        //Get layer id - was stored in SolveRoute method
        object id = System.Web.HttpContext.Current.Session["RouteLayerID"];
        if (id == null)
            return;

        int layerID = (int)id;
        INALayer2 naLayer = NetworkAnalystUtility.LayerFromLayerID(gisresource, layerID) as INALayer2;

        //Get context - was stored in Map1_BeginDisposeWebMap method
        string currentContextSerialized = System.Web.HttpContext.Current.Session["NAContext"] as string;

        if (currentContextSerialized != null)
        {
            INAContext currentContext = mapContext.LoadObject(currentContextSerialized) as INAContext;
            naLayer.AttachContext(currentContext);
        }
    }

    public void ApplyOriginalNAContext(GISResourceItem resource)
    {
        ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal gisresource = 
            (ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal)resource.Resource;
        IServerContext mapContext = gisresource.ServerContextInfo.ServerContext;

        //Get layer id - was stored in SolveRoute method
        // If null, the layer id wasn't saved.  Just get out.
        object id = System.Web.HttpContext.Current.Session["RouteLayerID"];
        if (id == null)
            return;

        int layerID = (int)id;
        INALayer2 naLayer = NetworkAnalystUtility.LayerFromLayerID(gisresource, layerID) as INALayer2;

        //store current context for future requests
        string currentContextSerialized = mapContext.SaveObject(naLayer.Context);
        System.Web.HttpContext.Current.Session["NAContext"] = currentContextSerialized;

        // Reattach original context
        string originalContextSerialized = System.Web.HttpContext.Current.Session["OriginalNAContext"] as string;
        INAContext originalNAContext = mapContext.LoadObject(originalContextSerialized) as INAContext;
        naLayer.AttachContext(originalNAContext);
    }
}