COM interop object throws InvalidCastException in one project but not in other(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,但在其他项目中不引发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:COM 互操作对象在一个项目中引发 InvalidCastException,但在其他项目中不引发
基础教程推荐
- Moq It.Is<>不匹配 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
