Calling STA COM Components from ASP.NET Webpage

by Chad 11. June 2010 16:10

Can not call friend function on object which is not an instance of defining class
Google offered little help resolving this problem, so I thought I’d create this keyword-rich blog post so that someday it might climb the page ranks and help someone.

FriendFunctionErrorAt work, we have an ASP.NET website that uses our Visual Basic 6 COM library via .NET Interops. Everything works quite well until you create a new .aspx page and forget to add the AspCompat=”true” @page directive attribute. It’s an easy thing to forget (it’s bitten me twice now.)  The difficult part is that the COMException has an error message that is misleading: “Can not call friend function on object which is not an instance of defining class”.

AspCompat=”true”
The solution is actually very simple. The problem is caused because ASP.NET pages execute in a multithreaded apartment (MTA) and the VB6 COM Components expect to be in a single-threaded apartment (STA.) Adding the AspCompat=”true” attribute to the @page directive to force the page to execute in a STA.

The MSDN documentation does offer one word of caution about setting AspCompat=”true”. It can introduce performance issues if you instantiate your COM objects inside the page’s constructor. Microsoft recommends keeping that code in the page event handlers such as Page_Init and Page_Load.

Tags:

ASP.NET | COM | Web

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About the author

Chad

Meeeee!!!

Hi, my name is Chad Boschert and I'm a software developer in Springfield, Missouri. I've been developing .NET applications in C# since 2002.

Recent Comments

Comment RSS

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar