1.Server端連接
ESRI.ArcGIS.Server.GISServerConnectionClass gisconnection= new ESRI.ArcGIS.Server.GISServerConnectionClass();
gisconnection.Connect("localhost");
ESRI.ArcGIS.Server.IServerObjectManager som = gisconnection.ServerObjectManager;
string servertype = "MapServer";
string serverobjectname = "china";
ESRI.ArcGIS.Server.IServerContext servercontext = som.CreateServerContext(serverobjectname, servertype);
IMapServer ms = (IMapServer)servercontext.ServerObject;
IMapServerObjects pMapServerObjs = ms as IMapServerObjects;
IMap pMap = pMapServerObjs.get_Map(ms.DefaultMapName);
for (int i = 0; i < pMap.LayerCount;i++ )
{
IFeatureLayer pFLayer = pMap.get_Layer(i) as IFeatureLayer;
IFeatureClass pFeatureClass = pFLayer.FeatureClass;
Console.WriteLine(pFeatureClass.FeatureCount(null).ToString());
}
servercontext.ReleaseContext();
2. Engine連接Server
(1) IAGSServerObjectName pServerObjectName = GetMapServer("機器名或者URL", "china", true);
IName pName = (IName)pServerObjectName;
//訪問地圖服務
IMapServer pMapServer = (IMapServer)pName.Open();
ESRI.ArcGIS.Carto.IMapServerLayer pMapServerLayer = new ESRI.ArcGIS.Carto.MapServerLayerClass();
//連接地圖服務
pMapServerLayer.ServerConnect(pServerObjectName, pMapServer.DefaultMapName);
axMapFull.AddLayer(pMapServerLayer as ILayer);//這里會把服務中的圖層合成一個圖層來顯示,雖然看起來
//和服務中的地圖一樣
axMapFull.Refresh();
(2)
public IAGSServerObjectName GetMapServer(string pHostOrUrl, string pServiceName, bool pIsLAN)
{
//設置連接屬性
IPropertySet pPropertySet = new PropertySetClass();
if (pIsLAN)
pPropertySet.SetProperty("machine", pHostOrUrl);//machine可以是機器名或者IP,連接方式為LAN。
else
pPropertySet.SetProperty("url", pHostOrUrl);//如果url連接的需要設置用戶名和密碼,那么要設置用戶名和密碼屬性。
//如果使用url連接,那么一定要在瀏覽器可以訪問才行。
//打開連接
IAGSServerConnectionFactory pFactory = new AGSServerConnectionFactory();
IAGSServerConnection pConnection = pFactory.Open(pPropertySet, 0);
//Get the image server.
IAGSEnumServerObjectName pServerObjectNames = pConnection.ServerObjectNames;
pServerObjectNames.Reset();
IAGSServerObjectName ServerObjectName = pServerObjectNames.Next();
while (ServerObjectName != null)
{
if ((ServerObjectName.Name.ToLower() == pServiceName.ToLower()) &&
(ServerObjectName.Type == "MapServer"))
{
break;
}
ServerObjectName = pServerObjectNames.Next();
}
//返回對象
return ServerObjectName;
}
3.ADF連接
ESRI.ArcGIS.ADF.Identity identity = new ESRI.ArcGIS.ADF.Identity("user", "passwd", "domain");
ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection ags_connection;
agsconnection = new ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection("hostname", identity);
agsconnection.Connect();
IServerObjectManager som = agsconnection.ServerObjectManager;