Managing state in a Network Analyst application


If you have created ArcGIS Server Web applications, you are familiar with the MapServer MapDescription and how it is used to save the state of your Web application. The MapDescription is specifically designed to support the programming patterns you need to employ to save state and apply it to the map.
NAContext was designed to support the saving of state for Network Analyst applications. NAContext is analogous to MapDescription; the basic principles are the same.
You can save NAContext in your Web application session state so that multiple users sharing the same pool of objects can each have their own network analysis results as illustrated in the following graphic.
As with any application, a Network Analyst application generates results that are specific to a particular user. For example, a user might enter two addresses and find directions between the two points. Another user of the same application will create directions from another set of locations. The Web application needs to maintain some information about each client's session so it can display these separate routes for each user. If the MapServer that is used by the application is pooled, and therefore shared across each session, the application must maintain the state of what each client is doing. NAContext is used to maintain the state of the Network Analyst application; therefore, each user of the application has their own copy of NAContext.
When creating a Network Analyst Web application, NAContext is serialized in the Web server session state to store all Network Analyst state from one request to another. For example, once you have a route displayed on the map, you can continue to display that route even after the map has been panned or zoomed. For each request to your application, a process occurs to retrieve, store, copy or apply, serialize, and restore NAContext. Typically, during each use of a map server object (that is, each request from the Web browser), the following steps take place for each Network Analyst layer with which you are working:
  1. At the beginning of the request, retrieve the original NAContext from the Network Analyst layer.
  2. Store the original NAContext.
If this is the first request:
      1. Make a copy of NAContext by serializing using the saveObject method on IServerContext.
      2. Store the serialized copy in session state for the duration of the request.
If this is not the first request, use loadObject on IServerContext to deserialize NAContext saved in the previous request and apply it to NALayer to restore the Network Analyst state.
  1. Perform the necessary Network Analyst work, such as adding a network location or solving for a route.
  2. At the end of the request, serialize the current NAContext and store it for the next request.
  3. Restore the original NAContext retrieved at the beginning of the request to return the server object to its initial state.
The Web controls include utility classes that manage the saving of NAContext in session state and apply it across requests for you.
The Network Analyst templates illustrate these steps; you should refer back to this topic as you review the Network Analyst templates.
When NAContext is serialized, it serializes all of its member objects. For example, all of the stops, barriers, and facilities in the NAClasses of the Network Analyst layer will be serialized. This is an important consideration when you are working with a Network Analyst layer that has a potentially large number of facilities, since the serialized NAContext can get very large. Once NAContext gets large, you may impact the performance of your Web application. The Closest Facility sample illustrates how you can address this concern by creating an application that doesn't require serialization of NAContext.


See Also:

Working with networks
Using Network Analyst objects