第一章 使用ArcGIS Engine 管理ArcGIS Server
1.1 原因
在10版本以及之前的版本中,有一些用户通过ArcGIS Engine来管理ArcGIS Server,比如发布服务,停止服务等,对服务的管理是通过DCOM方式的,而在10.1中ArcGIS 不支持DOCM方式的连接,。如果你在代码中使用了Server库中的GISServerConnection或者GISClient库中的AGSServerConnection,在ArcGIS Server 10.1环境下,这些代码必须移除,如下面的代码:如果强行执行,那么会有下面的错误:
1.2 解决
ArcGIS Server在10.1版本中提供了ADMIN API,只要我们通过http请求构造相应的参数就行,对于ArcGIS Server的连接,我们可以看下desktop是如何连接的:从这两个图中就可以看出,在选择连接Server的时候,选择角色,选择服务的类型(SDS和ArcGIS Server),在10版本以及之前的版本的时候跟这个有较大区别,连接ArcGIS 10.1 for Server的代码如下(注释已经写出):
public IServerObjectAdmin ConnectAGS(string host, string username, string password)
{
try
{
IPropertySet propertySet = new PropertySetClass();
propertySet.SetProperty("url", host);
propertySet.SetProperty("ConnectionMode", esriAGSConnectionMode.esriAGSConnectionModeAdmin);
propertySet.SetProperty("ServerType", esriAGSServerType.esriAGSServerTypeDiscovery);
propertySet.SetProperty("user", username);
propertySet.SetProperty("password", password);
propertySet.SetProperty("ALLOWINSECURETOKENURL", true); //设置为false会弹出一个警告对话框
IAGSServerConnectionName3 pConnectName = new AGSServerConnectionNameClass() as IAGSServerConnectionName3;//10.1新增接口
pConnectName.ConnectionProperties = propertySet;
IAGSServerConnectionAdmin pAGSAdmin = ((IName)pConnectName).Open() as IAGSServerConnectionAdmin;
token = GenerateAGSToken_RESTAdmin(host, "arcgis", "arcgis");
return pAGSAdmin.ServerObjectAdmin;
}
catch (Exception exc)
{
Console.WriteLine("连接失败: {0}. Message: {1}", host, exc.Message);
return null ;
}
}
ConnectAGS("http://192.168.110.136:6080/arcgis/admin", "arcgis", "arcgis");
当获得了IAGSServerConnectionAdmin 这个接口我们进而可以获取IServerObjectAdmin,IServerObjectAdmin接口,
在帮助中看到了IDiscoveryServerObjectAdmin接口,该接口的方法和属性如下:
我欣喜若狂,因为这些和ArcGIS Server的Admin几乎是对应的,实现该接口的类不能被实例化,必须通过其他对象获取。但是不幸的是我在dll中并没有发现这个。
1.2.1 一个猜想
既然这个接口不能自己new,必须通过其他的实例化,那么这个对象是谁?我们很自然的想到应该在连接成功之后就返回这个对象,该对象实现的接口如下:
我们注意下IRESTServerObjectAdmin 接口,字面意思就是通过rest方式跟Server打交道,而该接口只被一个类实现,该类就DiscoveryServerObjectAdmin
如果连接成功返回的真是DiscoveryServerObjectAdmin对象,那么我们QI到IRESTServerObjectAdmin上是没有问题的,结果正如我猜想的一样,有图为证:
以上是我的个人猜想,但是不知道为什么dll中没找到这个接口,还是ESRI故意将这个接口隐藏起来了。
1.3 AO操作
当连接上ArcGIS Server,我们获取了IAGSServerConnectionAdmin,通过该对象我们获取了IServerObjectAdmin,通过该对象可以对Server进行管理1.3.1 更改实例数
private void ChangInstance(IServerObjectAdmin pSAdmin, IServerObjectConfiguration pConf, int pMin, int pMax){
IEnumServerObjectConfiguration pEnServerConf = pSAdmin.GetConfigurations();
IServerObjectConfiguration pSConf = pEnServerConf.Next();
IServerObjectConfiguration pSOConfig = null;
while (pSConf != null)
{
if (pSConf.Name == pConf.Name && pSConf.TypeName == pConf.TypeName)
{
pSOConfig = pSConf;
break;
}
pSConf = pEnServerConf.Next();
}
if (pSConf != null)
{
pSConf.MinInstances = pMin;
pSOConfig.MaxInstances = pMax;
pSAdmin.UpdateConfiguration(pSOConfig);
}
}