COM互操作对象在一个项目中抛出InvalidCastException,但在其他项目中不抛出 [英] COM interop object throws InvalidCastException in one project but not in other

查看:176
本文介绍了COM互操作对象在一个项目中抛出InvalidCastException,但在其他项目中不抛出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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'。此操作失败,因为由于以下错误,COM组件上的QueryInterface调用与IID'{5DFA18E8-4E71-4ADC-A812-6B166C242561}'的接口失败:没有此类接口支持(来自HRESULT的异常:0x80004002(E_NOINTERFACE))

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)).

我搜索了整个系统的每一个interop和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应用程序中工作,但不在我的类库中(由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线程从主线程执行代码,而asp.net没有。它是通过在STAThread中显式地运行COM来解决的。

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.

这篇关于COM互操作对象在一个项目中抛出InvalidCastException,但在其他项目中不抛出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆