Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Instantiate COM+ using New keyword

Posted on 2003-03-25
Medium Priority
Last Modified: 2013-11-25

I've already learned that when doing programming for a COM+ class we must get rid of New keyword if we need to instantiate another object hosted in COM+. However, is it allowed to instantiate a class hosted in COM+ using New keyword from VB client? Let's say I've got a Foo class hosted in COM+ then is it ok to instantiate it in my VB Form like this:

         Dim objFoo as MyComponent.cFoo '// cFoo is a COM+ hosted class
         set objFoo = new cFoo

or should I instantiate it using CreateObject?

         Dim objFoo as MyComponent.cFoo
         set objFoo = CreateObject("MyComponent.cFoo")

Which one is better and safest?

Thanks for any help

Question by:MiBlg

Expert Comment

ID: 8201585
Try first to do it with New (they are nearly equal)

Expert Comment

by:Naveen Swamy
ID: 8201655
copied from msdn

You approach building MTS components differently than components for stand-alone applications. You need to understand how MTS initializes the components and how you tell MTS that you’re done with the component. First, you can’t create an MTS object by using the New keyword. When you use New (for example, Set myObj = New obj), VB bypasses MTS and provides an object pointer that points directly to the new object. To create an MTS object, you can use either the Server.CreateObject method, which MTS will intercept, or you can use the ObjectContext.CreateInstance method. If you already have an ObjectContext reference, use CreateInstance.

The object reference that you create via CreateObject or CreateInstance isn’t a direct pointer to the object at all (although your program can’t tell the difference). Instead, it’s a pointer to a context object, which MTS creates as an intermediary. The context object’s job is to fool your program into thinking that it can control the target object directly. Therefore, you can treat the object as if you had a “real” object pointer. Behind the scenes, MTS manages one or more instances of the object, but those few instances may be connected to many clients.

Whenever you call a method of your object, MTS looks for the first free instance and passes the call to that instance. That means that the instance that handles your second method call may not be the same instance that handled the first. At this point, you should begin to see why MTS improves scalability. If there were five clients, and each needed to use a certain component, you would normally have five copies of the component in memory. With MTS though, you might only need one or two. MTS uses the time between method calls to recycle a component for many clients.

Because many clients share the components, you can’t set a property on a component and then run a method that depends on the component “remembering” the property. For example (and I promised in Chapter 11 that I’d tell you about this), if you set a database access component’s ConnectionString property, then use that property to make multiple method calls to the database, the property may not be available. That’s the primary reason that I set up the CRepositoryData class so that you have to pass the ConnectionString with each method call—so it would be easier to move that class into MTS.

A client program can use an MTS object pointer over many method calls and everything will work fine, but the component needs to inform MTS each time it completes a unit of work so that MTS can recycle it. Components do this via the ObjectContext object. Here’s a typical sequence:

The client requests an MTS component instance—an object.
MTS provides a handle to an ObjectContext object instead.
The client accepts that handle as a “real” object reference.
The client makes a method call to the component.
MTS locates an idle instance of the component. If none are available, it creates a new instance.
MTS passes the method call to the component.
The component uses the getObjectContext method to obtain a reference to its object context.
The component performs the method. If the method is successful, the component calls the ObjectContext.SetComplete method; if the method is unsuccessful, it calls ObjectContext.SetAbort.
In either case, success or failure, the call to SetComplete or SetAbort informs MTS that the component has completed its work and is ready to be recycled.
When the client calls another method on the component, the cycle begins from step 5, not from step 1. A different instance of the component is likely to perform the method.

hope this helps u

Expert Comment

by:Naveen Swamy
ID: 8208984
   '******ADO objects are not created using ObjectContext.CreateInstance but rather the New
    '******operators - this is because ADO objects do not need to be created using
    '******ObjectContext.CreateInstance to participate in the current transaction (if any).
    '******If you are using a custom wrapper for database access (which in turn could be using ADO) you
    '******would need to create this wrapper using ObjectContext.CreateInstance for it to participate in the MTS transactions.
    '******reference (http://support.microsoft.com/default.aspx?scid=kb;EN-US;234218)
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

by:Naveen Swamy
ID: 8208988

'******CreateObject and CreateInstance Have the Same Effect in COM+

Author Comment

ID: 8229062
navstar16, you're close to get me to the answer, but let me have one more question. If I create a VB Form which calls COM+ object, it's actually in different process from the COM+ object so am I right to think that I can instantiate it using New keyword in order to get fastest performance in object creation?


Accepted Solution

fluglash earned 200 total points
ID: 8237415

Expert Comment

ID: 8531655
Hi MiBlg,
This old question (QID 20562109) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question