Created
July 7, 2013 13:52
-
-
Save stoolrossa/5943529 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Collections.Specialized; | |
using System.Runtime.InteropServices; | |
using Castle.Windsor; | |
using Castle.Windsor.Installer; | |
using ESRI.ArcGIS.esriSystem; | |
using ESRI.ArcGIS.Server; | |
using ESRI.ArcGIS.Geometry; | |
using ESRI.ArcGIS.Geodatabase; | |
using ESRI.ArcGIS.Carto; | |
using ESRI.ArcGIS.SOESupport; | |
using Blog.SoeExample.Service; | |
namespace Blog.SoeExample | |
{ | |
[ComVisible(true)] | |
[Guid("39bd6f7c-5c41-423f-ba67-dee2f3b166ed")] | |
[ClassInterface(ClassInterfaceType.None)] | |
[ServerObjectExtension("ExampleSoe", | |
AllCapabilities="", | |
DefaultCapabilities="", | |
Description="Example of using IoC in a Server Object Extension", | |
DisplayName="Blog.SoeExample.ExampleSoe", | |
Properties="", | |
SupportsREST=true, | |
SupportsSOAP=false)] | |
public class ExampleSoe : IServerObjectExtension, IObjectConstruct, IRESTRequestHandler | |
{ | |
private string soeName; | |
private IPropertySet configProps; | |
private IServerObjectHelper serverObjectHelper; | |
private ServerLogger logger; | |
private IRESTRequestHandler reqHandler; | |
private IMapServerDataAccess mapServerDataAccess; | |
private IMapLayerInfos layerInfos; | |
public ExampleSoe() | |
{ | |
soeName = this.GetType().Name; | |
logger = new ServerLogger(); | |
reqHandler = new SoeRestImpl(soeName, CreateRestSchema()) as IRESTRequestHandler; | |
} | |
#region IServerObjectExtension Members | |
public void Init(IServerObjectHelper serverObjectHelper) | |
{ | |
this.serverObjectHelper = serverObjectHelper; | |
this.mapServerDataAccess = (IMapServerDataAccess)this.serverObjectHelper.ServerObject; | |
IMapServer3 ms = (IMapServer3)this.mapServerDataAccess; | |
IMapServerInfo mapServerInfo = ms.GetServerInfo(ms.DefaultMapName); | |
this.layerInfos = mapServerInfo.MapLayerInfos; | |
} | |
public void Shutdown() | |
{ | |
// release the IoC container | |
container.Dispose(); | |
} | |
#endregion | |
#region IObjectConstruct Members | |
public void Construct(IPropertySet props) | |
{ | |
configProps = props; | |
bootstrapContainer(); | |
} | |
#endregion | |
#region IRESTRequestHandler Members | |
public string GetSchema() | |
{ | |
return reqHandler.GetSchema(); | |
} | |
public byte[] HandleRESTRequest(string Capabilities, string resourceName, string operationName, string operationInput, string outputFormat, string requestProperties, out string responseProperties) | |
{ | |
return reqHandler.HandleRESTRequest(Capabilities, resourceName, operationName, operationInput, outputFormat, requestProperties, out responseProperties); | |
} | |
#endregion | |
#region Inversion of Control | |
private static IWindsorContainer container; | |
private static void bootstrapContainer() | |
{ | |
// create the IoC container instance | |
container = new WindsorContainer().Install(FromAssembly.This()); | |
} | |
private IExampleService createService() | |
{ | |
// get the feature class as an IDataSet | |
var featureClassDataSet = (IDataset)this.mapServerDataAccess.GetDataSource(((IMapServer3)this.mapServerDataAccess).DefaultMapName, 1); | |
// resolve the dependencies of the service and create an instance of it | |
IExampleService exampleService = container.Resolve<IExampleService>(); | |
// set the workspace dependency as a property | |
exampleService.Workspace = featureClassDataSet.Workspace; | |
return exampleService; | |
} | |
#endregion | |
private RestResource CreateRestSchema() | |
{ | |
RestResource rootRes = new RestResource(soeName, false, RootResHandler); | |
RestOperation getLandUseSelectionOperation = new RestOperation("getLandUseSelection", | |
new string[] { "landUse" }, | |
new string[] { "json" }, | |
getLandUseSelectionOperationHandler); | |
rootRes.operations.Add(getLandUseSelectionOperation); | |
return rootRes; | |
} | |
private byte[] RootResHandler(NameValueCollection boundVariables, string outputFormat, string requestProperties, out string responseProperties) | |
{ | |
responseProperties = null; | |
JsonObject result = new JsonObject(); | |
return Encoding.UTF8.GetBytes(result.ToJson()); | |
} | |
// the operation handler for our method | |
private byte[] getLandUseSelectionOperationHandler(NameValueCollection boundVariables, | |
JsonObject operationInput, | |
string outputFormat, | |
string requestProperties, | |
out string responseProperties) | |
{ | |
responseProperties = null; | |
// create the service instance | |
var service = createService(); | |
// execute the service method | |
return service.GetLandUseSelection(boundVariables, | |
operationInput, | |
outputFormat, | |
requestProperties, | |
out responseProperties); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment