.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Enable Object Construction for a .NET Class

Posted By:Shashi Ray       Posted Date: November 30, 2009    Points: 25    Category: .Net Framework    URL: http://www.dotnetspark.com  
 

 

Enable Object Construction for a .NET Class

You can enable a .NET managed class for object construction by using Enterprise (COM+) Services).

To enable a .NET managed class

  1. Derive your class from the ServicedComponent class located in the System.EnterpriseServices namespace.
2.     using System.EnterpriseServices;
3.     public class DataAccessComponent : ServicedComponent
  1. Decorate your class with the ConstructionEnabled attribute, and optionally specify a default construction string. This default value is held in the COM+ catalog. Administrators can use the Component Services Microsoft Management Console (MMC) snap-in to maintain the value.
5.      [ConstructionEnabled(Default="default DSN")]
6.     public class DataAccessComponent : ServicedComponent
  
  1. Provide an overridden implementation of the virtual Construct method. This method is called after the object's language-specific constructor. The construction string maintained in the COM+ catalog is supplied as the single argument to this method.
8.     public override void Construct( string constructString )
9.     {
10.    // Construct method is called next after constructor.
11.    // The configured DSN is supplied as the single argument
12.  }
  
  1. Provide a strong name for the assembly by signing it through the AssemblyKey file or AssemblyKeyName attribute. Any assembly registered with COM+ services must have a strong name.
14.   [assembly: AssemblyKeyFile("DataServices.snk")]
  
  1. To support dynamic (lazy) registration, use the assembly-level attributes ApplicationName and ApplicationActivation to specify the name of the COM+ application used to hold assembly components and the application's activation type, respectively.
16.  // the ApplicationName attribute specifies the name of the
17.  // COM+ Application which will hold assembly components
18.  [assembly : ApplicationName("DataServices")]
19.   
20.  // the ApplicationActivation.ActivationOption attribute specifies 
21.  // where assembly components are loaded on activation
22.  // Library : components run in the creator's process
23.  // Server : components run in a system process, dllhost.exe
24.  [assembly: ApplicationActivation(ActivationOption.Library)]
  

The following code fragment shows a serviced component called DataAccessComponent, which uses COM+ construction strings to obtain a database connection string.

using System;
using System.EnterpriseServices;
 
// the ApplicationName attribute specifies the name of the
// COM+ Application which will hold assembly components
[assembly : ApplicationName("DataServices")]
 
// the ApplicationActivation.ActivationOption attribute specifies 
// where assembly components are loaded on activation
// Library : components run in the creator's process
// Server : components run in a system process, dllhost.exe
[assembly: ApplicationActivation(ActivationOption.Library)]
 
// Sign the assembly. The snk key file is created using the 
// sn.exe utility
[assembly: AssemblyKeyFile("DataServices.snk")]
 
[ConstructionEnabled(Default="Default DSN")]
public class DataAccessComponent : ServicedComponent
{
    private string connectionString;
    public DataAccessComponent()
    {
      // constructor is called on instance creation
    }
    public override void Construct( string constructString )
    {
      // Construct method is called next after constructor.
      // The configured DSN is supplied as the single argument
      this.connectionString = constructString;
    }
}

 

Shashi Ray


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend