Export (0) Print
Expand All

How to: Configure Object Construction

[Note: This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

The following procedure and example describe how to configure object construction and set the default initialization string of the TestObjectConstruct class to the string "Initial Catalog=Northwind;Data Source=.\\SQLServerInstance;Trusted_Connection=yes". This string is used to connect to a SQL Server database. The topic, Object Construction, explains further the use of COM+ object construction from the System.EnterpriseServices.ServicedComponent class.

To configure object construction and set the default initialization string for a class

  1. Define a class that derives directly or indirectly from the System.EnterpriseServices.ServicedComponent class. For example, the following code shows a class TestObjectConstruct that derives directly from the System.EnterpriseServices.ServicedComponent class.

    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. Apply the ConstructionEnabled attribute to the class and set the attribute's Default property. For example, the following code applies the ConstructionEnabled attribute to the TestObjectConstruct class and sets the Default property to the SQL Server connection string.

    <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. Override the Construct method.

    <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. In the client application, create an instance of the component class without specifying a construction string, so that the default is used. For example, the following code creates an instance of the TestObjectConstruct class, and the construction string defaults to "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();
          }
    }
    

Conclusion

Once you have installed the component services application, you can specify construction strings by using the Component Services administrative tool. To enter an object construction string for a component, perform the following steps:

  1. Open the Component Services administrative tool.

  2. In the Component Services administrative tool, right-click the component that you want to configure, and then click Properties.

  3. If you have not set the ConstructionEnabled attribute to True, in the Properties dialog box, on the Activation tab, select the Enable object construction check box to enable use of the object construction string.

  4. If you want to change the construction string from the default specified with the ConstructionEnabled attribute, in the Constructor string box, enter the construction string.

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();
     }
    }
}
Show:
© 2014 Microsoft