This sample demonstrates how to use the custom Server Object Extension (SOE) hosted by ArcGIS Server.
More information on SOEs can be found here.
The SOE is accessible via the ArcGIS for Server REST API. A web request is programmatically constructed by populating argument\value pairs to match parameters defined by the SOE's REST schema (discoverable via the ArcGIS for Server REST Services Directory).
The response from the SOE contains string data in JSON (JavaScript Object Notation) format. There are a variety of techniques for handling JSON in WPF. This sample uses the DataContractJsonSerializer Class to deserialize the JSON response from the server.
For more information on the DataContractJsonSerializer see http://msdn.microsoft.com/en-GB/library/system.runtime.serialization.json.datacontractjsonserializer(v=vs.100).aspx.
The types to which the JSON is deserialized can be automatically created based on the URL or the returned JSON by using a service such as Json2CSharp (http://json2csharp.com/).
Alternatively you can use the JavaScriptSerializer (http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer(v=vs.100).aspx)
or there are a number of 3rd party JSON libraries available such as Json.NET (http://james.newtonking.com/projects/json-net.aspx).
Download Sample Application
<UserControl x:Class="ArcGISWPFSDK.SOEElevationLatLonJsonObject"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:esri="http://schemas.esri.com/arcgis/client/2009">
<Grid x:Name="LayoutRoot">
<esri:Map x:Name="MyMap" UseAcceleratedDisplay="True" MouseClick="MyMap_MouseClick">
<esri:ArcGISTiledMapServiceLayer Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer" />
</esri:Map>
<esri:InfoWindow x:Name="MyInfoWindow"
Padding="2"
Background="LightYellow"
Map="{Binding ElementName=MyMap}" />
</Grid>
</UserControl>
using System;
using System.Net;
using System.Windows.Controls;
using ESRI.ArcGIS.Client.Geometry;
using System.Web.Script.Serialization;
using System.Collections.Generic;
namespace ArcGISWPFSDK
{
public partial class SOEElevationLatLonJsonObject : UserControl
{
private static ESRI.ArcGIS.Client.Projection.WebMercator _mercator =
new ESRI.ArcGIS.Client.Projection.WebMercator();
public SOEElevationLatLonJsonObject()
{
InitializeComponent();
}
private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
{
MapPoint geographicPoint = _mercator.ToGeographic(e.MapPoint) as MapPoint;
string SOEurl = "http://sampleserver4.arcgisonline.com/ArcGIS/rest/services/Elevation/ESRI_Elevation_World/MapServer/exts/ElevationsSOE/ElevationLayers/1/GetElevationAtLonLat";
SOEurl += string.Format(System.Globalization.CultureInfo.InvariantCulture, "?lon={0}&lat={1}&f=json", geographicPoint.X, geographicPoint.Y);
/*
* e.g. http://sampleserver4.arcgisonline.com/ArcGIS/rest/services/Elevation/ESRI_Elevation_World/MapServer/exts/ElevationsSOE/ElevationLayers/1/GetElevationAtLonLat?lon=0&lat=0&f=json
* {"elevation":-4929.5259200009168}
*/
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += (s, a) =>
{
var jss = new JavaScriptSerializer();
IDictionary<string, object> dictionary;
try
{
using (System.IO.StreamReader sreader = new System.IO.StreamReader(a.Result))
{
string jsonString = sreader.ReadToEnd();
dictionary = jss.DeserializeObject(jsonString) as IDictionary<string, object>;
}
}
catch (Exception)
{
dictionary = null;
}
double elevation = Convert.ToDouble(dictionary["elevation"]);
a.Result.Close();
MyInfoWindow.Anchor = e.MapPoint;
MyInfoWindow.Content = string.Format("Elevation: {0} meters", elevation.ToString("0"));
MyInfoWindow.IsOpen = true;
};
webClient.OpenReadAsync(new Uri(SOEurl));
}
}
}
Imports System
Imports System.Net
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client.Geometry
Imports System.Web.Script.Serialization
Imports System.Collections.Generic
Namespace ArcGISWPFSDK
Partial Public Class SOEElevationLatLonJsonObject
Inherits UserControl
Private Shared _mercator As New ESRI.ArcGIS.Client.Projection.WebMercator()
Public Sub New()
InitializeComponent()
End Sub
Private Sub MyMap_MouseClick(ByVal sender As Object, ByVal e As ESRI.ArcGIS.Client.Map.MouseEventArgs)
Dim geographicPoint As MapPoint = TryCast(_mercator.ToGeographic(e.MapPoint), MapPoint)
Dim SOEurl As String = "http://sampleserver4.arcgisonline.com/ArcGIS/rest/services/Elevation/ESRI_Elevation_World/MapServer/exts/ElevationsSOE/ElevationLayers/1/GetElevationAtLonLat"
SOEurl &= String.Format(System.Globalization.CultureInfo.InvariantCulture, "?lon={0}&lat={1}&f=json", geographicPoint.X, geographicPoint.Y)
Dim webClient As New WebClient()
AddHandler webClient.OpenReadCompleted,
Sub(s, a)
Dim jss = New JavaScriptSerializer()
Dim dictionary As IDictionary(Of String, Object)
Try
Using sreader As New System.IO.StreamReader(a.Result)
Dim jsonString As String = sreader.ReadToEnd()
dictionary = TryCast(jss.DeserializeObject(jsonString), IDictionary(Of String, Object))
End Using
Catch generatedExceptionName As Exception
dictionary = Nothing
End Try
Dim elevation As Double = Convert.ToDouble(dictionary("elevation"))
a.Result.Close()
MyInfoWindow.Anchor = e.MapPoint
MyInfoWindow.Content = String.Format("Elevation: {0} meters", elevation.ToString("0"))
MyInfoWindow.IsOpen = True
End Sub
webClient.OpenReadAsync(New Uri(SOEurl))
End Sub
End Class
End Namespace
5/16/2014