How to develop the REST SOE
This topic explains how a sample REST SOE might be developed.
About developing the REST SOE
This topic is the first in a series that walks through the process of writing and deploying the sample Spatial Query REST SOE. This section of the walkthrough explains how to get started developing the server object extension (SOE). It also provides complete code examples that you can paste into Visual Studio if you want to follow along. Although not every line of code can be discussed, the walkthrough points out the most important snippets.
Creating an SOE from the Visual Studio template
When you install the ArcGIS Enterprise SDK, you get a template you can use to begin developing a REST SOE. It is strongly recommended that you start with the template.
Do the following steps to begin developing a new SOE with the template:
- Start Visual Studio.
- Click File, New, then Project. The New Project dialog box appears.
- Under the Installed Templates area, expand the Visual C#, ArcGIS, Server Object Extensions nodes.
- Click the template "REST SOE Template (ArcGIS Pro)".
- Type SpatialQueryREST in the Name field and click OK.
- In the Solution Explorer, double-click SpatialQueryREST.cs and review the template code.
As you review the template, you’ll notice a class already created that implements the necessary interfaces (that is, IServer
, IObject
, and IRESTRequest
). Within these implementations is the basic code required to make a REST SOE. You’ll also see some example resources and operations stubbed out for you that you delete when you write your SOE.
For more information about the structure of this template, see quick tour of the REST SOE.
Modifying the SOE attributes
The SOE attributes define which capabilities and properties the SOE supports, among other information. You need to modify the attributes to contain the display name for your SOE and the following two properties that this SOE supports:
- FieldName
- LayerName
You'll also set default values for those properties. If the server administrator wants to change the values from the defaults, do this in Manager after enabling the SOE on the service.
Find the following code:
Replace the preceding code with the following:
Adding variables
You need to add some variables for things you will work with throughout the SOE:
- Feature class to be queried
- Layer name of that feature class
- Field on which the area summary will be based
Near the top of the SpatialQueryREST class, find the following variable declarations:
Add three more variables below the preceding variables. See the following code example:
Examining the SOE constructor
Immediately below these declarations, you’ll see the SOE’s constructor that only contains a few lines of code. Do not change the template code for the constructor. If you have your initialization logic that should run when the SOE starts, put it in IObject
.
Implementing IServerObjectExtension
The members of IServer
have been stubbed out for you. You won’t change the Init method, but you can add some code to the Shutdown method to log the shutdown and clean up all your variables.
Find the following code:
Replace the preceding code with the following:
Implementing IObjectConstruct
IObject
contains one member, Construct
, that runs when the service instance is created. The Construct method is able to retrieve any SOE properties that were set in ArcGIS Pro or Manager. If you have any logic that needs to run only one time, the Construct method is the place to put it.
In this walkthrough, you’ll add some logic to the Construct
method to get the IFeature
interface that will be queried by the SOE. Since this SOE doesn’t allow end users to change the feature class they query, you don’t have to run this logic every time someone performs a query; you can just run it once when the service instance is created.
Find the following code for the Construct
method:
Replace the preceding code with the following:
The preceding code can be summarized as follows:
- Get all the layer information (
IMap
) from this map service (Layer Infos IMap
).Server - Iterate through all the layer information and get the layer of interest (which was designated in the SOE properties).
- Get the data source (
IFeature
) for the layer of interest.Class
Along the way, the SOE writes messages to the ArcGIS Server log files. Writing log messages can be an effective way to debug your SOE if you are having trouble stepping through the code.
In the preceding code, the IMapServerDataAccess interface allows this SOE to work with the data underlying the map service. You cannot use any MXD-specific interfaces such as IMap or ILayer to get to the data source; instead, you must use IMapServerDataAccess. For more information about this interface, see IMapServerDataAccess.
Implementing IRESTRequest Handler
Below IObject
you’ll see IRESTRequest
, which has the following methods:
Get
—You don't need to alter this code unless you want to put a log message here.Schema Handle
—You don't need to change this code either.RESTRequest
Modifying the Create Rest Schema
function
The Create
function builds the REST schema for the SOE. All REST SOEs have resources, which are pieces of information returned from the server, and operations, which are things that the server can do with your resource. For each REST SOE, you must programmatically piece together a schema of resources and operations that the SOE will support.
This example, like many REST SOEs, has a simple schema. Like all REST SOEs, it has a resource at the root level. From there, you can access one "SpatialQuery" operation.
In your REST SOE template, find the following Create
function:
The preceding code has an example root resource and one example operation stubbed out for you. The code you use to build your SOE schema follows this structure somewhat. For simplicity, delete the entire Create
function in the preceding code and replace it with the following:
The difference between the code you deleted and the code you added is the SpatialQuery operation (spatialQueryOper) that was created. To create an operation, create a RestOperation and supply the name you want for the operation, the parameters of the operation, the supported return formats, and the handler function that runs when someone invokes the operation.
You need to supply a handler function for each resource and operation that you put in your schema. These functions run when users invoke the resources and operations. You’ll work with the handler functions next.
Modifying the Root R e s Handler
function
The root resource of your SOE does not do anything special in this example, which is typical. You just need to remove the "hello world" example that is included in the REST SOE template. Find the following line of code in the Root
function and delete it:
Leave the remainder of the Root
function as is.
Modifying the operation handler function
The REST SOE template has a sample operation handler function, Sample
. You can remove this function because you added your operation to the schema. Find the following code and delete it:
Now, add your handler function for the SpatialQuery operation. Paste the following function where you just deleted the Sample
function:
Your operation handler must de-serialize the JavaScript Object Notation (JSON) input parameters, do something with those parameters, then return the result as JSON. To learn more about this, see working with JSON in a REST SOE.
In the preceding function, your operation is expecting a point and a distance as input parameters. Notice the call to Try
to retrieve the point, followed by the ToGeometry conversion to get it as an IPoint. This takes care of de-serializing the point. The distance is retrieved using Try
.
The preceding function runs a helper method, Query
, to make the query, summarize the areas, and bundle everything as JSON. It then returns that JSON to the user. The user’s client application (such as a Web application built with the ArcGIS application programming interface [API] for JavaScript) can try to draw some of the JSON response in the map or display it in a table.
Adding helper functions Query Point
and Create Json Records
Your handler function, Spatial
, uses the following helper functions:
Query
Point Create
Json Record
This is where all the ArcGIS Enterprise SDK code occurs to make the spatial query, add up the results, and package everything as JSON to return to the client.
Paste the following functions immediately after your Spatial
function:
By the time you get to these functions, you have an input point as an IPoint and a double representing the buffer distance. This is enough for you to start doing all the work you need in ArcGIS Enterprise SDK.
The function QueryPoint does the following:
- Buffers the input point by the user-specified distance
- Queries all the vegetation polygons that intersect the buffer
- Loops through each polygon and clips it to the buffer outline
- Converts the clipped polygon to a JSON object and adds it to a .NET list
- Sums the area of the clipped polygon and adds the area to a dictionary
- After looping through all polygons, serializes the clipped polygons and the area stats into one JSON object, and returns that JSON object to the user
To perform this last step of serialization, the SOE uses a helper method, Create
, that was written specifically for this SOE. This takes in the dictionary of area statistics and makes a JSON object from each record in the dictionary. Each of these JSON objects is added to an array, which is returned by the function.
Going back to the Query
function, notice how the final JSON object is created. A list of JSON objects containing all the clipped polygons and an array of JSON objects containing all the area records are added to a single parent JSON object for return to the client.
Signing and building the project
Once you’ve finished writing code, you need to sign and build the project. This creates a .soe file that helps you deploy the SOE to ArcGIS Server.
- In the Solution Explorer, right-click the SpatialQueryREST project and click Properties.
- Click the Signing tab and ensure the "Sign the assembly" check box is selected.
- In the "Choose a strong name key file" area, choose to use either the key file included with the template mykey.snk or create your own key. Click mykey.snk in the drop-down list to see the New and Browse options.
- Save your project.
- In the Solution Explorer, right-click your project and click Build. This creates SpatialQueryREST_ent.soe in your project's
bin\debug
orbin\release
folder.