使用工具:Visual Studio 2010
目标框架:.NET Framework 3.5
新建项目 -> ArcGIS -> Server Object Extensions -> REST Template
以下为REST模板的内容解析:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Server;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.SOESupport;
//TODO: sign the project (project properties > signing tab > sign the assembly)
// this is strongly suggested if the dll will be registered using regasm.exe <your>.dll /codebase
namespace RestSOE1
{
[ComVisible(true)]
[Guid("f732b972-fd0b-4417-a39d-f72d3e21063b")]
[ClassInterface(ClassInterfaceType.None)]
[ServerObjectExtension("MapServer",
AllCapabilities = "",
DefaultCapabilities = "",
Description = "Insert SOE Description here",
DisplayName = "RestSOE1",
Properties = "",
SupportsREST = true,
SupportsSOAP = false)]
public class RestSOE1 : IServerObjectExtension, IObjectConstruct, IRESTRequestHandler
{
private string soe_name;
private IPropertySet configProps;
private IServerObjectHelper serverObjectHelper;
private ServerLogger logger;
private IRESTRequestHandler reqHandler;
public RestSOE1()
{
soe_name = this.GetType().Name;
// ServerLogger():日志记录对象,对SOE进行信息记录,可通过Manager的日志查看
logger = new ServerLogger();
reqHandler = new SoeRestImpl(soe_name, CreateRestSchema()) as IRESTRequestHandler;
}
#region IServerObjectExtension Members
//SOE初始化
public void Init(IServerObjectHelper pSOH)
{
//生命周期开始时调试
System.Diagnostics.Debugger.Launch();
serverObjectHelper = pSOH;
}
/// <summary>
/// 在服务器关闭时调用,可在该方法中释放SOE中使用的资源
/// </summary>
public void Shutdown()
{
}
#endregion
#region IObjectConstruct Members
// 该方法在Init方法执行后立即被执行,如果SOE有配置属性,就可以通过该方法的参数得到
// 只调用一次,可将SOE中比较耗费资源的逻辑写在该方法中
public void Construct(IPropertySet props)
{
configProps = props;
}
#endregion
#region IRESTRequestHandler Members
public string GetSchema()
{
// 以JSON格式返回SOE资源
return reqHandler.GetSchema();
}
public byte[] HandleRESTRequest(string Capabilities, string resourceName, string operationName, string operationInput, string outputFormat, string requestProperties, out string responseProperties)
{
// operationName:操作名称
// 若operationName参数为空字符串,则作用为获取资源在实例级别的描述
// 若operationName参数不为空字符串,则作用为回调资源和操作的方法
// Capabilities:一组资源授权的操作,可以为空字符串
// resourceName:资源名称,空字符串表示根级别,子资源通过'/'表示
// operationInput:操作的参数,JSON格式
// outputFormat:客户端请求的输出格式,如JSON、AMF等
// responseProperties:通过操作返回一组键值对,逗号分开
return reqHandler.HandleRESTRequest(Capabilities, resourceName, operationName, operationInput, outputFormat, requestProperties, out responseProperties);
}
#endregion
// 创建了REST资源和REST资源操作
private RestResource CreateRestSchema()
{
RestResource rootRes = new RestResource(soe_name, false, RootResHandler);
// RestOperation类,对应了一个SOE操作
// 对应参数含义为:操作名称,参数,支持的格式,该操作的处理函数
RestOperation sampleOper = new RestOperation("sampleOperation",
new string[] { "parm1", "parm2" },
new string[] { "json" },
SampleOperHandler);
rootRes.operations.Add(sampleOper);
return rootRes;
}
// 处理资源方法
private byte[] RootResHandler(NameValueCollection boundVariables, string outputFormat, string requestProperties, out string responseProperties)
{
responseProperties = null;
JsonObject result = new JsonObject();
result.AddString("hello", "world");
return Encoding.UTF8.GetBytes(result.ToJson());
}
// 资源操作方法
// 操作的handler是整个SOE的核心部分
private byte[] SampleOperHandler(NameValueCollection boundVariables,
JsonObject operationInput,
string outputFormat,
string requestProperties,
out string responseProperties)
{
responseProperties = null;
string parm1Value;
bool found = operationInput.TryGetString("parm1", out parm1Value);
if (!found || string.IsNullOrEmpty(parm1Value))
throw new ArgumentNullException("parm1");
string parm2Value;
found = operationInput.TryGetString("parm2", out parm2Value);
if (!found || string.IsNullOrEmpty(parm2Value))
throw new ArgumentNullException("parm2");
JsonObject result = new JsonObject();
result.AddString("parm1", parm1Value);
result.AddString("parm2", parm2Value);
return Encoding.UTF8.GetBytes(result.ToJson());
}
/* SOE里面的很大一部分代码是和AO对象打交道
* 而这部分也是核心功能
* 当使用Esri提供给的SOE模板之后
* 要核心要做的就是写SOE请求函数
* 在这个请求函数也就是handler中
* 核心AO代码就在这里 */
/* 其实如果在ArcGIS Engine中做过和线性参考相关的工作
* 那么这端代码是完全可以在ArcGIS Engine中使用的
* 所以说SOE的开发并不是想象中的困难,相反如果有了AO基础的话
* 的确是很简单 */
}
}
内容可参照:https://wenku.baidu.com/view/d7b4134658f5f61fb73666e5.html