using System;
using System.Configuration;
using ESRI.ArcGIS.Server;
public class ArcGisServerContext : IServerContext, IDisposable
{
#region Fields
private IServerContext serverContext;
#endregion
#region Constructors()
// Here you can add overloaded constructors to take additional
// arguments such as login credentials - Or you could alter the
// constructor to use an ArcGIS Server Identity from web.config.
// This example just connects as the current user, which will be
// ASPNET or NETWORK SERVICE if running under IIS on Wind XP or 2003.
public ArcGisServerContext(string serviceName,
ArcGisServiceType serviceType)
{
IGISServerConnection conn = new GISServerConnectionClass()
as IGISServerConnection;
// Get optional host setting from web.config,
// or use localhost if not found.
string host = ConfigurationManager.AppSettings["AGSHost"];
if (String.IsNullOrEmpty(host))
{
host = "localhost";
}
// Make connection and cache IServerContext
conn.Connect(host);
IServerObjectManager som = conn.ServerObjectManager;
serverContext = som.CreateServerContext(serviceName,
serviceType.ToString());
}
#endregion
#region IServerContext Implementation
// These methods just pass through to the cached server context
public object CreateObject(string clsid)
{
return serverContext.CreateObject(clsid);
}
public object GetObject(string name)
{
return serverContext.GetObject(name);
}
public object LoadObject(string str)
{
return serverContext.LoadObject(str);
}
public void ReleaseContext()
{
serverContext.ReleaseContext();
}
public void Remove(string name)
{
serverContext.Remove(name);
}
public void RemoveAll()
{
serverContext.RemoveAll();
}
public string SaveObject(object obj)
{
return serverContext.SaveObject(obj);
}
public IServerObject ServerObject
{
get { return serverContext.ServerObject; }
}
public void SetObject(string name, object obj)
{
serverContext.SetObject(name, obj);
}
#endregion
#region IDisposable Implementation
// Implementation of the Dispose Pattern
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
// Release the server context
if (disposing)
{
if (serverContext != null)
{
serverContext.ReleaseContext();
}
}
}
#endregion
}