为什么我的COM对象不显示在组件服务的方法呢? [英] Why is my COM object not displaying methods in Component Services?

查看:120
本文介绍了为什么我的COM对象不显示在组件服务的方法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个COM对象,并在COM +下注册。一切似乎都进展顺利,但是当我看在组件服务,并通过深入

  

控制台根|组件服务|计算机|我的电脑| COM +应用| TestCom |组件| TestCom.Com.MyCom |接口| _MyCom |方法

我不认为我的方法列出。

在我的项目属性我已经签署了我的组装。

AssemblyInfo.cs中:

  [总成:应用程序名称(TestCom)
[总成:ApplicationAccessControl(假)]
[总成:ApplicationActivati​​on(Activati​​onOption.Server)
[总成:标记有ComVisible特性(真)
 

MyCom.cs

使用系统; 使用的System.EnterpriseServices;

 命名空间TestCom.Com
{
    [交易(TransactionOption.Required)
    公共类MYCOM:ServicedComponent的
    {
        [自动完成]
        公众诠释GetIntFromCom()
        {
            VAR的结果=新的随机(DateTime.Now.Second)。接下来();
            返回结果;
        }
    }
}
 

我生成DLL和VS2012命令提示符下我跑regsvcs TestCom.Com.dll。其结果是:

 安装的组件:
 

 大会:C:\ TestCom.Com.dll
应用:TestCom
类型库:C:\ TestCom.Com.tlb
 

我一定是失去了一些东西,我只是无法弄清楚它是什么。

解决方案

您在此处使用默认设置。通过[标记有ComVisible特性]对象支持的默认界面是IDispatch接口,只有良好的后期绑定的。事情是这样的微软prefers,大量少当客户端code总是有不同版本的组件,被称为DLL地狱有问题的痛苦。脚本语言总是使用的IDispatch,并不需要的方法可见。编译语言通常支持早期绑定,您可以添加类型库,你会得到语法错误检查和自动完成功能,以及大大提高了运行时性能。极大的方便,同时节目,但是当DLL地狱罢工风险很大。

如果你想看到的方法,那么你必须使用 [InterfaceType(ComInterfaceType.InterfaceIsDual)] 属性声明公共接口

。它公开的IDispatch的的接口类型库的类型。你不会有自动生成的 _MyCom 界面了,你看你自己。你会通常把它叫做 IMyCom

这需要声明的接口明确,所以你可以应用属性。通常一个有点不便,如果你并不是一开始就这样。您还可以应用 [ClassInterface(ClassInterfaceType.AutoDual)] 的公共类会暴露。随着缺点也暴露System.Object的成员,并采取在.NET Framework类型库的依赖。这是okayish。

I am trying to create a COM object and register it under COM+. Everything seems to go well, but when I look in Component Services and drill down through

Console Root | Component Services | Computers | My Computer | COM+ Applications | TestCom | Components | TestCom.Com.MyCom | Interfaces | _MyCom | Methods

I do not see my method listed.

In my project properties I have signed my assembly.

AssemblyInfo.cs:

[assembly: ApplicationName("TestCom")]
[assembly: ApplicationAccessControl(false)]
[assembly: ApplicationActivation(ActivationOption.Server)]
[assembly: ComVisible(true)]

MyCom.cs

using System; using System.EnterpriseServices;

namespace TestCom.Com
{
    [Transaction(TransactionOption.Required)]
    public class MyCom : ServicedComponent
    {
        [AutoComplete]
        public int GetIntFromCom()
        {
            var results = new Random(DateTime.Now.Second).Next();
            return results;
        }
    }
}

I build the dll and in the VS2012 command prompt I run regsvcs TestCom.Com.dll. This results in:

Installed Assembly:

Assembly: c:\TestCom.Com.dll
Application: TestCom
TypeLib: c:\TestCom.Com.tlb

I must be missing something, I just can't figure out what it is.

解决方案

You are using default settings here. The default interface supported by [ComVisible] objects is IDispatch, good only for late-binding. It is the way Microsoft prefers, lots less misery when the client code assumes a different version of the component, a problem known as DLL Hell. Scripting languages always use IDispatch and don't need the methods to be visible. Compiled languages usually support early binding, you can add the type library and you'll get syntax error checking and auto-completion as well as greatly improved runtime performance. A great convenience while programming, but very risky when DLL Hell strikes.

If you want to see the methods then you have to use the [InterfaceType(ComInterfaceType.InterfaceIsDual)] attribute on the public interfaces you declare. Which exposes IDispatch and the interface type in the type library. You won't have the auto-generated _MyCom interface anymore, you see your own. You'd typically call it IMyCom.

That requires declaring the interface explicitly so you can apply the attribute. Usually a bit of an inconvenience if you didn't start out that way. You can also apply [ClassInterface(ClassInterfaceType.AutoDual)] to the public class you expose. With the disadvantage that you also expose the members of System.Object and take a dependency on the .NET Framework type library. Which is okayish.

这篇关于为什么我的COM对象不显示在组件服务的方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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