/// <summary> /// co-create optimized mapserver. /// </summary> /// <param name="msdfile"></param> public static IMapServer ConstructMapServer(String msdfile) { try { IMapServer pMapServer = Activator.CreateInstance(Type.GetTypeFromProgID("esriCartoX.MapServerX")) as IMapServer; IMapServerInit2 mapServerInit = (IMapServerInit2)pMapServer; mapServerInit.PhysicalOutputDirectory = System.IO.Path.GetTempPath(); mapServerInit.VirtualOutputDirectory = System.IO.Path.GetTempPath(); mapServerInit.Connect(msdfile); return(pMapServer); } catch (Exception ex) { MessageBox.Show("Error CoCreating MapServer objection" + Environment.NewLine + ex.Message); return(null); } }
private static Dictionary <string, List <MapServiceInfo> > CollectMapServerInfo(IEnumerable <string> servers) { Dictionary <string, List <MapServiceInfo> > output; output = new Dictionary <string, List <MapServiceInfo> >(); AGSServerConnection connection = null; IServerObjectManager4 som = null; IServerContext ctxt = null; IMapServer mapServer = null; IEnumServerObjectConfigurationInfo socInfos; IServerObjectConfigurationInfo2 socInfo; MapServiceInfo mapServiceInfo; try { // Loop through all of the hosts (servers) and create a div for each one containing map service test results. foreach (var host in servers) { var mapServiceInfos = new List <MapServiceInfo>(); output.Add(host, mapServiceInfos); // Create the connection object connection = new AGSServerConnection(host, null, false, true); try { // Attempt to connect to the server. connection.Connect(false); if (connection.IsConnected) { // Get the Server Object Manager (SOM) for the current ArcGIS Server. som = (IServerObjectManager4)connection.ServerObjectManager; // Get a list of the services on the server. socInfos = som.GetConfigurationInfos(); // Get the first service from the list. socInfo = socInfos.Next() as IServerObjectConfigurationInfo2; // Loop through the list of services... while (socInfo != null) { mapServiceInfo = new MapServiceInfo { MapServiceName = socInfo.Name }; try { // Proceed only if the current service is a "MapServer". if (string.Compare(socInfo.TypeName, "MapServer", true) == 0) { // Create a div for the current map service. Console.Error.WriteLine("{0}", socInfo.Name); try { // Create a server context for the current map service. ctxt = som.CreateServerContext(socInfo.Name, socInfo.TypeName); // Cast the context object to an IMapServer. mapServer = (IMapServer)ctxt.ServerObject; // Get the document name IMapServerInit2 msInit = null; msInit = mapServer as IMapServerInit2; string sourceDocName = msInit != null ? msInit.FilePath : null; if (sourceDocName != null) { mapServiceInfo.SourceDocumentPath = sourceDocName; } // Create a dictionary of the properties for all of the layers in the map service. mapServiceInfo.ConnectionProperties = mapServer.GetConnectionProperties(); } catch (COMException comEx) { // See if the exception was caused by a stopped service. Just write the message in this case. if (comEx.ErrorCode == -2147467259) { mapServiceInfo.ErrorMessage = comEx.Message; } else { mapServiceInfo.ErrorMessage = string.Format("{0}: {1}", comEx.ErrorCode, comEx.Message); } } } } catch (Exception ex) { mapServiceInfo.ErrorMessage = ex.ToString(); } finally { // Release the server context. if (ctxt != null) { ctxt.ReleaseContext(); } // Go to the next soc info. socInfo = socInfos.Next() as IServerObjectConfigurationInfo2; } mapServiceInfos.Add(mapServiceInfo); } } } finally { connection.Dispose(); } } } finally { // Release any COM objects that have been created. if (mapServer != null) { Marshal.ReleaseComObject(mapServer); } if (ctxt != null) { Marshal.ReleaseComObject(ctxt); } if (som != null) { Marshal.ReleaseComObject(som); } } return(output); }