第一章 使用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);
}
}
1.3.2 停止服务
pServerAdmin.StopConfiguration("Flow", "MapServer");
1.3.3 删除服务
pServerAdmin.DeleteConfiguration("BusSimulation/DataPrep", "MapServer");//如果自己建立了目录请按照这种方式写,不然出错
1.4 REST方式操作
ArcGIS 10.1 for Server可以通过http请求的方式访问,server接到请求后,然后响应,响应的结果是json格式的,对json格式我们可以自己去解析,但是这个很麻烦,在10.1中增加了很多对json格式处理的接口IJSONReader2,IJSONWriter等很多,我在这里偷懒直接使用了AO中自带的接口,如果不用AO中的这些接口,自己借助一些json的解析工具如:JSON.NET也是可以的。
1.4.1 获取token
public string GenerateAGSToken_RESTAdmin(string restAdmin, string username, string password)
{
try
{
if (restAdmin.EndsWith("\\"))
{
restAdmin = restAdmin.Substring(0, restAdmin.Length - 1);
}
if (restAdmin.EndsWith("services"))
{
restAdmin = restAdmin.Substring(0, restAdmin.Length - 9);
}
string loginUrl = restAdmin + "/generateToken";
WebRequest request = WebRequest.Create(loginUrl);
request.Method = "POST";
string credential = "username=" + username + "&password=" + password + "&client=requestip&expiration=&f=json";
byte[] content = Encoding.UTF8.GetBytes(credential);
request.ContentLength = content.Length;
request.ContentType = "application/x-www-form-urlencoded";
Stream requestStream = request.GetRequestStream();
requestStream.Write(content, 0, content.Length);
requestStream.Close();
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string result = reader.ReadToEnd();
IJSONReader2 pJsonReader = new JSONReaderClass();
IJSONObject pOb = pJsonReader.ParseJSONString(result) as IJSONObject;
string token;
if (pOb.TryGetValueAsString("token", out token))
{
}
return token;
}
catch { return ""; }
}
//""表示根目录
public bool DeleteService_RESTAdmin(string restAdmin, string username, string password, string serviceName,string pType,string pFolder
)
{
try
{
//string token = GenerateAGSToken_RESTAdmin(restAdmin, username, password);
restAdmin = restAdmin.EndsWith("/") ? restAdmin.Substring(0, restAdmin.Length - 1) : restAdmin;
string serviceUrl = restAdmin + "/services" +pFolder+"/"+ serviceName + "." + pType + "/delete";
WebRequest request = WebRequest.Create(serviceUrl);
string postcontent = "f=pjson&token=" + token;
Byte[] content = Encoding.UTF8.GetBytes(postcontent);
request.ContentLength = content.Length;
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
Stream requestStream = request.GetRequestStream();
requestStream.Write(content, 0, content.Length);
requestStream.Close();
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string result = reader.ReadToEnd();
return result.Contains("success");
}
catch { return false; }
}
1.4.2 停止服务
public bool StopService_RESTAdmin(string restAdmin, string username, string password, string serviceName, string pType, string pFolder)
{
try
{
// string token = GenerateAGSToken_RESTAdmin(restAdmin, username, password);
restAdmin = restAdmin.EndsWith("/") ? restAdmin.Substring(0, restAdmin.Length - 1) : restAdmin;
if (pFolder.Length > 0)
{
pFolder = pFolder.StartsWith("/") ? pFolder : "/" + pFolder;
}
string serviceUrl = restAdmin + "/services" +pFolder+"/"+ serviceName + "." + pType + "/stop";
WebRequest request = WebRequest.Create(serviceUrl);
string postcontent = "f=pjson&token=" + token;
Byte[] content = Encoding.UTF8.GetBytes(postcontent);
request.ContentLength = content.Length;
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
Stream requestStream = request.GetRequestStream();
requestStream.Write(content, 0, content.Length);
requestStream.Close();
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(re