you can use arcobjects in an MVC service (or any service for that matter) as long as the service(s) reside on the same server as ArcGIS Server. Use ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Server)) and ... serverStatus = ao.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcServer);
public HttpResponseMessage Post([FromBody] List<ModelRequest> requests)
{
//request not getting through
//verify TrowIfMaxHttpCollectionKeysExceeded
//limits the amount of collections sent (default 1000) overide with below.
//<appSettings>
//<add key="aspnet:MaxHttpCollectionKeys" value="1001" />
//</appSettings>
if (requests == null)
{
return CreateResponseMessage(HttpStatusCode.InternalServerError, "Unhandled Exception", "Request Is Null");
}
HttpResponseMessage returnMessage = CreateResponseMessage(HttpStatusCode.InternalServerError, "Unhandled Exception", "Task Failed"); ;
var sta = new StaTaskScheduler(numberOfThreads: 1);
var task = Task.Factory.StartNew(() =>
{
//do work in sta thread
returnMessage = DoWork(requests);
}, CancellationToken.None, TaskCreationOptions.None, sta);
Task.WaitAll(task);
return returnMessage;
}
private HttpResponseMessage DoWork(List<ModelRequest> requests)
{
//now in StaThread
string staVerify = Thread.CurrentThread.GetApartmentState().ToString();
//Do ArcObjects coding here... make sure ALL arcobjects are created in the
//StaThread. Watch out for member level variables that are created in static
//classes for example.
return Request.CreateResponse(HttpStatusCode.OK, myModelResults, "application/json");
}
/// <summary>
/// Create an HTTP custom response.
/// </summary>
/// <param name="code">HTTP Status Code</param>
/// <param name="title">title of response message.</param>
/// <param name="content">response message content.</param>
/// <returns>HttpResponseMessage</returns>
private HttpResponseMessage CreateResponseMessage(HttpStatusCode code, string title, string content)
{
var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent(content),
ReasonPhrase = title
};
return resp;
}