This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
201 lines (181 loc) · 6.54 KB
/
Program.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
/*
ArcGIS Service Non-Direct-Connect Checker
Copyright (C) 2011 Washington State Department of Transportation
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Web.Script.Serialization;
using ArcGisServiceDCChecker.Properties;
using ESRI.ArcGIS.ADF.Connection.AGS;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Server;
namespace ArcGisServiceDCChecker
{
class Program
{
private static LicenseInitializer _aoLicenseInitializer = new ArcGisServiceDCChecker.LicenseInitializer();
private static readonly string
_jsonFile = Settings.Default.OutputJson,
_csvFile = Settings.Default.OutputCsv;
[STAThread()]
static void Main(string[] args)
{
Dictionary<string, List<MapServiceInfo>> output;
JavaScriptSerializer serializer;
// Get the server names from the config.
string[] servers = Settings.Default.Servers.Split(',');
Console.Error.WriteLine("Getting license...");
//ESRI License Initializer generated code.
if (!_aoLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeArcView },
new esriLicenseExtensionCode[] { }))
{
Console.Error.WriteLine(_aoLicenseInitializer.LicenseMessage());
Console.Error.WriteLine("This application could not initialize with the correct ArcGIS license and will shutdown.");
_aoLicenseInitializer.ShutdownApplication();
return;
}
Console.Error.WriteLine("License acquired.");
output = CollectMapServerInfo(servers);
serializer = new JavaScriptSerializer();
string json = serializer.Serialize(output);
File.WriteAllText(_jsonFile, json);
// Write to CSV...
using (StreamWriter writer = new StreamWriter(_csvFile))
{
output.ToCsv(writer);
}
//ESRI License Initializer generated code.
//Do not make any call to ArcObjects after ShutDownApplication()
_aoLicenseInitializer.ShutdownApplication();
}
private static Dictionary<string, List<MapServiceInfo>> CollectMapServerInfo(IEnumerable<string> servers)
{
Dictionary<string, List<MapServiceInfo>> output;
output = new Dictionary<string, List<MapServiceInfo>>();
AGSServerConnection connection = null;
IServerObjectManager4 som = null;
IServerContext ctxt = null;
IMapServer mapServer = null;
IEnumServerObjectConfigurationInfo socInfos;
IServerObjectConfigurationInfo2 socInfo;
MapServiceInfo mapServiceInfo;
try
{
// Loop through all of the hosts (servers) and create a div for each one containing map service test results.
foreach (var host in servers)
{
var mapServiceInfos = new List<MapServiceInfo>();
output.Add(host, mapServiceInfos);
// Create the connection object
connection = new AGSServerConnection(host, null, false, true);
try
{
// Attempt to connect to the server.
connection.Connect(false);
if (connection.IsConnected)
{
// Get the Server Object Manager (SOM) for the current ArcGIS Server.
som = (IServerObjectManager4)connection.ServerObjectManager;
// Get a list of the services on the server.
socInfos = som.GetConfigurationInfos();
// Get the first service from the list.
socInfo = socInfos.Next() as IServerObjectConfigurationInfo2;
// Loop through the list of services...
while (socInfo != null)
{
mapServiceInfo = new MapServiceInfo { MapServiceName = socInfo.Name };
try
{
// Proceed only if the current service is a "MapServer".
if (string.Compare(socInfo.TypeName, "MapServer", true) == 0)
{
// Create a div for the current map service.
Console.Error.WriteLine("{0}", socInfo.Name);
try
{
// Create a server context for the current map service.
ctxt = som.CreateServerContext(socInfo.Name, socInfo.TypeName);
// Cast the context object to an IMapServer.
mapServer = (IMapServer)ctxt.ServerObject;
// Get the document name
IMapServerInit2 msInit = null;
msInit = mapServer as IMapServerInit2;
string sourceDocName = msInit != null ? msInit.FilePath : null;
if (sourceDocName != null)
{
mapServiceInfo.SourceDocumentPath = sourceDocName;
}
// Create a dictionary of the properties for all of the layers in the map service.
mapServiceInfo.ConnectionProperties = mapServer.GetConnectionProperties();
}
catch (COMException comEx)
{
// See if the exception was caused by a stopped service. Just write the message in this case.
if (comEx.ErrorCode == -2147467259)
{
mapServiceInfo.ErrorMessage = comEx.Message;
}
else
{
mapServiceInfo.ErrorMessage = string.Format("{0}: {1}", comEx.ErrorCode, comEx.Message);
}
}
}
}
catch (Exception ex)
{
mapServiceInfo.ErrorMessage = ex.ToString();
}
finally
{
// Release the server context.
if (ctxt != null)
{
ctxt.ReleaseContext();
}
// Go to the next soc info.
socInfo = socInfos.Next() as IServerObjectConfigurationInfo2;
}
mapServiceInfos.Add(mapServiceInfo);
}
}
}
finally
{
connection.Dispose();
}
}
}
finally
{
// Release any COM objects that have been created.
if (mapServer != null)
{
Marshal.ReleaseComObject(mapServer);
}
if (ctxt != null)
{
Marshal.ReleaseComObject(ctxt);
}
if (som != null)
{
Marshal.ReleaseComObject(som);
}
}
return output;
}
}
}