COM互操作对象引发InvalidCastException的一个项目,但不是在其他是在、但不、对象、操作

2023-09-03 03:08:17 作者:落红尘°

我有我在我的测试C#的解决方案导入一个COM对象。 一切工作正常,并将继续这样做。

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

然后我进口的相同COM对象在我实际的解决方案,并为实现测试的完全相同的行作为测试项目。 当我运行的实际项目中,我得到一个InvalidCastException

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

无法转换类型的COM对象CTWebReport.WebReportCOMClass接口类型CTWebReport.IWebReportCOM。此操作失败的原因是对IID为{5DFA18E8-4E71-4ADC-A812-6B166C242561}的接口的COM组件调用QueryInterface以下错误失败,原因是:不支持此接口(异常来自HRESULT:0x80004002(E_NOINTERFACE)) 的

我搜索我的整个系统,为每一位参考互操作和COM库,这包括所有的bin和OBJ文件夹的我能找到。 除了原来的COM库我删除了所有这些,好措施清理我的回收站。 没有区别,完全一样的情况。测试项目工程,真正的项目没有。

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.

修改 看来,COM工程的WinForms应用程序,而不是在我的课librar(由一个asp.net的MVC Web applicatoin消耗)。

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?

推荐答案

所不同的是线程。在WinForm线程执行的code从主线程,而asp.net没有。它是由明确的运行COM的STAThread解决了。

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.

 
精彩推荐
图片推荐