Using COM Objects


The world of Web development has embraced COM as a means for packaging pieces of functionality. Companies have been built on selling COM objects for use in Web applications. ASP.NET and the CLR allow us to continue using COM objects. However, there is a cost associated with using COM objects from an ASP.NET Web application. When you have control of the functionality, make it a priority to rewrite COM objects in managed code to avoid the overhead of COM Interop.

Many COM objects have multiple APIs for accomplishing the same tasks. Perhaps one method call with an extra parameter accomplishes the same thing as several separate calls to different methods. Parameters and return results must be marshaled between the native code world of the COM object and the managed code environment of ASP.NET. Opt for making the single larger call to minimize the number of Interop calls and separate instances of parameter marshalling.

ASP.NET has to take special action in order to run single-threaded apartment (STA) COM objects from within a page. To enable this functionality, you must set ASPCompat=“true” in the Page directive. The page will run from a specially created STA thread pool in which the context and page intrinsics are made available in a fashion compatible with legacy COM objects. When running in ASPCompat mode, be sure to create all COM objects and operating system resources as late as possible and release them as soon as they are no longer needed. This will reduce contention for the resources. If the page constructor creates an STA COM object, this object will be created on a separate thread from the thread that will run the page, automatically requiring all calls to the object to be marshaled across threads, which has an unnecessary and significant performance cost.

Tip

Do not create COM objects in a page constructor, either explicitly or by declaring the object as a page-scoped variable. If you must use legacy COM objects, create them when they are needed and release them as soon as you are finished with them.




Microsoft ASP. NET Coding Strategies with the Microsoft ASP. NET Team
Microsoft ASP.NET Coding Strategies with the Microsoft ASP.NET Team (Pro-Developer)
ISBN: 073561900X
EAN: 2147483647
Year: 2005
Pages: 144

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net