/// <summary> /// 获取ArcGIS Server发布的地图服务图层 /// </summary> /// <param name="serviceUrl">服务地址,例:http://services.arcgisonline.com/ArcGIS/services </param> /// <param name="serviceName">服务名称,例:ESRI_Imagery_World_2D</param> /// <param name="isLAN">是否局域网(Local Area Network)</param> /// <returns></returns> public ILayer GetServerLayer(string serviceUrl, string serviceName, bool isLAN = false) { IAGSServerObjectName serverObjectName = GetServerObjectName(serviceUrl, serviceName, isLAN); IName iName = (IName)serverObjectName; IAGSServerObject serverObject = (IAGSServerObject)iName.Open(); IMapServer mapServer = (IMapServer)serverObject; IMapServerLayer mapServerLayer = new MapServerLayerClass(); //连接地图服务,第一个参数为地图服务名称,第二个参数为数据框架名称(the name of a data frame) mapServerLayer.ServerConnect(serverObjectName, mapServer.DefaultMapName); return((ILayer)mapServerLayer); }
private void AddAGSService(string fileName) { try { IMxDocument mxDoc = (IMxDocument)m_application.Document; IMap map = (IMap)mxDoc.FocusMap; IActiveView activeView = (IActiveView)map; bool isAlreadyInMap = false; if (isAlreadyInMap) { ShowErrorMessageBox(StringResources.MapServiceLayerAlreadyExistInMap); return; } else { if (fileName.ToLower().Contains("http") && !fileName.ToLower().Contains("arcgis/rest")) { if(fileName.EndsWith("MapServer")) fileName = fileName.Remove(fileName.LastIndexOf("MapServer")); String[] s = fileName.ToLower().Split(new String[]{"/services"}, StringSplitOptions.RemoveEmptyEntries); IPropertySet propertySet = new PropertySetClass(); propertySet.SetProperty("URL", s[0] + "/services"); // fileName IMapServer mapServer = null; IAGSServerConnectionFactory pAGSServerConFactory = new AGSServerConnectionFactory(); IAGSServerConnection agsCon = pAGSServerConFactory.Open(propertySet,0); IAGSEnumServerObjectName pAGSSObjs = agsCon.ServerObjectNames; IAGSServerObjectName pAGSSObj = pAGSSObjs.Next(); while (pAGSSObj != null) { if(pAGSSObj.Type=="MapServer" && pAGSSObj.Name.ToLower() == s[1].TrimStart('/').TrimEnd('/')){ break; } pAGSSObj = pAGSSObjs.Next(); } IName pName = (IName) pAGSSObj; IAGSServerObject pAGSO = (IAGSServerObject) pName.Open(); mapServer = (IMapServer) pAGSO; IPropertySet prop = new PropertySetClass(); prop.SetProperty("URL", fileName); prop.SetProperty("Name",pAGSSObj.Name); IMapServerLayer layer = new MapServerLayerClass(); layer.ServerConnect(pAGSSObj,mapServer.get_MapName(0)); mxDoc.AddLayer((ILayer)layer); } else { IGxFile pGxFile; if (fileName.ToLower().EndsWith(".tif")) { IRasterLayer pGxLayer = (IRasterLayer)new RasterLayer(); pGxLayer.CreateFromFilePath(fileName); if (pGxLayer.Valid) { map.AddLayer((ILayer)pGxLayer); } } else { if (fileName.ToLower().Contains("http") && fileName.ToLower().Contains("arcgis/rest")) { String[] s = fileName.ToLower().Split(new String[] { "/rest" }, StringSplitOptions.RemoveEmptyEntries); IPropertySet propertySet = new PropertySetClass(); propertySet.SetProperty("URL", s[0] + "/services"); // fileName IMapServer mapServer = null; IAGSServerConnectionFactory pAGSServerConFactory = new AGSServerConnectionFactory(); IAGSServerConnection agsCon = pAGSServerConFactory.Open(propertySet, 0); IAGSEnumServerObjectName pAGSSObjs = agsCon.ServerObjectNames; IAGSServerObjectName pAGSSObj = pAGSSObjs.Next(); String[] parts = s[1].ToLower().Split(new String[] { "/" }, StringSplitOptions.RemoveEmptyEntries); while (pAGSSObj != null) { if (pAGSSObj.Type == "MapServer" && pAGSSObj.Name.ToLower() == parts[1]) { break; } pAGSSObj = pAGSSObjs.Next(); } IName pName = (IName)pAGSSObj; IAGSServerObject pAGSO = (IAGSServerObject)pName.Open(); mapServer = (IMapServer)pAGSO; IPropertySet prop = new PropertySetClass(); prop.SetProperty("URL", fileName); prop.SetProperty("Name", pAGSSObj.Name); IMapServerLayer layer = new MapServerLayerClass(); layer.ServerConnect(pAGSSObj, mapServer.get_MapName(0)); mxDoc.AddLayer((ILayer)layer); } else { IGxLayer pGxLayer = new GxLayer(); pGxFile = (GxFile)pGxLayer; pGxFile.Path = fileName; if (pGxLayer.Layer != null) { map.AddLayer(pGxLayer.Layer); } } } } } } catch (Exception ex) { ShowErrorMessageBox(StringResources.AddArcGISLayerFailed + "\r\n" + ex.Message); } }
/// <summary> /// Add AGS Map Layer /// </summary> /// <param name="inUserName"></param> /// <param name="inPassword"></param> /// <param name="inName"></param> /// <param name="inMapUrl"></param> internal void AddAGSLayer(string inUserName, string inPassword, string inName, string inMapUrl) { IAGSServerConnectionName2 agsServerConnectionName = (IAGSServerConnectionName2) new AGSServerConnectionName(); // http://localhost/ArcGIS/rest/services/Map1/MapServer // Convert to http://localhost/ArcGis/Services // and http://localgsot/ArcGIS/Map1/MapServer string servicecatalog = inMapUrl.Substring(0, inMapUrl.IndexOf("/REST/SERVICES", StringComparison.CurrentCultureIgnoreCase)) + "/Services"; string mapserviceurl = ReplaceEx(inMapUrl, "/REST/", "/", StringComparison.InvariantCultureIgnoreCase); string mapservice = ""; Regex rMapServerRest = new Regex("^(?i:.*/REST/SERVICES/(?<mapname>.*)/MAPSERVER.*)$"); Match m = rMapServerRest.Match(inMapUrl); if (m.Success) { mapservice = m.Groups["mapname"].Value; } //Create a property set of connection details IPropertySet props = new PropertySet(); props.SetProperty("url", servicecatalog); if (inUserName != null) { props.SetProperty("user", inUserName); props.SetProperty("password", inPassword); props.SetProperty("hideuserproperty", false); props.SetProperty("anonymous", false); } IAGSServerObjectName2 agsServerObjectName = (IAGSServerObjectName2) new AGSServerObjectName(); IAGSServerConnectionFactory AGSConnectionFactory = new AGSServerConnectionFactory(); IAGSServerConnection AGSConnection = AGSConnectionFactory.Open(props, 0); IAGSEnumServerObjectName enumSOName = null; enumSOName = AGSConnection.ServerObjectNames; IAGSServerObjectName SOName = null; SOName = enumSOName.Next(); while (SOName != null) { System.Diagnostics.Debug.WriteLine(SOName.Name + ": " + SOName.Type); if (SOName.Name.Equals(mapservice, StringComparison.InvariantCultureIgnoreCase) && SOName.Type == "MapServer") { //create the layer object IMapServerGroupLayer mapServerLayer = new MapServerLayerClass(); IName mapServerConnectionName = (IName)SOName; IDataLayer dataLayer = (IDataLayer)mapServerLayer; //try to connect try { dataLayer.Connect(mapServerConnectionName); } catch (Exception ex) { throw new Exception("Could not connect to URL", ex); } //create the layer name ILayer layer; layer = (ILayer)mapServerLayer; layer.Visible = true; layer.Name = inName; //return the layer ArcMap.Document.AddLayer(layer); break; } SOName = enumSOName.Next(); } }
public void LoadData(IList ilist_0) { IEnumDataset subsets; IDataset j; string str; IFeatureClass dataset; IFeatureLayer cadFeatureLayerClass; ITopologyLayer topologyLayerClass; IEnumFeatureClass classes; IFeatureClass m; IFeatureClass n; IFeatureLayer featureLayerClass; List <object> objs = new List <object>(); foreach (object ilist0 in ilist_0) { if (ilist0 is IGxDataset) { if ((ilist0 as IGxDataset).Type != esriDatasetType.esriDTFeatureDataset) { objs.Add(ilist0); } else { IEnumGxObject children = (ilist0 as IGxObjectContainer).Children; children.Reset(); for (IGxObject i = children.Next(); i != null; i = children.Next()) { objs.Add(i); } } } else if (!(ilist0 is IDataset)) { objs.Add(ilist0); } else { if ((ilist0 as IDataset).Type != esriDatasetType.esriDTFeatureDataset) { continue; } subsets = (ilist0 as IDataset).Subsets; subsets.Reset(); for (j = subsets.Next(); j != null; j = subsets.Next()) { objs.Add(j); } } } ProcessAssist processAssist = new ProcessAssist(); processAssist.InitProgress(); processAssist.SetMaxValue(objs.Count); processAssist.SetMessage("正在加载数据,请稍候..."); processAssist.Start(); try { ILayer layer = null; IMap map = this.GetMap(); this.list_0 = new List <ILayer>(); List <string> strs = new List <string>(); IEnvelope envelope = null; IDataset item = null; IGxDataset gxDataset = null; for (int k = 0; k < objs.Count; k++) { string name = ""; if (objs[k] is IGxObject) { name = (objs[k] as IGxObject).Name; } else if (objs[k] is IDataset) { name = (objs[k] as IDataset).Name; } bool flag = true; str = string.Format("加载图层[{0}],第{1}/{2}个", name, k + 1, objs.Count); processAssist.Increment(1); processAssist.SetMessage(str); if (objs[k] is IGxLayer) { layer = (objs[k] as IGxLayer).Layer; if (layer is IGeoDataset && !SpatialReferenctOperator.ValideFeatureClass(layer as IGeoDataset)) { strs.Add(layer.Name); flag = false; } this.AddLayer(map, layer, ref envelope, flag); } else if (!(objs[k] is IGxAGSObject)) { if (objs[k] is IGxDataset) { gxDataset = objs[k] as IGxDataset; item = gxDataset.Dataset; } else if (ilist_0[k] is IDataset) { item = objs[k] as IDataset; } if (item != null) { if (item is IGeoDataset) { flag = SpatialReferenctOperator.ValideFeatureClass(item as IGeoDataset); } if (item.Type == esriDatasetType.esriDTFeatureClass) { dataset = (IFeatureClass)item; if ((gxDataset as IGxObject).Category.IndexOf("CAD") != -1) { if (dataset.FeatureType != esriFeatureType.esriFTCoverageAnnotation) { cadFeatureLayerClass = new CadFeatureLayerClass(); } else { cadFeatureLayerClass = new CadAnnotationLayerClass(); } cadFeatureLayerClass.FeatureClass = dataset; cadFeatureLayerClass.Name = dataset.AliasName; } else if (dataset.FeatureType == esriFeatureType.esriFTAnnotation) { cadFeatureLayerClass = new FDOGraphicsLayerClass() { FeatureClass = dataset, Name = dataset.AliasName }; } else if (dataset.FeatureType != esriFeatureType.esriFTDimension) { cadFeatureLayerClass = new FeatureLayerClass() { FeatureClass = dataset, Name = dataset.AliasName }; } else { cadFeatureLayerClass = new DimensionLayerClass() { FeatureClass = dataset, Name = dataset.AliasName }; } if (!flag) { strs.Add(cadFeatureLayerClass.Name); } this.AddLayer(map, cadFeatureLayerClass, ref envelope, flag); } else if (item.Type == esriDatasetType.esriDTCadDrawing) { if ((gxDataset as IGxObject).Category != "CAD绘图") { IEnumGxObject enumGxObject = (gxDataset as IGxObjectContainer).Children; enumGxObject.Reset(); for (IGxDataset l = enumGxObject.Next() as IGxDataset; l != null; l = enumGxObject.Next() as IGxDataset) { dataset = l.Dataset as IFeatureClass; if (dataset != null) { if (dataset.FeatureType != esriFeatureType.esriFTAnnotation) { cadFeatureLayerClass = new CadFeatureLayerClass() { FeatureClass = dataset, Name = dataset.AliasName }; } else { cadFeatureLayerClass = new FDOGraphicsLayerClass() { FeatureClass = dataset, Name = dataset.AliasName }; } flag = true; if (cadFeatureLayerClass is IGeoDataset) { bool flag1 = SpatialReferenctOperator.ValideFeatureClass(cadFeatureLayerClass as IGeoDataset); flag = flag1; if (!flag1) { strs.Add(cadFeatureLayerClass.Name); } } this.AddLayer(map, cadFeatureLayerClass, ref envelope, flag); } } } else { ICadLayer cadLayerClass = new CadLayerClass() { CadDrawingDataset = gxDataset.Dataset as ICadDrawingDataset, Name = gxDataset.Dataset.Name }; if (!flag) { strs.Add(cadLayerClass.Name); } this.AddLayer(map, cadLayerClass, ref envelope, flag); } } else if (item.Type == esriDatasetType.esriDTFeatureDataset) { subsets = item.Subsets; subsets.Reset(); for (j = subsets.Next(); j != null; j = subsets.Next()) { if (j.Type == esriDatasetType.esriDTFeatureClass) { dataset = j as IFeatureClass; if (dataset.FeatureType != esriFeatureType.esriFTAnnotation) { cadFeatureLayerClass = new FeatureLayerClass() { FeatureClass = dataset, Name = dataset.AliasName }; flag = true; if (cadFeatureLayerClass is IGeoDataset) { bool flag2 = SpatialReferenctOperator.ValideFeatureClass(cadFeatureLayerClass as IGeoDataset); flag = flag2; if (!flag2) { strs.Add(cadFeatureLayerClass.Name); } } this.AddLayer(map, cadFeatureLayerClass, ref envelope, flag); } else { cadFeatureLayerClass = new FDOGraphicsLayerClass() { FeatureClass = dataset, Name = dataset.AliasName }; flag = true; if (cadFeatureLayerClass is IGeoDataset) { bool flag3 = SpatialReferenctOperator.ValideFeatureClass(cadFeatureLayerClass as IGeoDataset); flag = flag3; if (!flag3) { strs.Add(cadFeatureLayerClass.Name); } } this.AddLayer(map, cadFeatureLayerClass, ref envelope, flag); } } else if (j.Type == esriDatasetType.esriDTTopology) { topologyLayerClass = new TopologyLayerClass() { Topology = j as ITopology }; (topologyLayerClass as ILayer).Name = j.Name; this.AddLayer(map, topologyLayerClass as ILayer, ref envelope, true); } } } else if (item.Type == esriDatasetType.esriDTTopology) { if (MessageBox.Show(string.Concat("是否将参加拓扑-", item.Name, "-的所有要素添加到地图中"), "添加拓扑层", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes) { classes = (item as IFeatureClassContainer).Classes; classes.Reset(); for (m = classes.Next(); m != null; m = classes.Next()) { cadFeatureLayerClass = new FeatureLayerClass() { FeatureClass = m, Name = m.AliasName }; flag = true; if (cadFeatureLayerClass is IGeoDataset) { bool flag4 = SpatialReferenctOperator.ValideFeatureClass(cadFeatureLayerClass as IGeoDataset); flag = flag4; if (!flag4) { strs.Add(cadFeatureLayerClass.Name); } } this.AddLayer(map, cadFeatureLayerClass, ref envelope, flag); } } topologyLayerClass = new TopologyLayerClass() { Topology = item as ITopology }; (topologyLayerClass as ILayer).Name = item.Name; this.AddLayer(map, topologyLayerClass as ILayer, ref envelope, true); } else if (item.Type == esriDatasetType.esriDTTin) { ITinLayer tinLayerClass = new TinLayerClass() { Dataset = (ITin)item, Name = item.Name }; flag = true; if (tinLayerClass is IGeoDataset) { bool flag5 = SpatialReferenctOperator.ValideFeatureClass(tinLayerClass as IGeoDataset); flag = flag5; if (!flag5) { strs.Add(tinLayerClass.Name); } } this.AddLayer(map, tinLayerClass, ref envelope, flag); } else if (item.Type == esriDatasetType.esriDTRasterCatalog) { IGdbRasterCatalogLayer gdbRasterCatalogLayerClass = null; gdbRasterCatalogLayerClass = new GdbRasterCatalogLayerClass(); if (gdbRasterCatalogLayerClass.Setup((ITable)(item as IRasterCatalog))) { bool flag6 = SpatialReferenctOperator.ValideFeatureClass(gdbRasterCatalogLayerClass as IGeoDataset); flag = flag6; if (!flag6) { strs.Add((gdbRasterCatalogLayerClass as ILayer).Name); } this.AddLayer(map, gdbRasterCatalogLayerClass as ILayer, ref envelope, flag); } } else if (!(item.Type == esriDatasetType.esriDTRasterDataset ? false : item.Type != esriDatasetType.esriDTRasterBand)) { bool flag7 = true; if (!((IRasterPyramid)item).Present) { if (this.m_pApp.PyramidPromptType == PyramidPromptType.AlwaysBuildNoPrompt) { ((IRasterPyramid)item).Create(); } else if (this.m_pApp.PyramidPromptType == PyramidPromptType.AlwaysPrompt) { DialogResult dialogResult = BuildPyramidSet.Show(); if (dialogResult == DialogResult.Yes) { ((IRasterPyramid)item).Create(); } else if (dialogResult == DialogResult.Cancel) { flag7 = false; } } } if (flag7) { IRasterLayer rasterLayerClass = new RasterLayerClass() { Cached = true }; rasterLayerClass.CreateFromDataset((IRasterDataset)item); rasterLayerClass.Name = item.Name; bool flag8 = SpatialReferenctOperator.ValideFeatureClass(rasterLayerClass as IGeoDataset); flag = flag8; if (!flag8) { strs.Add(rasterLayerClass.Name); } this.AddLayer(map, rasterLayerClass, ref envelope, flag); } } else if (item.Type == esriDatasetType.esriDTTable) { try { IStandaloneTableCollection standaloneTableCollection = this.GetMap() as IStandaloneTableCollection; IPropertySet connectionProperties = item.Workspace.ConnectionProperties; bool flag9 = false; int num = 0; while (true) { if (num < standaloneTableCollection.StandaloneTableCount) { ITable table = standaloneTableCollection.StandaloneTable[num].Table; if (!connectionProperties.IsEqual((table as IDataset).Workspace.ConnectionProperties) || !((table as IDataset).Name == item.Name)) { num++; } else { flag9 = true; break; } } else { break; } } if (!flag9) { standaloneTableCollection.AddStandaloneTable(new StandaloneTableClass() { Table = item as ITable }); } } catch (Exception exception) { CErrorLog.writeErrorLog(this, exception, ""); } } else if (item.Type == esriDatasetType.esriDTGeometricNetwork) { IGeometricNetwork geometricNetwork = item as IGeometricNetwork; if (geometricNetwork != null) { IEnumFeatureClass classesByType = geometricNetwork.ClassesByType[esriFeatureType.esriFTSimpleJunction]; classesByType.Reset(); for (n = classesByType.Next(); n != null; n = classesByType.Next()) { featureLayerClass = new FeatureLayerClass() { FeatureClass = n, Name = (n as IDataset).Name }; this.AddLayer(map, featureLayerClass, ref envelope, true); } classesByType = geometricNetwork.ClassesByType[esriFeatureType.esriFTComplexJunction]; classesByType.Reset(); for (n = classesByType.Next(); n != null; n = classesByType.Next()) { featureLayerClass = new FeatureLayerClass() { FeatureClass = n, Name = (n as IDataset).Name }; this.AddLayer(map, featureLayerClass, ref envelope, true); } classesByType = geometricNetwork.ClassesByType[esriFeatureType.esriFTSimpleEdge]; classesByType.Reset(); for (n = classesByType.Next(); n != null; n = classesByType.Next()) { featureLayerClass = new FeatureLayerClass() { FeatureClass = n, Name = (n as IDataset).Name }; this.AddLayer(map, featureLayerClass, ref envelope, true); } classesByType = geometricNetwork.ClassesByType[esriFeatureType.esriFTComplexEdge]; classesByType.Reset(); for (n = classesByType.Next(); n != null; n = classesByType.Next()) { featureLayerClass = new FeatureLayerClass() { FeatureClass = n, Name = (n as IDataset).Name }; this.AddLayer(map, featureLayerClass, ref envelope, true); } } } else if (item.Type == esriDatasetType.esriDTNetworkDataset) { INetworkLayer networkLayerClass = new NetworkLayerClass() { NetworkDataset = item as INetworkDataset }; this.AddLayer(map, networkLayerClass as ILayer, ref envelope, true); if (MessageBox.Show(string.Concat("是否将参加网络要素集-", item.Name, "-的所有要素添加到地图中"), "添加拓扑层", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes) { classes = (item as IFeatureClassContainer).Classes; classes.Reset(); for (m = classes.Next(); m != null; m = classes.Next()) { cadFeatureLayerClass = new FeatureLayerClass() { FeatureClass = m, Name = m.AliasName }; this.AddLayer(map, cadFeatureLayerClass, ref envelope, true); } } } } } else { IMapServerLayer mapServerLayerClass = new MapServerLayerClass(); mapServerLayerClass.ServerConnect((objs[k] as IGxAGSObject).AGSServerObjectName, (objs[k] as IGxAGSObject).DefaultMapName); this.AddLayer(map, mapServerLayerClass as ILayer, ref envelope, false); } } processAssist.End(); bool layerCount = this.GetMap().LayerCount > 0; if (strs.Count > 0) { MessageBox.Show("警告:数据范围不一致。\r\n 一个或多个添加的图层范围与关联的空间坐标系范围不一致。请检查数据问题。存在问题图层信息请查看错误日志!", "管理平台", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); StringBuilder stringBuilder = new StringBuilder(); foreach (string str1 in strs) { if (stringBuilder.Length > 0) { stringBuilder.Append(","); } stringBuilder.Append(str1); } CErrorLog.writeErrorLog(stringBuilder, null, "图层范围不一致!"); } foreach (ILayer list0 in this.list_0) { list0.Visible = true; } if (layerCount && envelope != null) { IMap map1 = this.GetMap(); ISpatialReference spatialReference = map1.SpatialReference; if ((spatialReference is IUnknownCoordinateSystem || envelope.SpatialReference is IUnknownCoordinateSystem || envelope.SpatialReference == null ? false : spatialReference != null)) { envelope.Project(spatialReference); } (map1 as IActiveView).Extent = envelope; (map1 as IActiveView).Refresh(); } if (this.m_pApp != null) { this.m_pApp.MapDocumentChanged(); } else if (ApplicationRef.Application != null) { ApplicationRef.Application.MapDocumentChanged(); } } catch (Exception exception1) { str = exception1.ToString(); } if (this.onLoadCompleteHand_0 != null) { this.onLoadCompleteHand_0(); } }
/// <summary> /// Parse ArcGIS MapServer Service /// </summary> /// <param name="urlAGS"></param> public void OpenAGS(string urlAGS) { try { ///Parse the url for MapServer -- get the service url and the service name string strUrl = urlAGS; string[] strUrlParts = strUrl.Split('/'); string strUrlSvr = null; string strSvrName = null; for (int i = 0; i < strUrlParts.Length; i++) { if (strUrlParts[i] == "services") { strUrlSvr = string.Join("/", strUrlParts.Take(i + 1).ToArray()); for (int j = i + 1; j < strUrlParts.Length - 1; j++) { if (strSvrName != null) { strSvrName += "/"; } strSvrName += strUrlParts[j]; } break; } } ///Set up ArcGIS Service connection factory, which includes the list of services IAGSServerConnectionFactory pAGSSvrConnFactory = new AGSServerConnectionFactoryClass(); IPropertySet pPropSet = new PropertySetClass(); pPropSet.SetProperty("URL", strUrlSvr); IAGSServerConnection pAGSSvrConn = pAGSSvrConnFactory.Open(pPropSet, 0); ///Get the list of services IAGSEnumServerObjectName pEnumSOName = pAGSSvrConn.ServerObjectNames; IAGSServerObjectName pAGSSOName = pEnumSOName.Next(); ///Get the specific service with the service name while (pAGSSOName != null) { //Debug.WriteLine(pAGSSOName.Name + ":" + pAGSSOName.Type); if (pAGSSOName.Name == strSvrName && pAGSSOName.Type == "MapServer") { break; } pAGSSOName = pEnumSOName.Next(); } ///Open the specific service IName pName = pAGSSOName as IName; IMapServer pMapServer = pName.Open() as IMapServer; IMapServerLayer pMapSvrLayer = new MapServerLayerClass(); pMapSvrLayer.ServerConnect(pAGSSOName, pMapServer.DefaultMapName); ///Configure the layer before adding it to the map ILayer pLayer = pMapSvrLayer as ILayer; AddLayer(pLayer); } catch (Exception ex) { MessageBox.Show(ex.StackTrace); } }