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

查看:123
本文介绍了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

无法转换类型的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.

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

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