Specify apartment state to use when instantiating out of proc COM object(指定从 proc COM 对象实例化时要使用的单元状态)
问题描述
我在 .NET 中创建了一个 COM 对象,并使用 regsvcs 将其注册为具有 Pooling = 1 的 COM+ 服务器应用程序.我目前正在寻找一个错误,因此需要确保此 COM 对象在 STA 中运行,而不是在 MTA 中运行.我该如何指定?
以下任何一项都会对我有所帮助:
I created a COM object in .NET and registered it as a COM+ server application with Pooling = 1 using regsvcs. I am currently hunting down a bug and therefore need to make sure that this COM object is running in STA, not MTA. How can I specify this?
Any of the following will help me:
- 组件服务管理单元中的一项设置
- 使 COM 对象只允许 STA 而不允许两者的设置/代码片段
- 调用方 C# 中的设置/代码片段告诉 COM+ COM 对象应使用 STA 初始化
更新:
我试图手动将注册表中的 ThreadingModel 条目从 Both 更改为 Apartment.这也没有帮助,因为当我尝试实例化 COM 对象时,我得到一个 COMException (0x80110802) 并且事件查看器说:
Update:
I tried to manually change the ThreadingModel entry in the registry from Both to Apartment. This didn't help either, because when I try to instantiate the COM object, I get an COMException (0x80110802) and the event viewer says:
注册表中指定的组件的线程模型与注册数据库不一致.故障组件为:<MyComponent>
The threading model of the component specified in the registry is inconsistent with the registration database. The faulty component is:
<MyComponent>
我还有什么地方需要更改线程模型吗?例如在那个注册数据库"中?我在哪里可以找到它?
Is there any other place I need to change the threading model? For example in that "registration database"? Where can I find it?
谢谢!
推荐答案
好的,我在暴露为 COM 对象的类中插入了以下代码,它似乎可以工作:
OK, I inserted the following code in the class that is exposed as COM object and it seems to work:
[ComRegisterFunction]
private static void Register(Type registerType)
{
if (registerType != null)
{
using (RegistryKey clsidKey = Registry.ClassesRoot.OpenSubKey("CLSID"))
{
using (RegistryKey guidKey = clsidKey.OpenSubKey(registerType.GUID.ToString("B"), true))
{
using (RegistryKey inproc = guidKey.OpenSubKey("InprocServer32", true))
{
inproc.SetValue("ThreadingModel", "Apartment", RegistryValueKind.String);
}
}
}
}
}
我完全不明白,为什么手动更改 ThreadingModel 并没有产生相同的结果,但我不在乎...
I don't understand at all, why changing the ThreadingModel by hand didn't yield the same result, but I don't care...
这篇关于指定从 proc COM 对象实例化时要使用的单元状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:指定从 proc COM 对象实例化时要使用的单元状态
基础教程推荐
- 获取C#保存对话框的文件路径 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 如果条件可以为空 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 将数据集转换为列表 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 从 C# 控制相机设备 2022-01-01
