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

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

问题描述

我在我的测试 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

无法将CTWebReport.WebReportCOMClass"类型的 COM 对象转换为接口类型CTWebReport.IWebReportCOM".此操作失败,因为 IID 为{5DFA18E8-4E71-4ADC-A812-6B166C242561}"的接口的 COM 组件上的 QueryInterface 调用因以下错误而失败:不支持此类接口(来自 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天全站免登陆