Network Analyst routing
ArcGIS_Routing_VBNet\App_Code\ServerObjectStateModifier.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
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
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.Display
Imports System.IO
Imports ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer
Imports ESRI.ArcGIS.ADF.Web.UI.WebControls
Imports ESRI.ArcGIS.NetworkAnalyst
Imports ESRI.ArcGIS.Server.Web.NetworkAnalyst

Public Class ServerObjectStateModifier
  Public Sub ApplySessionNAContext(ByVal resource As GISResourceItem)
    Dim gisresource As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal = CType(resource.Resource, ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal)
    Dim mapContext As IServerContext = gisresource.ServerContextInfo.ServerContext

    'Get layer id - was stored in SolveRoute method
    Dim id As Object = System.Web.HttpContext.Current.Session("RouteLayerID")
    If id Is Nothing Then
      Return
    End If

    Dim layerID As Integer = CInt(Fix(id))
    Dim naLayer As INALayer2 = TryCast(NetworkAnalystUtility.LayerFromLayerID(gisresource, layerID), INALayer2)

    'Get context - was stored in Map1_BeginDisposeWebMap method
    Dim currentContextSerialized As String = TryCast(System.Web.HttpContext.Current.Session("NAContext"), String)

    If Not currentContextSerialized Is Nothing Then
      Dim currentContext As INAContext = TryCast(mapContext.LoadObject(currentContextSerialized), INAContext)
      naLayer.AttachContext(currentContext)
    End If
  End Sub

  Public Sub ApplyOriginalNAContext(ByVal resource As GISResourceItem)
    Dim gisresource As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal = CType(resource.Resource, ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal)
    Dim mapContext As IServerContext = gisresource.ServerContextInfo.ServerContext

    'Get layer id - was stored in SolveRoute method
    ' If null, the layer id wasn't saved.  Just get out.
    Dim id As Object = System.Web.HttpContext.Current.Session("RouteLayerID")
    If id Is Nothing Then
      Return
    End If

    Dim layerID As Integer = CInt(Fix(id))
    Dim naLayer As INALayer2 = TryCast(NetworkAnalystUtility.LayerFromLayerID(gisresource, layerID), INALayer2)

    'store current context for future requests
    Dim currentContextSerialized As String = mapContext.SaveObject(naLayer.Context)
    System.Web.HttpContext.Current.Session("NAContext") = currentContextSerialized

    ' Reattach original context
    Dim originalContextSerialized As String = TryCast(System.Web.HttpContext.Current.Session("OriginalNAContext"), String)
    Dim originalNAContext As INAContext = TryCast(mapContext.LoadObject(originalContextSerialized), INAContext)
    naLayer.AttachContext(originalNAContext)
  End Sub
End Class