18.04.2015 Views

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

.NET APPLICATION PROGRAMMING INTERFACE<br />

In the case of SDE data sources, the cursor holds on to an SDE stream, and if<br />

the application has multiple clients, each may get and hold on to an SDE stream,<br />

eventually exhausting the maximum allowable streams. The effect of the number<br />

of SDE streams exceeding the maximum is that other clients will fail to open<br />

their own cursors to query the database.<br />

Because of the above reasons, it’s important to ensure that your reference to any<br />

cursor your application opens is released in a timely manner. In .NET, your reference<br />

on the cursor (or any other COM object) will not be released until garbage<br />

collection kicks in. In a Web application or Web service that services multiple<br />

concurrent sessions and requests, relying on garbage collection to release references<br />

on objects will result in cursors and their resources not being released in a<br />

timely manner.<br />

To ensure a COM object is released when it goes out of scope, the WebControls<br />

assembly contains a helper object called WebObject. Use the ManageLifetime<br />

method to add your COM object to the set of objects that will be explicitly<br />

released when the WebObject is disposed. You must scope the use of WebObject<br />

within a using block. When you scope the use of WebObject within a using block,<br />

any object (including your cursor) that you have added to the WebObject using the<br />

ManageLifetime method will be explicitly released at the end of the using block.<br />

The following example demonstrates this coding pattern:<br />

[VB.NET]<br />

Private SubSystem.object doSomething_Click(ByVal sender As System.Object,<br />

ByVal e As System.EventArgs) Handles doSomething.Click<br />

Dim webobj As WebObject = New WebObject<br />

Dim ctx As IServerContext = Nothing<br />

Try<br />

Dim serverConn As ServerConnection = New ServerConnection("doug", True)<br />

Dim som As IServerObjectManager = serverConn.ServerObjectManager<br />

ctx = som.CreateServerContext("Yellowstone", "MapServer")<br />

Dim mapsrv As IMapServer = ctx.ServerObject<br />

Dim mapo As IMapServerObjects = mapsrv<br />

Dim map As IMap = mapo.Map(mapsrv.DefaultMapName)<br />

Dim flayer As IFeatureLayer = map.Layer(0)<br />

Dim fClass As IFeatureClass = flayer.FeatureClass<br />

Dim fcursor As IFeatureCursor = fClass.Search(Nothing, True)<br />

webobj.ManageLifetime(fcursor)<br />

Dim f As IFeature = fcursor.NextFeature()<br />

Do Until f Is Nothing<br />

' Do something with the feature.<br />

f = fcursor.NextFeature()<br />

Loop<br />

Finally<br />

ctx.ReleaseContext()<br />

webobj.Dispose()<br />

Chapter 4 • <strong>Developer</strong> environments • 177

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!