So after tons of searching and bothersome problems, It took a while to find code, but none could be found. So I wanted to make sure the end example was easily findable on the internet next time. Hope this saves some time of another programmer out there!
This code will publish an .MXD map to the ArcGIS server. And turn it on to be used right away.
//The following C# code shows how to connect to the ArcGIS Server called " ",
//and use the IServerObjectConfiguration interface to set the properties of
//a new server object configuration. The new configuration is created and
// added to the server with the
//CreateConfiguration and AddConfiguration methods on IServerObjectAdmin.
/// <summary>
/// This publishes a Map to the ArcGIS Server
/// </summary>
/// <param name="serverMachineName">Servername</param>
/// <param name="serviceName">Service name in which you want to call it</param>
/// <param name="pathToMap">File Path to the .mxd map.</param>
/// <param name="format">Which format you would like. Look in code.</param>
public static void AddMapService(string serverMachineName, string serviceName, string pathToMap, int format)
{
// Initialize ESRI licenses
IAoInitialize aoInit = new AoInitializeClass();
aoInit.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcServer);
//define paths for the onlineresource property in the extension properties
string resource = "http://" + serverMachineName + "/ArcGIS/services/" + serviceName + "/MapServer/WMSServer";
string resourceKML = "http://" + serverMachineName + "/ArcGIS/services/" + serviceName + "/MapServer/KMLServer";
string resourceWCS = "http://" + serverMachineName + "/ArcGIS/services/" + serviceName + "/MapServer/WCSServer";
string resourceWFS = "http://" + serverMachineName + "/ArcGIS/services/" + serviceName + "/MapServer/WFSServer";
// Connect to the ArcGIS server called serverMachineName (string) .
IGISServerConnection pGISServerConnection = new GISServerConnectionClass();
// example : pGISServerConnection.Connect("llv38-tasc65688");
pGISServerConnection.Connect(serverMachineName);
// create the new configuration
IServerObjectAdmin pServerObjectAdmin = pGISServerConnection.ServerObjectAdmin;
IServerObjectConfiguration pConfiguration = pServerObjectAdmin.CreateConfiguration();
IServerObjectConfiguration2 pConfiguration2 = (IServerObjectConfiguration2)pConfiguration;
IServerObjectConfiguration3 pConfiguration3 = (IServerObjectConfiguration3)pConfiguration;
// set the General Configuration Settings
pConfiguration.Name = serviceName; // the name of this configuration
pConfiguration.TypeName = "MapServer"; // the type of server object to be created
pConfiguration.IsPooled = true;
pConfiguration.MinInstances = 1;
pConfiguration.MaxInstances = 2;
pConfiguration.WaitTimeout = 60;
pConfiguration.UsageTimeout = 600;
pConfiguration.StartupType = esriStartupType.esriSTAutomatic;
pConfiguration.IsolationLevel = esriServerIsolationLevel.esriServerIsolationHigh;
// Set the configuration Properties of the MapServer Object
IPropertySet pProps = pConfiguration.Properties;
pProps.SetProperty("FilePath", pathToMap); // required property
pProps.SetProperty("OutputDir", "c:\\arcgisserver\\arcgisoutput");
string virtualOutDir = " http://" + serverMachineName + "/arcgisoutput";
pProps.SetProperty("VirtualOutputDir", virtualOutDir);
pProps.SetProperty("MaxImageHeight", "2048");
pProps.SetProperty("MaxRecordCount", "500");
pProps.SetProperty("MaxBufferCount", "100");
pProps.SetProperty("MaxImageWidth", "2048");
pConfiguration.Properties = pProps;
// Set the info segment of the MapServer Object properties
IPropertySet info = pConfiguration2.Info;
info.SetProperty("WebEnabled", "true");
info.SetProperty("WebCapabilities", "Map,Query,Data");
pConfiguration2.Info = info;
// Set the recycle properties of the MapSrver object
IPropertySet pProp = pConfiguration.RecycleProperties;
pProp.SetProperty("StartTime", "1:00 AM"); // start recycling at midnight
pProp.SetProperty("Interval", "86400"); // every 24 hours
pConfiguration.RecycleProperties = pProp;
bool enabled;
if (format == 1 || format == 15)
{ //Set WMS extension Properties
pConfiguration2.set_ExtensionEnabled("WmsServer", true);
IPropertySet pExtensionProps = pConfiguration2.get_ExtensionProperties("WmsServer");
pExtensionProps.SetProperty("OnlineResource", resource);
pExtensionProps.SetProperty("Name", "WMS");
pExtensionProps.SetProperty("Title", serviceName);
// pConfiguration2.set_ExtensionProperties("WmsServer", pExtensionProps);
IPropertySet pProp2 = pConfiguration2.get_ExtensionInfo("WmsServer");
pProp2.SetProperty("WebEnabled", "true");
pProp2.SetProperty("WebCapabilities", "Map,Query,Data");
pConfiguration2.set_ExtensionInfo("WmsServer", pProp2);
}
if (format == 2 || format == 15)
{ // Set KML extension properties
pConfiguration2.set_ExtensionEnabled("KMLServer", true);
IPropertySet pPropKML = pConfiguration2.get_ExtensionInfo("KMLServer");
pPropKML.SetProperty("WebEnabled", "true");
pPropKML.SetProperty("WebCapabilities", "SingleImage,SeparateImages,Vectors");
pConfiguration2.set_ExtensionInfo("KMLServer", pPropKML);
IPropertySet pExtensionPropKML = pConfiguration2.get_ExtensionProperties("KMLServer");
pExtensionPropKML.SetProperty("ImageSize", "1024");
pExtensionPropKML.SetProperty("FeatureLimit", "1000000");
pExtensionPropKML.SetProperty("Dpi", "96");
pExtensionPropKML.SetProperty("MinRefreshPeriod", "30");
pExtensionPropKML.SetProperty("UseDefaultSnippets", "false");
// pConfiguration2.set_ExtensionProperties("KMLServer", pExtensionPropKML);
}
if (format == 4 || format == 15)
{ //Set WCS extension Properties
pConfiguration2.set_ExtensionEnabled("WcsServer", true);
IPropertySet pExtensionPropsWCS = pConfiguration2.get_ExtensionProperties("WcsServer");
pExtensionPropsWCS.SetProperty("OnlineResource", resourceWCS);
pExtensionPropsWCS.SetProperty("Name", "WCS");
pExtensionPropsWCS.SetProperty("Title", serviceName);
// pConfiguration2.set_ExtensionProperties("WcsServer", pExtensionPropsWCS);
IPropertySet pPropWCS = pConfiguration2.get_ExtensionInfo("WcsServer");
pPropWCS.SetProperty("WebEnabled", "true");
pConfiguration2.set_ExtensionInfo("WcsServer", pPropWCS);
}
if (format == 8 || format == 15)
{ //Set WFS Extension Properties
pConfiguration2.set_ExtensionEnabled("WfsServer", true);
IPropertySet pExtensionPropsWFS = pConfiguration2.get_ExtensionProperties("WfsServer");
pExtensionPropsWFS.SetProperty("OnlineResource", resourceWFS);
pExtensionPropsWFS.SetProperty("Name", "WFS");
pExtensionPropsWFS.SetProperty("Title", serviceName);
pExtensionPropsWFS.SetProperty("AppSchemaURI", resourceWFS);
pExtensionPropsWFS.SetProperty("AppSchemaPrefix", serviceName);
// pConfiguration2.set_ExtensionProperties("WfsServer", pExtensionPropsWFS);
IPropertySet pPropWFS = pConfiguration2.get_ExtensionInfo("WfsServer");
pPropWFS.SetProperty("WebEnabled", "true");
pConfiguration2.set_ExtensionInfo("WfsServer", pPropWFS);
}
//' add the configuration to the server
pServerObjectAdmin.AddConfiguration(pConfiguration2);
pServerObjectAdmin.StartConfiguration(serviceName, "MapServer");
} // end method
If you liked this post, please be sure to subscribe to my
RSS Feed.