tags:

views:

342

answers:

2

I have a COM object I imported in my test C# solution. Everything works fine and continues to do so.

Then I imported the same COM object in my actual solution and as a test implemented the exact same lines as in the test project.
When I run the real project I get an InvalidCastException

Unable to cast COM object of type 'CTWebReport.WebReportCOMClass' to interface type 'CTWebReport.IWebReportCOM'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{5DFA18E8-4E71-4ADC-A812-6B166C242561}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

I searched my entire system for every reference to the interop and com library, this includes all the bin and obj folders I could find. Except for the original COM library I deleted them all and for good measure cleaned out my recycle bin.
No difference, exact same situation. Test project works, real project doesn't.

EDIT
It seems that the COM works in winforms applications, but not in my class librar (that is consumed by an asp.net mvc web applicatoin).

I don't know what to do next. Suggestions?

A: 

Since your COM component is working in a WinForm app but not in ASP.NET I'm thinking that you may be hitting some permission issues. Can you temporarily elevate the permissions that ASP.NET is running under (e.g. Administrator) and see if you can execute the component?

Tuzo
Yes, that was one of the things I checked, but even wen both were running with the same permission, it didn't change a thing.
borisCallens
A: 

The difference was in threading. The winform thread executed the code from the main thread, whereas the asp.net didn't. It was solved by explicitly running the COM in a STAThread.

borisCallens
What exactly have you changed in code?
sharptooth
With thanks to @sharptooth for setting me on the right track
borisCallens
To be honoust, not sure. The COM was generated in Delphi in our India department. Now all I had to do was explicitly add the [STAThread] attribute to my caller.
borisCallens
Also, how fast was your comment!? Did you sleep at all the past few days or were you just watching this thread?
borisCallens
A fast comment is just a coincidence. Where have you added the attribute?
sharptooth
If you are running in ASP.NET, then you can add the AspCompat tag to the page directive <%@ Page AspCompat="true" Language="C#" %> to force the component to force the page to run in STA mode. That should also resolve your issue.
Tuzo
Are you sure? In asp.net mvc the view is only loaded after all the other work is done, so I don't think the tag is taken into consideration when the controller is delegating all its work..
borisCallens
@sharptooth: on the method that calls the com wrapper.
borisCallens
Will you please provide a code snippet to illustrate that?
sharptooth