C# 注册COM+组件步骤~

这篇是在C#中注册COM+组建测试用例.....

项目创建:1.添加引用System.EnterpriseServices,并且using System.EnterpriseServices;

2.Public class public class ComSample:ServicedComponent //类要从ServicedComponent继承

在类的前面要添加 [Transaction(TransactionOption.Required)],以启用事务

步骤1:(实例:如下图1)

(1)给类一个强名称,创建强名称,可以使用 sn 工具。

    在 Visual Studio .NET 命令提示符处,键入 sn.exe -k FrankXuKey.snk 以创建一个密钥文件.

(2)在工程中引用这个强名称

    注释掉AssemblyInfo.cs中的[assembly: AssemblyKeyFile( "" )]和[assembly: AssemblyKeyName( "" )]
    在AssemblyInfo.cs写入
    using System.Data.OleDb;//引用ADO.net命名空间
    using System.Data;//引用数据空间
    using System.Runtime.InteropServices;//为了调用GUID
    ///System.EnterpriseServices命名空间包涵所有COM+的类型库所以在编写COM+组件的时候一定要用到

    ///System.EnterpriseServices命名空  间,这个命名空间在引用中.net页下可以找到
    
using System.EnterpriseServices;//引用COM+名命空间
   
using System.Runtime.CompilerServices;//运行时编译服务器
   
using System.Reflection;//用些全局属性取得强名属性
    [assembly: ApplicationName( "myCom" )]
    //强名文件名和文件属性.用sn.exe生成,用法 sn -k mycom.snk
    [assembly: ApplicationName("ComApp")]
    [assembly: AssemblyKeyFileAttribute(@"C:\Program Files\Microsoft Visual Studio 9.0\VC\ComApp.snk")]

步骤2:编译成COM+组件(实例:如下图2和3)

首先,必须为该程序集创建一个类型库.类型库是 .NET 程序集中所包含的元数据的 COM 等效组件.类型库通常包含在扩展名为 .tlb 的文件中.类型库包含必要信息,COM 客户端使用这些信息可以确定在特定服务器中有哪些类,以及这些类支持的方法、属性和事件..NET 框架 SDK 包含一个名为 tlbexp(类型库导出程序)的工具,它可以从程序集创建类型库.tlbexp 包含许多选项,可以在命令提示符处键入 tlbexp /? 查看所有选项.其中一个是 /out 选项,用于指定已生成的类型库的名称.(如果您不自己指定名称,将自动为您创建一个名称.)例如,要将元数据从一个名为 MyCom.dll 的程序集提取到一个名为 MyCom.tlb 的类型库中,可以使用以下命令行:
*注意:tlbexp MyCom.dll /out:MyCom.tlb的命令中out:MyCom.tlb
out: 和   MyCom.tlb   就是要导出的类型库文件中间一定要有一个空格
tlbexp MyCom.dll /out:MyCom.tlb

  • 应使用 .NET 框架 SDK 中的程序集注册工具 ( regasm ),通过一次操作同时完成类型库的创建和注册.这是在一台计算机上同时进行 .NET 和 COM 开发所能使用的最简单的工具.与 tlbexp 类似,regasm 有许多选项.在命令提示符处键入 regasm /?,可以查看所有选项.要使用 regasm 创建并注册一个类型库,可以使用相应的命令行,如:
    regasm /tlb:MyCom.tlb MyCom.dll
  • 或者:设置项目配置里的生成属性,为Com Interop 注册:True

    步骤3:(实例:如下图:4)注意这里第一行出现不能注册的错误:原因是AssemblyInfo.cs文件中的[assembly: ComVisible(false)]默认是false

    解决办法:[assembly: ComVisible(true)]

    dcomcnfg注册Com+组件后图示:

    测试代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.OleDb;//引用ADO.net命名空间
    using System.Data;//引用数据空间
    using System.Runtime.InteropServices;//为了调用GUID
    //System.EnterpriseServices命名空间包涵所有COM+的类型库所以在编写COM+组件的时候一定要用到System.EnterpriseServices命名空间,这个命名空间在引用中.net页下可以找到
    using System.EnterpriseServices;//引用COM+名命空间
    using System.Runtime.CompilerServices;//运行时编译服务器
    using System.Reflection;//用些全局属性取得强名属性

    namespace ComApp
    {
        [InterfaceType(ComInterfaceType.InterfaceIsDual)]
        //定义接口
        public interface Add
       
        {
            ///接口内的方法返回布尔值有一个整型参数
            bool AddData( int d );
           
            bool AddDataM( int d );
        }
        //实现这个接口
        //事务属性表示需要新事务
        //这个属性用来记录类是否支持事务有车个选项同VB中类的属性相同
        [Transaction( TransactionOption.RequiresNew)]
        //在类中实现这个接口必须要继承System.EnterpriseServices;命名空间的ServicedComponent类
        //在后面继承前面所定义的接口如果有多个可以用“,”分开
        public class myClass:ServicedComponent,Add
        {
            //  public myCom.myClass   error =new ErrorsLibrary.Class1( );
           
            //定认字符变量用来存放数据库连接字符串
            private static string strConnect ="Provider=MSDAORA.1;Password=erpii;User ID=erpii;Data Source=erpii;Persist Security Info=True";
            //创建这个连接
            private OleDbConnection conConnection = new OleDbConnection (strConnect) ;
           
           
            //实现接口中的查询方法
            public bool AddData( int id )
            {
                try
                {
                   
                    //string StrSql="insert into    TEST1   values('" + id + "' )";
                    string StrSql=" insert into    test_d  values('" + id + "',"
                    +"'" + id + "',"
                    + "'" + id + "',"
                    + "'" + id + "' )";
                    //     string StrSql="DELETE FROM TEST1";
                    //找开连接
                    conConnection.Open( ); // 打开数据连接
                    //执行查询
                    OleDbCommand cmd = new OleDbCommand (StrSql , conConnection) ;
                    cmd.ExecuteNonQuery ( );
                    conConnection.Close ( );
                   
                    //ContextUtil.SetComplete( ) ;
                    return true;
                }
                catch ( Exception e )
                {
                   
                    //error.Description =e.Message;
                    //    EnterpriseServices.ContexUtil.
                    ContextUtil.SetAbort( );
                    return false;
                    //   
                }
            }
           
            public bool AddDataM( int id )
            {
                try
                {
                    //    string StrSql="insert into    TEST21  values(" + id + "' )";
                    //    string StrSql="DELETE FROM TEST2";
                    //找开连接
                    string StrSql=" insert into    test_m  values('" + id + "',"
                    +"'" + id + "',"
                    + "'" + id + "',"
                    + "'" + id + "' )";
                    conConnection.Open( ); // 打开数据连接
                    //执行查询
                    OleDbCommand cmd = new OleDbCommand (StrSql , conConnection) ;
                    cmd.ExecuteNonQuery ( );
                    conConnection.Close ( );
                    //    System.EnterpriseServices.ContextUtil.SetComplete( ) ;
                    return true;
                   
                }
                catch ( Exception e )
                {
                    //error.Description =e.Message;
                    //    EnterpriseServices.ContexUtil.
                    //    System.EnterpriseServices.ContextUtil.SetAbort( );
                    //    e.Message;
                    ContextUtil.SetAbort ( ) ;
                    return false;
                   
                }
               
            }
           
            //如果在方法或类上使用[AutoComplete]则事务类或方法在没有错误的情状下自动提交事务
            [AutoComplete]
            public bool addRs( )
            {
               
                try
                {
                    for ( int i=0;i<100;i++ )
                    {
                        ////      
                        if ( !AddData( i ) )
                        {    //事务回滚
                            ContextUtil.SetAbort ( ) ;/// 函数返回值
                            return false;
                        }
                        //如函数不等于真则回滚其中(?┫嗟庇赩B中的(NOT)
                        if (!AddDataM( i ) )
                       
                        {
                            ContextUtil.SetAbort ( ) ;
                            return false;
                        }
                       
                    }
                       ///如查成功能提交事务
                    ContextUtil.SetComplete( );
                    return  true;
                   
                }
                catch ( Exception e )
                {
                    //      error.Description =e.Message;
                    //    EnterpriseServices.ContexUtil.
                    //    ContextUtil.SetAbort( );
                    ContextUtil.SetAbort ( ) ;
                    return false;
                    //   
                }
            }
           
            public myClass( )
            {
                //
                // TODO: 在此处添加构造函数逻辑
                //
            }
        }

    }

    AssemblyInfo.cs:

    using System.Data.OleDb;//引用ADO.net命名空间
    using System.Data;//引用数据空间
    using System.Runtime.InteropServices;//为了调用GUID
    //System.EnterpriseServices命名空间包涵所有COM+的类型库所以在编写COM+组件的时候一定要用到System.EnterpriseServices命名空间,这个命名空间在引用中.net页下可以找到
    using System.EnterpriseServices;//引用COM+名命空间
    using System.Runtime.CompilerServices;//运行时编译服务器
    using System.Reflection;//用些全局属性取得强名属性

    // General Information about an assembly is controlled through the following
    // set of attributes. Change these attribute values to modify the information
    // associated with an assembly.
    [assembly: AssemblyTitle("ComApp")]
    [assembly: AssemblyDescription("")]
    [assembly: AssemblyConfiguration("")]
    [assembly: AssemblyCompany("")]
    [assembly: AssemblyProduct("ComApp")]
    [assembly: AssemblyCopyright("Copyright ©  2009")]
    [assembly: AssemblyTrademark("")]
    [assembly: AssemblyCulture("")]

    [assembly: ApplicationName("ComApp")]
    [assembly: AssemblyKeyFileAttribute(@"C:\Program Files\Microsoft Visual Studio 9.0\VC\ComApp.snk")]
    [assembly: ApplicationActivation(ActivationOption.Server)]
    // Setting ComVisible to false makes the types in this assembly not visible
    // to COM components.  If you need to access a type in this assembly from
    // COM, set the ComVisible attribute to true on that type.
    [assembly: ComVisible(true)]

    // The following GUID is for the ID of the typelib if this project is exposed to COM
    [assembly: Guid("3fb37504-f308-4013-9df7-19605d153a4f")]

    // Version information for an assembly consists of the following four values:
    //
    //      Major Version
    //      Minor Version
    //      Build Number
    //      Revision
    //
    // You can specify all the values or you can default the Build and Revision Numbers
    // by using the '*' as shown below:
    // [assembly: AssemblyVersion("1.0.*")]
    [assembly: AssemblyVersion("1.0.0.0")]
    [assembly: AssemblyFileVersion("1.0.0.0")]

    原文地址:https://www.cnblogs.com/AriLee/p/1997269.html