如何:配置对象构造

下面的过程和示例描述如何配置对象构造并将 TestObjectConstruct 类的默认初始化字符串设置为字符串 "Initial Catalog=Northwind;Data Source=.\\SQLServerInstance;Trusted_Connection=yes"。此字符串用于连接 SQL Server 数据库。对象构造主题解释如何从 ServicedComponent 类进一步使用 COM+ 对象构造。

配置对象构造并设置类的默认初始化字符串

  1. 定义直接或间接从 System.EnterpriseServices.ServicedComponent 类派生的类。例如,下面的代码显示直接从 System.EnterpriseServices.ServicedComponent 类派生的类 TestObjectConstruct

    Imports System.EnterpriseServices
    Imports System
    Imports System.Data
    Imports System.Data.SqlClient
    …
    Public Class TestObjectConstruct 
          Inherits ServicedComponent
          …
    End Class
    
    using System;
    using System.EnterpriseServices;
    using System.Data;
    using System.Data.SqlClient;
    
    …
    public class TestObjectConstruct : ServicedComponent
    {
       …
    }
    
  2. ConstructionEnabledAttribute 特性应用于该类并设置该特性的 Default 属性。例如,下面的代码将 ConstructionEnabledAttribute 特性应用于 TestObjectConstruct 类并将 Default 属性设置为 SQL Server 连接字符串。

    <ConstructionEnabled([Default] := "Initial Catalog=Northwind;Data Source=.\\SQLServerInstance;Trusted_Connection=yes")> _
    Public Class TestObjectConstruct 
        …
    End Class
    
    [ConstructionEnabled(Default="Initial Catalog=Northwind;Data Source=.\\SQLServerInstance;Trusted_Connection=yes")]
    public class TestObjectConstruct : ServicedComponent
    {
        …
    }
    
  3. 重写 Construct 方法。

    <ConstructionEnabled([Default] := "Initial Catalog=Northwind;Data Source=.\\SQLServerInstance;Trusted_Connection=yes")> _
    Public Class TestObjectConstruct 
          Inherits ServicedComponent
          Private m_connectStr As String
          Private conn as SqlConnection
          Protected Overrides Sub Construct(constructString As String)
          ' Called after constructor.
            m_connectStr = constructString
          End Sub 
          Public Sub ConnectToDatabase()
              conn = New SqlConnection(m_connectStr)
          End Sub 
    
    End Class 
    
    [C#]
    [ConstructionEnabled(Default="Initial Catalog=Northwind;Data Source=.\\SQLServerInstance;Trusted_Connection=yes")]
    public class TestObjectConstruct : ServicedComponent
    {
          private string connectStr;
          SqlConnection conn;
          public TestObjectConstruct()
          {
            …
          }
          protected override void Construct(string constructString)
          {
             // Called after constructor.
             connectStr = constructString;
         }
         public void ConnectToDatabase()
         {
              conn = new SqlConnection(connectStr);
              conn.Open();
         }
    }
    
  4. 在客户端应用程序中,创建组件类的实例但不指定构造字符串,以便使用默认值。例如,下面的代码创建 TestObjectConstruct 类的一个实例,并且构造字符串默认为 "Initial Catalog=Northwind;Data Source=.\\SQLServerInstance;Trusted_Connection=yes"

    Public Class App
          Overloads Public Shared Sub Main()
                Dim order As New TestObjectConstruct()
                order.ConnectToDatabase()
          End Sub
    End Class
    
    public class App
    {
          public static void Main()
          {
                TestObjectConstruct order = new TestObjectConstruct();
                order. ConnectToDatabase();
          }
    }
    

安装了组件服务应用程序后,就可以通过使用“组件服务”管理工具指定构造字符串。若要输入组件的对象构造字符串,请执行下列步骤:

  1. 打开“组件服务”管理工具。

  2. 在“组件服务”管理工具中,右键单击要配置的组件,然后单击“属性”。

  3. 如果尚未将 ConstructionEnabled 特性设置为 True,则在“属性”对话框的“激活”选项卡上,选中“启用对象构造”复选框,以启用对象构造字符串。

  4. 如果要更改使用 ConstructionEnabled 属性指定的默认构造字符串,请在“构造函数字符串”****框中输入构造字符串。

示例

Imports System.EnterpriseServices
Imports System
Imports System.Data
Imports System.Data.SqlClient
<assembly: ApplicationName("OCDemo")>

Namespace OCDemo 
    <ConstructionEnabled([Default] := "Initial Catalog=Northwind;
       Data Source=.\\SQLServerInstance;Trusted_Connection=yes")> _
    Public Class TestObjectConstruct 
         Inherits ServicedComponent
      Private m_connectStr As String
      Private conn as SqlConnection
      Protected Overrides Sub Construct(constructString As String)
      ' Called after constructor.
        m_connectStr = constructString
      End Sub 
      Public Sub ConnectToDatabase()
          conn = New SqlConnection(m_connectStr)
      End Sub 
    End Class 
End Namespace 
using System;
using System.EnterpriseServices;
using System.Data;
using System.Data.SqlClient;
[assembly : ApplicationName("OCDemo")]

namespace OCDemo
{
    [ConstructionEnabled(Default="Initial Catalog=Northwind;
       Data Source=.\\SQLServerInstance;Trusted_Connection=yes")]
    public class TestObjectConstruct : ServicedComponent
    {
      private string connectStr;
      SqlConnection conn;
      public TestObjectConstruct()
      {
        …
      }
      protected override void Construct(string constructString)
      {
         // Called after constructor.
         connectStr = constructString;
     }
     public void ConnectToDatabase()
     {
          conn = new SqlConnection(connectStr);
          conn.Open();
     }
    }
}

请参见

参考

ConstructionEnabledAttribute
System.EnterpriseServices

概念

对象构造
可用的 COM+ 服务摘要

Footer image

版权所有 (C) 2007 Microsoft Corporation。保留所有权利。