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?

9
  • I once experienced similar behaviour - the problem was that the interface was not Automation-compatible and I had marshalling on or off in different situations. It worked allright with marshaling off, but the interface could not be retrieved with marshalling on. Commented Jul 10, 2009 at 10:21
  • Care to elaborate? In both project the marshalling is done by .net itself (the automatically generated interop class). Commented Jul 10, 2009 at 10:23
  • I tried to CoCreateInstance a component with an interface which was not Automation-compatible. First it was just regsvr32'd and I called CoCreateInstance() in a 32-bit C++ program - it worked allright. When I put the component into COM+ (to make it run in a separate process) CoCreateInstance in the same 32-bit program now returned E_NOINTERFACE. Commented Jul 10, 2009 at 11:03
  • Could this be the same situation? I just discovered that whenever I try to use the COM in a web application it fails and in a winform application it doesn't seem to have any problems. Commented Jul 10, 2009 at 11:23
  • 1
    Yes, it can be this. It can somehow depend on whether the program is multithreaded. If it is - marshalling is turned on. Commented Jul 10, 2009 at 11:34

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
Also, how fast was your comment!? Did you sleep at all the past few days or were you just watching this thread?
A fast comment is just a coincidence. Where have you added the attribute?
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.
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..
|
0

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?

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.