?
Solved

Instantiate COM+ using New keyword

Posted on 2003-03-25
7
Medium Priority
?
321 Views
Last Modified: 2013-11-25
Hi,

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

MI
0
Comment
Question by:MiBlg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 7

Expert Comment

by:fluglash
ID: 8201585
Try first to do it with New (they are nearly equal)
0
 
LVL 9

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
0
 
LVL 9

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)
0
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!

 
LVL 9

Expert Comment

by:Naveen Swamy
ID: 8208988

'******CreateObject and CreateInstance Have the Same Effect in COM+
    '******http://support.microsoft.com/default.aspx?scid=kb;EN-US;250865
0
 
LVL 1

Author Comment

by:MiBlg
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?

MI
0
 
LVL 7

Accepted Solution

by:
fluglash earned 200 total points
ID: 8237415
YES!
0
 

Expert Comment

by:CleanupPing
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.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month12 days, 17 hours left to enroll

777 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