指定从 proc COM 对象实例化时要使用的单元状态 [英] Specify apartment state to use when instantiating out of proc COM object

查看:13
本文介绍了指定从 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 对象实例化时要使用的单元状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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