在ArcGIS Server的程序中來連接GIS Server,通常有兩個理由:1、連接後,創建ServerContext,之後創建更多的AO對象,從而在程序中調用AO來工作;2、連接後,可以用來管理GIS Server,比如重新啓動某個服務。
通過程序來連接到GISServer,主要有兩個對象可以使用:ESRI.ArcGIS.Server.GISServerConnection(實現了IGISServerConnection2接口)和ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection。前者是com對象,後者是原生的.net對象。
先來看ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection。這是ADF中的.net對象,通常推薦使用這個對象來進行連接工作,因爲它可以指定使用特定的賬戶身份來連接GIS Server。
/*經本人測試,好像只有當本機和連接的機器(當然兩個不是同一機器)的用戶名和密碼分別一樣時,才能連接上遠程地圖服務。這可能是本機的環境的問題。同時必須保證此連接用戶是屬於agsadmin
和agsusers用戶組中的*/
ESRI.ArcGIS.ADF.Identity id =
new ESRI.ArcGIS.ADF.Identity("Administrator",
"123456", "gis2-trq");
ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection agsconn;
agsconn = new ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection("10.150.38.34", id);
agsconn.Connect();
if (!agsconn.IsConnected)
{
agsconn.Dispose();
return ;
}
//獲取som對象,然後因此獲取地圖對象
ESRI.ArcGIS.Server.IServerObjectManager som = agsconn.ServerObjectManager;
string servertype = "MapServer";
string serverobjectname = "GAL";
ESRI.ArcGIS.Server.IServerContext sc = som.CreateServerContext(serverobjectname, servertype);
IMapServer pMapServer = sc.ServerObject as IMapServer;
IMapServerObjects pMapServerObjs = pMapServer as IMapServerObjects;
IMap pMap = pMapServerObjs.get_Map(pMapServer.DefaultMapName);
再來看ESRI.ArcGIS.Server.GISServerConnection。使用方法如下:
ESRI.ArcGIS.Server.IGISServerConnection2 pGISSC = new ESRI.ArcGIS.Server.GISServerConnectionClass();
pGISSC.Connect("yourservername");
ESRI.ArcGIS.Server.IServerObjectAdmin pAdm= pGISSC.ServerObjectAdmin;
/*經本人測試,這種連接方式與上種需要滿足的條件是一樣的 */