• 使用ASP调用C#写的COM+组件


    使用ASP调用C#写的COM+组件

    1 新建类库MyTestDLL

    2 右击项目“MyTestDLL”-》属性-》生成-》勾选“为COM互操作注册”

    3 打开 AssemblyInfo.cs 文件 修改 [assembly: ComVisible(true)], 添加强命名文件.

    4 打开Visual Sutdio 2008 的命令提示行工具输入guidgen.exe 选择DEFINE_GUID 单击 "New GUID"

    5代码

          1、每个类名对应一个接口名,接口名是类名前加上一个大写的I

          2、接口中声明的方法要使用属性 [DispId(n)]

          3、类必须有一个无参构造函数


     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;

    using System.EnterpriseService
    namespace MyTestDll
    {

         //  这里Guid为第4步生成的。
        [Guid("FFA4B191-FB5B-4dd5-B7B1-B2F32BF6F1FF")]
        public interface IMyTestDll
        {
            [DispId(0)]
            string GetAbout();
        }
        public class Test1:IMyTestDll
        {
            private string summary;
            public Test1()
            {
                summary = "这是我的第一个测试";
            }
            public string GetAbout()
            {
                return summary;
            }
        }
    }

    6 生成项目

    ASP测试代码 

    <%  
      Dim  o   
      Set o = Server.CreateObject("MyTestDll.Test1")  
      Response.Write o.GetAbout()
      Set o=Nothing 
     
      %>  

    提示:如果要在其他的电脑使用我们用C#开发的这个COM组件还需要是用regasm来注册

    方法为:

    首先把bin\Debug目录的文件拷贝到目标电脑上,然后打开命令提示行工具输入:
    regasm 你拷贝到的目录/文件名.dll /tlb f:/dll/文件名.tlb /codebase

     运行既可在该电脑上使用。
     

     Com+ 实例

    一. AssemblyInfo.cs

    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.EnterpriseServices;

    // 有关程序集的常规信息通过下列属性集
    // 控制。更改这些属性值可修改
    // 与程序集关联的信息。
    [assembly: AssemblyTitle("MyComPlusSample")]
    [assembly: AssemblyDescription("MyComPlusSample Testing")]
    [assembly: AssemblyConfiguration("")]
    [assembly: AssemblyCompany("Axisoft")]
    [assembly: AssemblyProduct("MyComPlusSample")]
    [assembly: AssemblyCopyright("Copyright © Axisoft 2010")]
    [assembly: AssemblyTrademark("Web Cornucopia")]
    [assembly: AssemblyCulture("en-US")]

    // 将 ComVisible 设置为 false 使此程序集中的类型
    // 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型,
    // 则将该类型上的 ComVisible 属性设置为 true。
    [assembly: ComVisible(true)]

    // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
    [assembly: Guid("4bc9a9f7-606c-4552-a3fe-aefb39b00ed1")]

    // 程序集的版本信息由下面四个值组成:
    //
    //      主版本
    //      次版本
    //      内部版本号
    //      修订号
    //
    // 可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值,
    // 方法是按如下所示使用“*”:
    // [assembly: AssemblyVersion("1.0.*")]
    [assembly: AssemblyVersion("1.0.0.0")]
    [assembly: AssemblyFileVersion("1.0.0.0")]

    [assembly: AssemblyDelaySign(false)]
    [assembly: ApplicationAccessControl(true)]
    //[assembly: AssemblyKeyFile("MyComPlusSample.snk")]
    //[assembly: AssemblyKeyName("")]

    //[assembly: ApplicationName("COM+ Bank Server Account Manager")]
    //[assembly: ApplicationActivation(ActivationOption.Server)]

    二. Transerfer.cs

    using System;
    using System.Collections.Generic;
    //using System.Linq;
    using System.Text;
    using System.EnterpriseServices;
    using System.Data.SqlClient;
    using System.Data;
    using System.Diagnostics;
    using System.Reflection;
    using System.Runtime.InteropServices;

    [assembly: ApplicationActivation(ActivationOption.Server)]
    // 不是必须的,可以在部署的时候进行设置。
    [assembly: ApplicationName("MyComPlusSample")] 
    namespace MyComPlusSample
    {
        /// <summary>
        /// Transfer 的摘要说明。
        /// </summary>
        ///
        /// Configure component's Transaction Option
        [Transaction(TransactionOption.Required)]
        //[ObjectPooling(true,1,100)] //设定对象可以放到池中
        [ClassInterface(ClassInterfaceType.AutoDual)]
        /// Enable event tracking
        [EventTrackingEnabled(true)]
        /// Enable JITA for the component
        [JustInTimeActivation(true)]
        [ComVisible(true)]
        public class Transfer:ServicedComponent
        {
            public Transfer()
            {
                ConsoleWrite(" UCOMBANK.TRANSFE Constructor" );
            }
            public Transfer(string s)
            {
                ConsoleWrite(" UCOMBANK.TRANSFE Constructor" + s);
            }

            private void ConsoleWrite(string strInput)
            {
                EventLog.WriteEntry("UCOM COM+ Service Sample", strInput, EventLogEntryType.Information);
            }

            protected override void Activate()
            {
                ConsoleWrite(" UCOMBANK.TRANSFE Activated");
            }

            protected override void Deactivate()
            {
                ConsoleWrite(" UCOMBANK.TRANSFE Deactivated");
            }
            protected override void Construct(string s)
            {
                base.Construct(s);
            }
            protected override bool CanBePooled()
            {
                ConsoleWrite(" UCOMBANK.TRANSFE CanBePooled");
                return true;
            }
            protected override void Dispose(bool disposing)
            {
                ConsoleWrite(" UCOMBANK.TRANSFE Dispose");
               
                base.Dispose(disposing);
            }

            //public int GetBalance(string strConnectiongString, string tableName, string id, ITransaction trans)
            [ComVisible(true)]
            public int GetBalance(string strConnectiongString, string tableName, string id)
            {
                SqlConnection conn = new SqlConnection(strConnectiongString);
                SqlDataAdapter da = new SqlDataAdapter("select * from " + tableName + " where account=" + id, conn);
                DataSet ds = new DataSet();
                //在执行下面的语句时出现错误,错误信息是:"在分布式事务中登记时出错。"
                da.Fill(ds);
                if (ds.Tables[0].Rows.Count > 0)
                    return int.Parse(ds.Tables[0].Rows[0]["accountid"].ToString());
                else
                    return 0;
            }
            [ComVisible(true)]
            public string GetInfo()
            {
                return "My com plus sample...";
            }
            [ComVisible(true)]
            public int GetUserID(string connstr, string tableName, string account)
            {
                try
                {
                    int rst;
                    //rst = GetBalance(connstr, tableName, account, (ITransaction)ContextUtil.Transaction);
                    rst = GetBalance(connstr, tableName, account);
                    ContextUtil.SetComplete();
                    return rst;
                }
                catch (Exception ex)
                {
                    ContextUtil.SetAbort();
                    ConsoleWrite(ex.Message.ToString());
                    return 0;
                }
            }

        }
    }

    三. 注册

      gacutil /i MyComPlusSample.dll 添加到GAC

      regsvcs /fc MyComPlusSample.dll 注册已存在重新注册, fc = find Create

      ComponentServices中找到注册的Com组件, Tab:Security中不勾选Enforce access checks fot this applicaion; Tab:Identity 中指定一用户.

    四. ASP调用

      

    <%

    on error resume next

    dim tempsql
    tempsql = "3eeee---" & _
    "eeerer7777erere"
    set obj = Server.CreateObject("MyComPlusSample.Transfer")

    dim str1
    str1 = obj.GetInfo()
    response.write("strEnCript=" & str1 & "<br>")

    if err.number <> 0 then
     response.write tempsql + "<br>"
     response.write err.description + "<br>"
     response.write "9999999999999999999999999"
     response.end
    end if

    %>

    参考资料:

    http://topic.csdn.net/u/20080625/13/0294fe91-200c-4939-b36b-c9a2c6781354.html


    http://topic.csdn.net/t/20060314/15/4613620.html

    http://cplus.e800.com.cn/articles/2009/211/1234338268521_3.html


    http://topic.csdn.net/t/20020712/10/868557.html
    http://www.itzhe.cn/news/20071123/21768.html

    http://www.cnblogs.com/illele/archive/2007/10/25/937050.html

  • 相关阅读:
    5.9
    5.8
    5.4
    04--深入探讨C++中的引用
    01--Qt扫盲篇
    00--Qt Creator 你必须要掌握的快捷操作
    02--读书笔记之:C++ Primer (第4版)及习题
    01--[转]C++强大背后
    01--数据结构——动态链表(C++)
    04-手把手教你把Vim改装成一个IDE编程环境(图文)
  • 原文地址:https://www.cnblogs.com/blackbean/p/1900904.html
Copyright © 2020-2023  润新知