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 对象实例化时要使用的单元状态
基础教程推荐
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
