1

So I am trying to access a map layer, which I can connect to.

try
                {            
                    agsconn.Connect();
                    Log.Write("Connected.", "myclass");
                    if (agsconn.IsConnected)
                    {
    ESRI.ArcGIS.Server.IServerObjectManager som = agsconn.ServerObjectManager;
                            IServerContext serverContext = som.CreateServerContext(mapDocName, "MapServer");
                            IMapServer mapServer = serverContext.ServerObject as IMapServer;
                            Log.Write("Server context established MapName=" + mapServer.DefaultMapName, "myclass");
                            ESRI.ArcGIS.Carto.IMapServerObjects mapSO = mapServer as ESRI.ArcGIS.Carto.IMapServerObjects;
                            Log.Write("IMapServerObjects created", "myclass");
                            IMap map = mapSO.get_Map(mapServer.DefaultMapName);
                            Log.Write("Layers map established", "myclass");
                            //int layerIndex = -1;
                            IFeatureLayer fLayer = map.get_Layer(0) as IFeatureLayer;

The map was created with direct connect, I don't know if that makes a difference.

So far, the arcgis connects, and everything goes fine, but it stops here:

IMap map = mapSO.get_Map(mapServer.DefaultMapName);

I checked mapServer.DefaultMapName, and it returns "Layers" so that's all correct.

mapSO also works because mapServer and mapSO are the same object basically.

So how is there a null here?

My log prints out an exception and never reaches "layers map established".

The exception:

Object reference not set to an instance of an object.

I've tried a few other maps too and it doesn't work. But this code is straight from sample code I've seen on ESRI website, and it works for ESRI.

I'm using ArcGIS 9.3.1.

  • 1
    Is the map service based on a MSD file? MSD-based services do not have access to fine-grained ArcObjects.. – Petr Krebs Mar 19 '12 at 20:06
  • Yes I was just reading about that just now. You are right I believe. I am gonna try to see if MXD files fix this problem. – Dexter Mar 19 '12 at 20:20
0

Solved. you have to make sure your Map Service is an MXD file, not an MSD. Otherwise you cannot use "get_Map" or "get_Layer" etc. in geodatabase API.

Also enable web services in the properties of ArcGIS Manager.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.