This little bit of ArcGIS Server Object Extension code can take an ArcGIS Service based on an empty MXD, and dynamically add all the feature classes from an SDE instance to it. Its one of my first SOEs, but it seems to run just fine. Somewhat unique about this code is that this SOE has no callable methods, its mere presence is its power.
using System;
using System.Runtime.InteropServices;
using System.EnterpriseServices;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Server;
namespace com.wordpress.mapwrecker
{
[AutomationProxy(true), ClassInterface(ClassInterfaceType.None), GuidAttribute("a9ed9e17-d2b2-461c-9da3-2915af9a3f00")]
public class SDE_Eater : ServicedComponent, IServerObjectExtension, ILogSupport
{
private ESRI.ArcGIS.esriSystem.ILog2 m_log;
private object getSingleton(string progId)
{
return Activator.CreateInstance(Type.GetTypeFromProgID(progId));
}
public void addAllSdeLayersService(IServerObjectHelper m_SOH)
{
IWorkspaceFactory sdeWorkFact = null;
try
{
MapServer ms = m_SOH.ServerObject as MapServer;
IMapServerObjects mso = ms as IMapServerObjects;
IMap map = (IMap)mso.get_Map(ms.DefaultMapName);
sdeWorkFact = (IWorkspaceFactory)getSingleton("esriDataSourcesGDB.SdeWorkspaceFactory");
IPropertySet propertySet = getSdeProperties();
IWorkspace pFWorkspace = sdeWorkFact.Open(propertySet, 0);
IEnumDataset penumDataset = pFWorkspace.get_Datasets(esriDatasetType.esriDTFeatureClass);
IDataset pDataset = penumDataset.Next();
while (pDataset != null)
{
IFeatureLayer featureLayer = new FeatureLayer();
featureLayer.FeatureClass = (IFeatureClass)pDataset;
m_log.AddMessage(3, 110000, "SDE SOE found: " + pDataset.Name);
featureLayer.Name = pDataset.Name;
map.AddLayer(featureLayer);
pDataset = penumDataset.Next();
}
mso.RefreshServerObjects();
} catch (Exception e) {
m_log.AddMessage(1, 110000, "SDE SOE error: " + e.StackTrace);
} finally {
int refsLeft = 0;
do { refsLeft = Marshal.ReleaseComObject(sdeWorkFact); } while (refsLeft > 0);
}
}
private IPropertySet getSdeProperties()
{
IPropertySet propertySet = new PropertySet();
propertySet.SetProperty("SERVER", "blah");
propertySet.SetProperty("INSTANCE", "sde:sqlserver:blah");
propertySet.SetProperty("DATABASE", "SDE");
propertySet.SetProperty("USER", "blah");
propertySet.SetProperty("PASSWORD", "blah");
propertySet.SetProperty("VERSION", "sde.DEFAULT");
propertySet.SetProperty("AUTHENTICATION_MODE", "DBMS");
return propertySet;
}
public void Init(IServerObjectHelper pSOH)
{
m_log.AddMessage(2, 110000, "SDE SOE is loaded");
addAllSdeLayersService(pSOH);
}
public void Shutdown()
{
m_log = null;
}
public void InitLogging(ILog Log)
{
m_log = (ESRI.ArcGIS.esriSystem.ILog2)Log;
}
}
}
October 31, 2008 at 8:58 pm |
[...] MapWrecker 2.0 AJAX, Maps, .NET, and Destruction « AGS + SDE – MXD + SOE [...]