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.SOEElevationLatLonDataContract"
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.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Windows.Controls;
using ESRI.ArcGIS.Client.Geometry;
namespace ArcGISWPFSDK
{
public partial class SOEElevationLatLonDataContract : UserControl
{
private static ESRI.ArcGIS.Client.Projection.WebMercator _mercator =
new ESRI.ArcGIS.Client.Projection.WebMercator();
public SOEElevationLatLonDataContract()
{
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
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += (s, a) =>
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ElevationSOELatLon));
ElevationSOELatLon elevationSOELatLon = serializer.ReadObject(a.Result) as ElevationSOELatLon;
a.Result.Close();
MyInfoWindow.Anchor = e.MapPoint;
MyInfoWindow.Content = string.Format("Elevation: {0} meters", elevationSOELatLon.elevation.ToString("0"));
MyInfoWindow.IsOpen = true;
};
webClient.OpenReadAsync(new Uri(SOEurl));
}
public class ElevationSOELatLon
{
public double elevation { get; set; }
}
}
}
Imports System
Imports System.Net
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Json
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client.Geometry
Namespace ArcGISWPFSDK
Partial Public Class SOEElevationLatLonDataContract
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 serializer As New DataContractJsonSerializer(GetType(ElevationSOELatLon))
Dim elevationSOELatLon As ElevationSOELatLon = TryCast(serializer.ReadObject(a.Result), ElevationSOELatLon)
a.Result.Close()
MyInfoWindow.Anchor = e.MapPoint
MyInfoWindow.Content = String.Format("Elevation: {0} meters", elevationSOELatLon.Elevation.ToString("0"))
MyInfoWindow.IsOpen = True
End Sub
webClient.OpenReadAsync(New Uri(SOEurl))
End Sub
<DataContract()>
Public Class ElevationSOELatLon
<DataMember(Name:="elevation")>
Public Property Elevation() As Double
End Class
End Class
End Namespace
5/16/2014