forked from Esri/arcgis-maps-sdk-dotnet-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalServerServices.xaml.cs
261 lines (222 loc) · 9.5 KB
/
LocalServerServices.xaml.cs
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright 2018 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 ArcGISRuntime.Samples.Managers;
using Esri.ArcGISRuntime.LocalServices;
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace ArcGISRuntime.WPF.Samples.LocalServerServices
{
[ArcGISRuntime.Samples.Shared.Attributes.Sample(
"Local Server services",
"Local Server",
"This sample demonstrates how to control local server and manage running services.",
"This sample depends on the local server being installed and configured. See https://developers.arcgis.com/net/latest/wpf/guide/local-server.htm for details and instructions. \n Sample data is downloaded automatically once local server is started. It may take some time for sample data to load. The list of services will be enabled once the download has finished.",
"Featured")]
[ArcGISRuntime.Samples.Shared.Attributes.OfflineData("dee5d8060a6048a4b063484199a9546b", "4e94fec734434d1288e6ebe36c3c461f", "da9e565a52ca41c1937cff1a01017068")]
public partial class LocalServerServices
{
// Hold references to the individual services
private LocalMapService _localMapService;
private LocalFeatureService _localFeatureService;
private LocalGeoprocessingService _localGeoprocessingService;
// Generic reference to the service selected in the UI
private LocalService _selectedService;
public LocalServerServices()
{
InitializeComponent();
// Set up the sample
Initialize();
}
private void Initialize()
{
try
{
// Subscribe to event notification for the local server instance
LocalServer.Instance.StatusChanged += (o, e) =>
{
UpdateUiWithServiceUpdate("Local Server", e.Status);
};
}
catch (Exception ex)
{
MessageBox.Show(String.Format("Please ensure that local server is installed prior to using the sample. See instructions in readme.md or metadata.json. Message: {0}", ex.Message), "Local Server failed to start");
}
}
private void UpdateUiWithServiceUpdate(string server, LocalServerStatus status)
{
// Construct the new status text
string updateStatus = String.Format("{0} status: {1} \t\t {2}\n{3}", server, status,
DateTime.Now.ToShortTimeString(), StatusTextbox.Text);
// Update the status box text
StatusTextbox.Text = updateStatus;
// Update the list of running services
ServicesListbox.ItemsSource = LocalServer.Instance.Services.Select(m => m.Name + " : " + m.Url);
}
private void CreateServices()
{
try
{
// Arrange the data before starting the services
string mapServicePath = GetMpkPath();
string featureServicePath = GetFeatureLayerPath();
string geoprocessingPath = GetGpPath();
// Create each service but don't start any
_localMapService = new LocalMapService(mapServicePath);
_localFeatureService = new LocalFeatureService(featureServicePath);
_localGeoprocessingService = new LocalGeoprocessingService(geoprocessingPath);
// Subscribe to status updates for each service
_localMapService.StatusChanged += (o, e) => { UpdateUiWithServiceUpdate("Map Service", e.Status); };
_localFeatureService.StatusChanged += (o, e) => { UpdateUiWithServiceUpdate("Feature Service", e.Status); };
_localGeoprocessingService.StatusChanged += (o, e) => { UpdateUiWithServiceUpdate("Geoprocessing Service", e.Status); };
// Enable the UI to select services
ServiceSelectionCombo.IsEnabled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Failed to create services");
}
}
private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Get the text of the selected item
string selection = ((ComboBoxItem)ServiceSelectionCombo.SelectedItem).Content.ToString();
// Update the selection
switch (selection)
{
case "Map Service":
_selectedService = _localMapService;
break;
case "Feature Service":
_selectedService = _localFeatureService;
break;
case "Geoprocessing Service":
_selectedService = _localGeoprocessingService;
break;
}
// Return if selection is invalid
if (_selectedService == null)
{
return;
}
// Update the state of the start and stop buttons
UpdateServiceControlUi();
}
private void UpdateServiceControlUi()
{
if (_selectedService == null)
{
return;
}
// Update the UI
if (_selectedService.Status == LocalServerStatus.Started)
{
ServiceStopButton.IsEnabled = true;
ServiceStartButton.IsEnabled = false;
}
else
{
ServiceStopButton.IsEnabled = false;
ServiceStartButton.IsEnabled = true;
}
}
private async void StartServiceButtonClicked(object sender, RoutedEventArgs e)
{
try
{
// Start the selected service
await _selectedService.StartAsync();
// Update the UI
UpdateServiceControlUi();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Failed to start service");
}
}
private async void StopServiceButtonClicked(object sender, RoutedEventArgs e)
{
try
{
// Stop the selected service
await _selectedService.StopAsync();
// Update the UI
UpdateServiceControlUi();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Failed to stop service");
}
}
private static string GetMpkPath()
{
return DataManager.GetDataFolder("dee5d8060a6048a4b063484199a9546b", "RelationshipID.mpk");
}
private static string GetFeatureLayerPath()
{
return DataManager.GetDataFolder("4e94fec734434d1288e6ebe36c3c461f", "PointsOfInterest.mpk");
}
private static string GetGpPath()
{
return DataManager.GetDataFolder("da9e565a52ca41c1937cff1a01017068", "Contour.gpk");
}
private async void StartServerButtonClicked(object sender, RoutedEventArgs e)
{
try
{
// Start the server
await LocalServer.Instance.StartAsync();
// Create the services
CreateServices();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Local Server Failed to start");
}
// Update the UI
LocalServerStopButton.IsEnabled = true;
LocalServerStartButton.IsEnabled = false;
}
private async void StopServerButtonClicked(object sender, RoutedEventArgs e)
{
// Update the UI
ServiceStartButton.IsEnabled = false;
ServiceStopButton.IsEnabled = false;
LocalServerStartButton.IsEnabled = true;
LocalServerStopButton.IsEnabled = false;
// Stop the server
await LocalServer.Instance.StopAsync();
// Clear references to the services
_localFeatureService = null;
_localMapService = null;
_localGeoprocessingService = null;
}
private void NavigateButtonClicked(object sender, RoutedEventArgs e)
{
// Return if selection is empty
if (ServicesListbox.SelectedItems.Count < 1) { return; }
try
{
// Get the full text in the selection
string strFullName = ServicesListbox.SelectedItems[0].ToString();
// Create array of characters to split text by; ':' separates the service name and the URI
char[] splitChars = { ':' };
// Extract the service URL
string serviceUri = strFullName.Split(splitChars, 2)[1].Trim();
// Navigate to the service
System.Diagnostics.Process.Start(serviceUri);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Couldn't navigate to service");
}
}
}
}