Local server feature layer

View on GitHubSample viewer app

Start a local feature service and display its features in a map.

Image of local server feature layer

Use case

For executing offline geoprocessing tasks in your applicationss via an offline (local) server.

How to use the sample

A Local Server and Local Feature Service will automatically be started. Once started then a FeatureLayer will be created and added to the map.

How it works

  1. Create and run a local server with LocalServer.Instance.
  2. Start the server asynchronously with Server.StartAsync().
  3. Create and run a local feature service.
    1. Instantiate LocalFeatureService(Url) to create a local feature service with the given url path to mpk file.
    2. Start the service asynchronously with LocalFeatureService.StartAsync(). The service will be added to the local server automatically.
  4. Create an event handler for the LocalFeatureService.StatusChanged event. This will run whenever the status of the local service has changed.
  5. When the service's status has changed to Started, create a feature layer from local feature service.
    1. Create a ServiceFeatureTable(Url) using the URL for the feature layer.
    2. Create feature layer from service feature table using new FeatureLayer(ServiceFeatureTable).
    3. Load the layer asynchronously using FeatureLayer.LoadAsync().
  6. Add feature layer to map using Map.OperationalLayers.Add(FeatureLayer).

Relevant API

  • FeatureLayer
  • LocalFeatureService
  • LocalServer
  • LocalServerStatus
  • StatusChangedEvent

Offline data

This sample downloads the following items from ArcGIS Online automatically:

Additional information

Local Server can be downloaded for Windows and Linux platforms from the developers website. Local Server is not supported on macOS.

Tags

feature service, local, offline, server, service

Sample Code

LocalServerFeatureLayer.xaml.csLocalServerFeatureLayer.xaml.csLocalServerFeatureLayer.xaml
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2021 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.

using ArcGIS.Samples.Managers;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.LocalServices;
using Esri.ArcGISRuntime.Mapping;
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;

namespace ArcGIS.WPF.Samples.LocalServerFeatureLayer
{
    [ArcGIS.Samples.Shared.Attributes.Sample(
        name: "Local server feature layer",
        category: "Local Server",
        description: "Start a local feature service and display its features in a map.",
        instructions: "A Local Server and Local Feature Service will automatically be started. Once started then a `FeatureLayer` will be created and added to the map.",
        tags: new[] { "feature service", "local", "offline", "server", "service" })]
    [ArcGIS.Samples.Shared.Attributes.OfflineData("92ca5cdb3ff1461384bf80dc008e297b")]
    public partial class LocalServerFeatureLayer
    {
        // Hold a reference to the local feature service; the ServiceFeatureTable will be loaded from this service
        private LocalFeatureService _localFeatureService;

        public LocalServerFeatureLayer()
        {
            InitializeComponent();

            // Create the UI, setup the control references and execute initialization
            _ = Initialize();
        }

        private async Task Initialize()
        {
            // Create a map and add it to the view
            MyMapView.Map = new Map(BasemapStyle.ArcGISStreetsRelief);

            try
            {
                // LocalServer must not be running when setting the data path.
                if (LocalServer.Instance.Status == LocalServerStatus.Started)
                {
                    await LocalServer.Instance.StopAsync();
                }

                // Set the local data path - must be done before starting. On most systems, this will be C:\EsriSamples\AppData.
                // This path should be kept short to avoid Windows path length limitations.
                string tempDataPathRoot = Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.Windows)).FullName;
                string tempDataPath = Path.Combine(tempDataPathRoot, "EsriSamples", "AppData");
                Directory.CreateDirectory(tempDataPath); // CreateDirectory won't overwrite if it already exists.
                LocalServer.Instance.AppDataPath = tempDataPath;

                // Start the local server instance
                await LocalServer.Instance.StartAsync();
            }
            catch (Exception ex)
            {
                var localServerTypeInfo = typeof(LocalMapService).GetTypeInfo();
                var localServerVersion = FileVersionInfo.GetVersionInfo(localServerTypeInfo.Assembly.Location);

                MessageBox.Show($"Please ensure that local server {localServerVersion.FileVersion} is installed prior to using the sample. The download link is in the description. Message: {ex.Message}", "Local Server failed to start");
                return;
            }

            // Load the sample data and get the path
            string myfeatureServicePath = GetFeatureLayerPath();

            // Create the feature service to serve the local data
            _localFeatureService = new LocalFeatureService(myfeatureServicePath);

            // Listen to feature service status changes
            _localFeatureService.StatusChanged += _localFeatureService_StatusChanged;

            // Start the feature service
            try
            {
                await _localFeatureService.StartAsync();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "The feature service failed to load");
            }
        }

        private async void _localFeatureService_StatusChanged(object sender, StatusChangedEventArgs e)
        {
            // Load the map from the service once ready
            if (e.Status == LocalServerStatus.Started)
            {
                // Get the path to the first layer - the local feature service url + layer ID
                string layerUrl = _localFeatureService.Url + "/0";

                // Create the ServiceFeatureTable
                ServiceFeatureTable myFeatureTable = new ServiceFeatureTable(new Uri(layerUrl));

                // Create the Feature Layer from the table
                FeatureLayer myFeatureLayer = new FeatureLayer(myFeatureTable);

                // Add the layer to the map
                MyMapView.Map.OperationalLayers.Add(myFeatureLayer);

                try
                {
                    // Wait for the layer to load
                    await myFeatureLayer.LoadAsync();

                    // Set the viewpoint on the MapView to show the layer data
                    await MyMapView.SetViewpointGeometryAsync(myFeatureLayer.FullExtent, 50);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "Error");
                }
            }
        }

        private static string GetFeatureLayerPath()
        {
            return DataManager.GetDataFolder("92ca5cdb3ff1461384bf80dc008e297b", "PointsOfInterest.mpkx");
        }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.