WEBINAR:

On-Demand

Use SafeRef() Before Passing a Reference of an MTS Object

SafeRef() returns a reference to the context wrapper instead of a reference to the object itself. You should never pass a direct reference of an MTS object to a client application. If you do, the client application can make a call on the MTS object without going through the context wrapper. This defeats the interception scheme set up by the MTS run time. You should use SafeRef() function which returns the outside world's view of an MTS object.

Let's say that you're writing a method implementation for an MTS object. In the method, you want to create an object of some class and then pass your own reference to the newly created object as follows:

This code is incorrect under MTS, however. The child object (pSomeClass) can invoke method calls on your object without going through the context wrapper. You should never bypass the interception scheme set up by the MTS run time. Instead, you should pass a reference to your object as follows:

pSomeClass.SomeMethod SafeRef(Me) ' Correct code

By calling SafeRef, you allow the child object to establish a connection that passes through the context wrapper. This keeps things in line with what the MTS run time expects. Note that the Me keyword is the only valid parameter you can pass when calling SafeRef with Visual Basic.