ExposeInstanceToUnit should be used from the initialization of the TdwsUnit table, see RTTIExposeTests / ExposeInstancesAfterInitTable for some code example. It allows you to directly expose dynamic instances.
Another approach is to use a collection of instances of the TdwsUnit component, you get development-time support and more controls over your instances and their lifetime.
Also keep in mind that the instances that you expose will behave correctly, even if the script is misbehaves, fi when the user tries to manually destroy the instance that you exposed, and this instance should not be destroyed. By default, ExposeRTTI displays destructors, so you can limit this by specifying eoNoFreeOnCleanup.
edit: The last recently added approach is to use the TdwsRttiConnector, which basically allows you to expose and connect to anything accessible via RTTI. It is very easy in terms of customization code, but the disadvantage is that you do not get any compile time checking.
source share