http://blog.csdn.net/arcgis_all/article/details/17376397

1 ArcGIS Engine 10.2 如何发布服务

ArcGIS Engine的代码不能直接将MXD地图文档作为数据源进行发布,如果要发布的话,需要用ArcMap将MXD转成MSD,然后使用转换成功的MSD文档进行发布,代码如下:

public void Publish(string host,string username,string password,  string service, string msdDocument, string outputDir)

        {

//IAGSServerConnectionFactory3 pConnectionFactory = new AGSServerConnectionFactoryClass();

////admin connection file works fine, tested in ArcCatalog on the same server

//IAGSServerConnection4 server = pConnectionFactory.OpenFromFile(@"e:\admin-connection.ags", 0) as IAGSServerConnection4;

//     IAGSServerConnectionAdmin pAGSServerConnectionAdmin = server as IAGSServerConnectionAdmin;

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;

IAGSServerConnectionAdmin pAGSServerConnectionAdmin = pAGSAdmin as IAGSServerConnectionAdmin;

IServerObjectAdmin pServerObjectAdmin = pAGSServerConnectionAdmin.ServerObjectAdmin;

IServerObjectConfiguration5 pConfiguration = (IServerObjectConfiguration5)pServerObjectAdmin.CreateConfiguration();

//Set the general configuration settings

pConfiguration.Name = service;

pConfiguration.TypeName = "MapServer";

pConfiguration.TargetCluster = "default";

pConfiguration.StartupType = esriStartupType.esriSTAutomatic;

pConfiguration.IsolationLevel = esriServerIsolationLevel.esriServerIsolationHigh;

pConfiguration.IsPooled = true;

pConfiguration.Description = "Modsim Map Output";

// pConfiguration.LoadBalancing = esriLoadBalancing.esriLoadBalancingNone;//没有集群的话可以不用设置

pConfiguration.MinInstances = 1;

pConfiguration.MaxInstances = 15;

pConfiguration.WaitTimeout = 60;

pConfiguration.UsageTimeout = 600;

pConfiguration.IdleTimeout = 1800;

//Set the configuration properties of the MapServer

IPropertySet pProps = pConfiguration.Properties;

pProps.SetProperty("FilePath", msdDocument);

pProps.SetProperty("OutputDir", outputDir);

pProps.SetProperty("MaxImageHeight", "2048");

pProps.SetProperty("MaxRecordCount", "1000");

pProps.SetProperty("MaxBufferCount", "100");

pProps.SetProperty("MaxImageWidth", "2048");

pConfiguration.Properties = pProps;

//MIME+URL (virtual directory)

IEnumServerDirectory dirs = pServerObjectAdmin.GetServerDirectories();

dirs.Reset();

IServerDirectory serverDir = dirs.Next();

while (serverDir != null)

            {

if (((IServerDirectory2)serverDir).Type == esriServerDirectoryType.esriSDTypeOutput)

                {

pProps.SetProperty("OutputDir", serverDir.Path);

pProps.SetProperty("VirtualOutputDir", serverDir.URL);

break;

// gp.AddMessage("[DEBUG] Outputpath: " + serverDir.Path + " || Virtual: " + serverDir.URL);

                }

serverDir = dirs.Next();

            }

//Set the info segment properties

IPropertySet info = pConfiguration.Info;

info.SetProperty("WebEnabled", "true");

info.SetProperty("WebCapabilities", "Map,Query,Data");

pConfiguration.Info = info;

//Set the recycle properties of the MapServer object

IPropertySet recycle = pConfiguration.RecycleProperties;

recycle.SetProperty("StartTime", "1:00 AM");

recycle.SetProperty("Interval", "86400");

pConfiguration.RecycleProperties = recycle;

//Add the configuration to the server

pServerObjectAdmin.AddConfiguration(pConfiguration);

pServerObjectAdmin.StartConfiguration(service, "MapServer");

        }

2 使用ArcGIS Engie中的GP发布地图文档

ArcGIS 10.1 在发布服务的时候其实是按照下面的步骤来的,如果认真观察过也不难得出:

l  将MXD文档转成sddraft文件;

l  将sddraft文件转成sd文件;

l 将sd文件上传到ArcGIS for Server中

既然这个过程已经知道了,那么就可以通过Python按照这个流程来自动化的完成服务的发布:

import arcpy

# define local variables

wrkspc = 'C:/Project/'

mapDoc = arcpy.mapping.MapDocument(wrkspc + 'counties.mxd')

con = r'GIS Servers\arcgis on MyServer_6080 (admin).ags'

service = 'Counties'

sddraft = wrkspc + service + '.sddraft'

sd = wrkspc + service + '.sd'

# create service definition draft

arcpy.mapping.CreateMapSDDraft(mapDoc, sddraft, service, 'ARCGIS_SERVER', con, True, None)

# analyze the service definition draft

analysis = arcpy.mapping.AnalyzeForSD(sddraft)

# stage and upload the service if the sddraft analysis did not contain errors

if analysis['errors'] == {}:

# Execute StageService

    arcpy.StageService_server(sddraft, sd)

# Execute UploadServiceDefinition

    arcpy.UploadServiceDefinition_server(sd, con)

else:

# if the sddraft analysis contained errors, display them

print analysis['errors']

可以将上面的脚本创建为一个tbx文件,然后在ArcGIS Engine中通过GP来实现服务的发布.

其实Esri在官网上发布了一个tbx里面就包含了对server服务管理的功能(有兴趣的可以下载,文档所在的目录下也包含了):

3 使用Admin API 发布文档

ArcGIS for Server 10.1 增加了Admin API,那么,所谓的Admin API 其实就是一一http请求的方法,这些方法包含了对Server的管理,下面为Admin API 的代码:

namespace ServerAPIAdmin.ArcGIS.Rest

{

using System;

using System.Collections;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Net;

using System.Text;

using System.Web;

using ESRI.ArcGIS.SOESupport;

///<summary>

/// tipi di servizio arcgis server (mappa la tabella servizio tipo)

///</summary>

public enum ServiceType

    {

MapServer,

GeocodeServer,

SearchServer,

IndexingLauncher,

IndexGenerator,

GeometryServer,

GeoDataServer,

GPServer,

GlobeServer,

ImageServer

    }

///<summary>

/// Load Balancing

///</summary>

public enum LoadBalancing

    {

ROUND_ROBIN,

FAIL_OVER

    }

///<summary>

/// isolation level

///</summary>

public enum IsolationLevel

    {

LOW,

HIGH

    }

///<summary>

/// administrative API Rest

///</summary>

public class AGSAdmin

    {

private string username;

private string password;

private string urlRestAdmin;

private string urlRestServer;

///<summary>

/// Initializes a new instance of the <see cref="AGSAdmin"/> class.

///</summary>

///<param name="serverName">server name</param>

///<param name="port">port of server</param>

///<param name="username">username administrator</param>

///<param name="password">password administrator</param>

public AGSAdmin(string serverName, int port, string username, string password)

        {

this.username = username;

this.password = password;

string url = string.Format("http://{0}:{1}/arcgis", serverName, port.ToString());

this.urlRestAdmin = url + "/admin";

this.urlRestServer = url + "/server";

        }

///<summary>

/// Prevents a default instance of the <see cref="AGSAdmin"/> class from being created.

///</summary>

private AGSAdmin()

        {

        }

///<summary>

/// Create arcgis server folder

///</summary>

///<param name="folderName">Folder name</param>

///<param name="description">Description of the folder</param>

///<returns>True if successfully created</returns>

public bool CreateServerFolder(string folderName, string description)

        {

try

            {

string token = this.GenerateAGSToken();

string folderUrl = this.urlRestAdmin + "/services/" + folderName + "?f=json&token=" + token;

string resultExistsFolder = this.GetResult(folderUrl);

if (!this.HasError(resultExistsFolder))

                {

return true; // exists

                }

else

                {

string createFolderUrl = this.urlRestAdmin + "/services/createFolder";

string postContent = string.Format("folderName={0}&description={1}&f=json&token={2}", folderName, description, token);

string result = this.GetResult(createFolderUrl, postContent);

return this.HasSuccess(result);

                }

            }

catch

            {

return false;

            }

        }

///<summary>

/// Get physical Path and virtual Path from directory ags

///</summary>

///<param name="directory">directory ags</param>

///<param name="physicalPath">physical Path</param>

///<param name="virtualPath">virtual Path</param>

///<returns>True if successfully return path</returns>

public bool GetServerDirectory(string directory, out string physicalPath, out string virtualPath)

        {

physicalPath = null;

virtualPath = null;

try

            {

string token = this.GenerateAGSToken();

string directoryUrl = this.urlRestAdmin + "/system/directories/" + directory + "?f=json&token=" + token;

string result = this.GetResult(directoryUrl);

JsonObject jsonObject = new JsonObject(result);

if (!jsonObject.Exists("physicalPath") || !jsonObject.TryGetString("physicalPath", out physicalPath))

                {

throw new Exception();

                }

jsonObject = new JsonObject(result);

if (!jsonObject.Exists("virtualPath") || !jsonObject.TryGetString("virtualPath", out virtualPath))

                {

throw new Exception();

                }

return true;

            }

catch

            {

return false;

            }

        }

///<summary>

/// Delete Service

///</summary>

///<param name="serviceName">Service Name</param>

///<param name="serviceType">Server Type</param>

///<returns>True if successfully deleted</returns>

public bool DeleteService(string serviceName, ServiceType serviceType)

        {

try

            {

string token = this.GenerateAGSToken();

string serviceUrl = this.urlRestAdmin + "/services/" + serviceName + "." + Enum.GetName(typeof(ServiceType), serviceType) + "/delete";

string result = this.GetResult(serviceUrl, "f=json&token=" + token);

return this.HasSuccess(result);

            }

catch

            {

return false;

            }

        }

///<summary>

/// Start Service

///</summary>

///<param name="serviceName">Service Name</param>

///<param name="serviceType">Server Type</param>

///<returns>True if successfully started</returns>

public bool StartService(string serviceName, ServiceType serviceType)

        {

try

            {

string token = this.GenerateAGSToken();

string serviceUrl = this.urlRestAdmin + "/services/" + serviceName + "." + Enum.GetName(typeof(ServiceType), serviceType) + "/start";

string result = this.GetResult(serviceUrl, "f=json&token=" + token);

return this.HasSuccess(result);

            }

catch

            {

return false;

            }

        }

///<summary>

/// Stop Service

///</summary>

///<param name="serviceName">Service Name</param>

///<param name="serviceType">Server Type</param>

///<returns>True if successfully stopped</returns>

public bool StopService(string serviceName, ServiceType serviceType)

        {

try

            {

string token = this.GenerateAGSToken();

string serviceUrl = this.urlRestAdmin + "/services/" + serviceName + "." + Enum.GetName(typeof(ServiceType), serviceType) + "/stop";

string result = this.GetResult(serviceUrl, "f=json&token=" + token);

return this.HasSuccess(result);

            }

catch

            {

return false;

            }

        }

///<summary>

/// list of services

///</summary>

public void ListServices()

        {

this.ListServices(null);

        }

///<summary>

/// list of services in folder

///</summary>

///<param name="folder">name of folder</param>

public void ListServices(string folder)

        {

try

            {

string token = this.GenerateAGSToken();

string serviceUrl = this.urlRestAdmin + "/services/" + folder;

string postcontent = "f=json&token=" + token;

string result = this.GetResult(serviceUrl, postcontent);

JsonObject jsonObject = new JsonObject(result);

object[] folders = null;

if (jsonObject.Exists("folders") && jsonObject.TryGetArray("folders", out folders))

                {

foreach (string subfolder in folders)

                    {

this.ListServices(subfolder);

                    }

                }

object[] services = null;

if (jsonObject.Exists("services") && jsonObject.TryGetArray("services", out services))

                {

IEnumerable<JsonObject> jsonObjectService = services.Cast<JsonObject>();

jsonObjectService.ToList().ForEach(jo =>

                    {

string serviceName;

jo.TryGetString("serviceName", out serviceName);

string folderName;

jo.TryGetString("folderName", out folderName);

Console.WriteLine(folderName + "/" + serviceName);

                    });

                }

            }

catch

            {

throw;

            }

        }

///<summary>

/// create service type MapServer

///</summary>

///<returns>>True if successfully created</returns>

public bool CreateService(String msdPath)

        {

try

            {

string token = this.GenerateAGSToken();

string serviceUrl = this.urlRestAdmin + "/services/createService";

JsonObject jsonObject = new JsonObject();

jsonObject.AddString("serviceName", "Test");

jsonObject.AddString("type", Enum.GetName(typeof(ServiceType), ServiceType.MapServer));

jsonObject.AddString("description", "This is an example");

jsonObject.AddString("capabilities", "Map,Query,Data");

jsonObject.AddString("clusterName", "default");

jsonObject.AddLong("minInstancesPerNode", 1);

jsonObject.AddLong("maxInstancesPerNode", 2);

jsonObject.AddLong("maxWaitTime", 60);

jsonObject.AddLong("maxStartupTime", 300);

jsonObject.AddLong("maxIdleTime", 1800);

jsonObject.AddLong("maxUsageTime", 600);

jsonObject.AddLong("recycleInterval", 24);

jsonObject.AddString("loadBalancing", Enum.GetName(typeof(LoadBalancing), LoadBalancing.ROUND_ROBIN));

jsonObject.AddString("isolationLevel", Enum.GetName(typeof(IsolationLevel), IsolationLevel.HIGH));

JsonObject jsonObjectProperties = new JsonObject();

// see for a list complete http://resources.arcgis.com/en/help/server-admin-api/serviceTypes.html

jsonObjectProperties.AddLong("maxBufferCount", 100); // optional 100

jsonObjectProperties.AddString("virtualCacheDir", this.urlRestServer + "/arcgiscache"); // optional

jsonObjectProperties.AddLong("maxImageHeight", 2048); // optional 2048

jsonObjectProperties.AddLong("maxRecordCount", 1000); // optional 500

// Other service types do not require you to use arcpy.mapping or create an MSD.

jsonObjectProperties.AddString("filePath", msdPath); // required

jsonObjectProperties.AddLong("maxImageWidth", 2048); // optional 2048

jsonObjectProperties.AddBoolean("cacheOnDemand", false); // optional false

jsonObjectProperties.AddString("virtualOutputDir", this.urlRestServer + "/arcgisoutput");

jsonObjectProperties.AddString("outputDir", @"C:\arcgisserver\directories\arcgisoutput"); // required

jsonObjectProperties.AddString("supportedImageReturnTypes", "MIME+URL"); // optional MIME+URL

jsonObjectProperties.AddBoolean("isCached", false); // optional false

jsonObjectProperties.AddBoolean("ignoreCache", false); // optional false

jsonObjectProperties.AddBoolean("clientCachingAllowed", false); // optional true

jsonObjectProperties.AddString("cacheDir", @"C:\arcgisserver\directories\arcgiscache"); // optional

jsonObject.AddJsonObject("properties", jsonObjectProperties);

string result = this.GetResult(serviceUrl, "service=" + HttpUtility.UrlEncode(jsonObject.ToJson()) + "&f=json&token=" + token);

return this.HasSuccess(result);

            }

catch

            {

return false;

            }

        }

///<summary>

/// check is status is equal success

///</summary>

///<param name="result">result of request</param>

///<returns>True if status is equal success</returns>

private bool HasSuccess(string result)

        {

JsonObject jsonObject = new JsonObject(result);

string status = null;

if (!jsonObject.Exists("status") || !jsonObject.TryGetString("status", out status))

            {

return false;

            }

return status == "success";

        }

///<summary>

/// check is status is equal error

///</summary>

///<param name="result">result of request</param>

///<returns>True if status is equal error</returns>

private bool HasError(string result)

        {

JsonObject jsonObject = new JsonObject(result);

string status = null;

if (!jsonObject.Exists("status") || !jsonObject.TryGetString("status", out status))

            {

return false;

            }

return status == "error";

        }

///<summary>

/// Get request rest

///</summary>

///<param name="url">url of request</param>

///<returns>return response</returns>

private string GetResult(string url)

        {

try

            {

WebRequest request = WebRequest.Create(url);

WebResponse response = request.GetResponse();

using (Stream responseStream = response.GetResponseStream())

                {

using (StreamReader reader = new StreamReader(responseStream))

                    {

return reader.ReadToEnd();

                    }

                }

            }

catch

            {

throw;

            }

        }

///<summary>

/// Post request rest

///</summary>

///<param name="url">url of request</param>

///<param name="postContent">content of post</param>

///<returns>return response</returns>

private string GetResult(string url, string postContent)

        {

try

            {

WebRequest request = WebRequest.Create(url);

byte[] content = Encoding.UTF8.GetBytes(postContent);

request.ContentLength = content.Length;

request.ContentType = "application/x-www-form-urlencoded";

request.Method = WebRequestMethods.Http.Post;

using (Stream requestStream = request.GetRequestStream())

                {

requestStream.Write(content, 0, content.Length);

requestStream.Close();

WebResponse response = request.GetResponse();

using (Stream responseStream = response.GetResponseStream())

                    {

using (StreamReader reader = new StreamReader(responseStream))

                        {

return reader.ReadToEnd();

                        }

                    }

                }

            }

catch

            {

throw;

            }

        }

///<summary>

/// Generate a token

///</summary>

///<returns>A token that has default expiration time</returns>

private string GenerateAGSToken()

        {

try

            {

string urlGenerateToken = string.Format("{0}/generateToken", this.urlRestAdmin);

string credential = string.Format("username={0}&password={1}&client=requestip&expiration=&f=json", this.username, this.password);

string result = this.GetResult(urlGenerateToken, credential);

JsonObject jsonObject = new JsonObject(result);

string token = null;

if (!jsonObject.Exists("token") || !jsonObject.TryGetString("token", out token))

                {

throw new Exception("Token not found!");

                }

return token;

            }

catch

            {

return string.Empty;

            }

        }

    }

}

ArcGIS Engine 10.2 如何发布服务的更多相关文章

  1. vs2012 arcgis engine 10 丢失arcgis模板

    1.Visual Studio 2012环境下安装ArcGIS Engine 10 Visual Studio 2012环境下安装ArcObject SDK for the Microsoft .Ne ...

  2. VS2010下WPF开发ARCGIS ENGINE 10的带Ribbon控件项目

    原文 http://blog.sina.com.cn/s/blog_47522f7f0100nq5t.html 题目好长,但是集目前最新的工具于一身..VS是最新的2010版,不过用的是.net3.5 ...

  3. ArcGIS SDE 10.1 for Postgresql 服务连接配置

    去年写了ArcGIS 10.1 如何连接Postgresql 数据库(http://blog.csdn.net/arcgis_all/article/details/8202709)当时采用的也是Ar ...

  4. [ArcGIS]ArcGIS Server环境搭建,发布服务,以及使用ArcGIS API for JavaScript

    环境搭建 安装Web服务器 IIS 控制面板-程序-程序和功能-启用或关闭Windows功能,勾选以下 安装VisualStudio,选择包括ASP.NET模块 安装ArcGIS服务器 ArcGIS ...

  5. 基于Arcgis Engine 10.2(C#)+PostgreSQL 11(Postgis 3)+pgRouting 3.0实现使用数据库进行路径规划

    前言:最近在(被迫)使用ArcGIS Engine10.2(.NET平台)进行二次开发(桌面应用),因为想做一个最短路径查询的功能,而arcgis的网络分析又比较麻烦,于是想到了使用Postgis.但 ...

  6. ArcGIS Engine 10.x许可代码

    相比9.3,10.x许可代码的书写改变了,ArcObjects SDK 10 Microsoft .NET Framework 帮助文档中,提供了以下两种方式: 1. Calling RuntimeM ...

  7. Arcgis Server 10.2默认服务端口号修改方法

    本人安装Arcgis Server 10.2之后发布了一个地图服务,该服务默认使用的端口号是6080,本人使用的是教育网,使用教育网均能正常使用该服务,但是使用电信或者移动网络均不能正常访问该网站. ...

  8. ArcGIS Server 10 Java 版的Rest服务的部署方法

    使用ArcGIS Server 10 Java版发布GIS服务,当使用ArcGIS Manager创建好服务后,然后打开“ArcGIS Services Directory”的链接时发现网页报出了找不 ...

  9. 【149】ArcGIS Desktop 10.0 & Engine 10.0 安装及破解

    写在前面:可能会出现按照此方法无法破解的情况,那请确保您有将 ArcGIS 10.0 已经完全卸载干净,直接通过控制面板进行卸载的时候并不能将其卸载干净,需要进行更深层次的卸载,包括删除注册表,各种文 ...

随机推荐

  1. linux AC的应用详解

    NAME ac - 输出用户连接时间 总览 ac [ -d | --daily-totals ] [ -y | --print-year ] [ -p | --individual-totals ] ...

  2. uva1612 Guess

    和cf的打分有点像啊 因为一共只有三道题,所以每个人的得分最多有8种可能性.把这8种可能性都算出来,存在数组里,排好序备用排名就是一个天然的链表,给出了扫描的顺序扫描时,维护两个变量:前一个playe ...

  3. centos7 搭建jenkins

    centos7 搭建jenkins.note 环境:VMware 虚拟机 centos 7+ jdk 1.8+ tomcat7+jenkins搭建好linux 服务器后,关闭防火墙 停止firewal ...

  4. HTML基础(一)

    什么是HTMLHTML Hypertext Markup Language:即超文本标记语言 HTML特点 1.HTML不需要编译,直接由浏览器执行 2.HTML文件是一个文本文件 3.HTML文件必 ...

  5. 雷林鹏分享:Lua 数据类型

    Lua是动态类型语言,变量不要类型定义,只需要为变量赋值. 值可以存储在变量中,作为参数传递或结果返回. Lua中有8个基本类型分别为:nil.boolean.number.string.userda ...

  6. 连接MySQL错误“plugin caching_sha2_password could not be loaded”的解决办法

    MySQL新版默认使用caching_sha2_password作为身份验证插件,而旧版是使用mysql_native_password.当连接MySQL时报错“plugin caching_sha2 ...

  7. SecureCRT 64位 破解版v8.1.4

    http://www.xue51.com/soft/1510.html#xzdz securecrt 破解版是一款支持SSH1和SSH2的终端仿真程序,这个程序能够在windows系统中登陆UNIX或 ...

  8. 文件默认权限umask掩码

    umask命令 作用:用于显示.设置文件的缺省权限 格式:umask   [-S] -S表示以rwx形式显示新建文件缺省权限 系统的默认掩码是0022 文件创建时的默认权限 = 0666 - umas ...

  9. mysql查询表中最小可用id值

    今天在看实验室的项目时,碰到的一个问题,.先把sql语句扔出来 // 这条语句在id没有1时,不能得到正确的查询结果. select min(id+1) from oslist c where not ...

  10. InnoDB INFORMATION_SCHEMA System Tables

    InnoDB INFORMATION_SCHEMA System Tables 可以使用InnoDB INFORMATION_SCHEMA系统表提取有关InnoDB管理的schema对象的元数据. 此 ...