[thelist] Friday Freebie

Scott Dexter sgd at ti3.com
Fri Aug 18 16:21:41 CDT 2000

warning, this one's more for the programmers as opposed to the designers ...
but hey, when was the last time I sent out a tip that involved design??

<tip type="developing components for IIS/MTS" author="Scott Dexter">
Well, if you've played at all in the world of writing a dll that you use
from your ASP pages, you've then most likely come across what happens during
development: when you test your dll, it works (yeah right) or doesn't work,
so you make some tweaks and go to overwrite the existing copy but can't, due
to some 'Permission Denied' error?

Hell, all this is happening even if you don't have the dll setup in MTS ?!
--Side tip: IIS uses MTS for *all* objects, setting them up in an MTS
package adds additional control over them.

Okay, so you try everything. Exit out of VB. Stop IIS. Stop MTS. Nothing
works except rebooting your machine (or the server you're testing on), and
doing that is *really* starting to piss you off.

I gots a question: are you creating objects inside your dll, possibly to do
some database operation maybe? Hmmm?

And just how are you creating those objects, hmmmm? set oDB = createObject()

--If you don't set up your object creation properly, MTS loses control over
them and can't shut them down, thereby losing control over your dll and it
remains locked in memory.

There are a couple ways to get them set up right, Early Binding and Late
Binding through the MTS context.


Early Binding == marking the object as a reference inside VB6
(Project/References) when you create the dll. For example, if you make the
"Microsoft XML, version 2.0" library a reference, you can Dim a variable as
an XML document, and then set it to a new document (creating the object):

Dim oXMLDoc as DOMDocument
Set oXMLDoc  = New DOMDocument

This is called Early Binding. When MTS loads your dll, is also loads the
object libraries that you reference, so it maintains control over them. This
is the easiest way, but you don't have total control over the (re)use of the
objects you create, or the ability to participate in MTS transactions. To do

Late Binding through the MTS context == Reference the MTS type library and
use its methods to create the object(s) you need. Start by making a
reference to the "Microsoft Transaction Server Type Library." Next define a
global variable as an ObjectContext datatype:

Dim oThisContext as ObjectContext

Now, when you need to create an object, say an ADO Connection object to do
some database work, do it through the ObjectContext object. MTS will create
it in the same thread and MTS context that created your dll. This way you
have tighter control over the object, including transactional control (a tip
for a later time), but most importantly, you can dictate when the object is
released. --A Good Thing, because you want to rid yourself of objects ASAFP
for performance:

Set oThisContext = GetObjectContext()
Set DBCON = oThisContext.CreateInstance("ADODB.Connection")

' do your db stuff in here, and free the object as soon as you can:


' tell MTS to release the object(s). If you're doing MTS transactions, this
commits the transaction

Set DBCON = Nothing
Set mThisContext = Nothing

Back to your development and rebooting your box. You've made these changes,
everything works, except you still get the 'Permission Denied' error writing
a new copy. Open a Command Prompt (a DOS box) and type 'mtxstop'. This will
explicitly shut down any running objects in MTS. Here's where your previous
dll hung because MTS couldn't get at the objects created. With your new
changes, MTS can, and your dll is unloaded from memory completely and you
can compile your new version safely, without rebooting your machine.

think safely

More information about the thelist mailing list