Creating a Simple COM Application


Creating a Simple COM+ Application

To create a .NET class that can be configured with Enterprise Services, you have to reference the assembly System.EnterpriseServices and add the namespace System.EnterpriseServices to the using declarations. The most important class to use is ServicedComponent.

The first example shows the basic requirements to create a serviced component. You start by creating a C# library application. All COM+ applications must be written as library applications regardless of whether they will run in their own process or in the process of the client. Name the library SimpleServer. Reference the assembly System.EnterpriseServices and add the declaration using System.EnterpriseServices; to the assmblyinfo.cs and class1.cs files.

Class ServicedComponent

Every serviced component class must derive from the base class ServicedComponent. ServicedComponent itself derives from the class ContextBoundObject, so an instance is bound to a .NET Remoting context.

The class ServicedComponent has some protected methods that can be overridden, as shown in the following table.

Open table as spreadsheet

Protected Method

Description

Activate() Deactivate()

The Activate() and Deactivate() methods are called if the object is configured to use object pooling. When the object is taken from the pool, the Activate() method is called. Before the object is returned to the pool, Deactivate() is called.

CanBePooled()

This is another method for object pooling. If the object is in an inconsistent state, you can return false in your overridden implementation of CanBePooled(). This way the object is not put back into the pool, but destroyed instead. A new object will be created for the pool.

Construct()

This method is called at instantiation time, where a construction string can be passed to the object. The construction string can be modified by the system administrator. Later in this chapter, you use the construction string to define the database connection string.

Sign the Assembly

Libraries configured with Enterprise Services need a strong name. For some Enterprise Services features it is also necessary to install the assembly in the global assembly cache. Strong names and the global assembly cache are discussed in Chapter 16, “Assemblies.”

Assembly Attributes

Some Enterprise Services attributes are also needed. The attribute ApplicationName defines the name of the application as it will be seen in the Component Services Explorer. The value of the Description attribute shows up as a description within the application configuration tool.

ApplicationActivation allows you to define whether the application should be configured as a library application or a server application, using the options ActivationOption.Library or ActivationOption .Server. With a library application, the application is loaded inside the process of the client. In that case the client might be the ASP.NET runtime. With a server application, a process for the application is started. The name of the process is dllhost.exe. With the attribute ApplicationAccessControl, you can turn off security so that every user is allowed to use the component.

You can add these attributes to the file class1.cs outside the namespace declaration:

  [assembly: ApplicationName("Wrox EnterpriseDemo")] [assembly: Description("Wrox Sample Application for Professional C#")] [assembly: ApplicationActivation(ActivationOption.Server)] [assembly: ApplicationAccessControl(false)] 

The following table lists the most important assembly attributes that can be defined with Enterprise Services applications.

Open table as spreadsheet

Attribute

Description

[ApplicationName]

The attribute [ApplicationName] defines the name for the COM+ application that shows up in the Component Services Explorer after the component is configured.

[ApplicationActivation]

The attribute [ApplicationActivation] defines if the application should run as a library within the client application or if a separate process should be started. The options to configure are defined with the enumeration ActivationOption. ActivationOption.Library defines to run the application inside the process of the client; ActivationOption.Server starts its own process, dllhost.exe.

[ApplicationAccessControl]

The attribute [ApplicationAccessControl] defines the security configuration of the application. Using a Boolean value you can set whether access control should be enabled or disabled. With the Authentication property you can set privacy levels - whether the client should be authenticated with every method call or just with the connection, and whether the data sent should be encrypted.

Creating the Component

In the class1.cs file, you can create your serviced component class. With serviced components, it is best to define interfaces that are used as the contract between the client and the component. This is not a strict requirement, but some of the Enterprise Services features (such as setting role-based security on a method or interface level) do require interfaces. Create the interface IGreeting with the method Welcome(). The attribute [ComVisible] is required for serviced component classes and interfaces that can be accessed from Enterprise Services features.

  using System; using System.EnterpriseServices; using System.Runtime.InteropServices; namespace Wrox.ProCSharp.EnterpriseServices {    [ComVisible(true)]    public interface IGreeting    {       string Welcome(string name);    } 

The class SimpleComponent derives from the base class ServicedComponent and implements the interface IGreeting. The class ServicedComponent acts as a base class of all serviced component classes, and offers some methods for the activation and construction phases. Applying the attribute [EventTrackingEnabled] to this class makes it possible to monitor the objects with the Component Services Explorer. By default, monitoring is disabled because using this feature reduces performance. The attribute [Description] only specifies text that shows up in the Explorer:

  [EventTrackingEnabled(true)] [ComVisible(true)] [Description("Simple Serviced Component Sample")] public class SimpleComponent : ServicedComponent, IGreeting {    public SimpleComponent()    {    } 

The method Welcome() only returns “Hello, “ with the name that is passed to the argument. So that you can see some visible result in the Component Services Explorer while the component is running, Thread.Sleep() simulates some processing time:

        public string Welcome(string name)       {          // simulate some processing time          System.Threading.Thread.Sleep(1000);          return "Hello, " + name;       }    } } 

Other than applying some attributes and deriving the class from ServicedComponent, there’s nothing special to do with classes that should use Enterprises Services features. All that is left to do is build and deploy a client application.

In the first sample component, the attribute [EventTrackingEnabled] was set. Some more commonly used attributes that influence the configuration of serviced components are described in the following table.

Open table as spreadsheet

Attribute Class

Description

[EventTrackingEnabled]

Setting the attribute [EventTrackingEnabled] allows monitoring the component with the Component Services Explorer. Setting this attribute to true has some additional overhead associated; that’s why, by default, event tracking is turned off.

[JustInTimeActivation]

With this attribute, the component can be configured to not activate when the caller instantiates the class, but instead when the first method is invoked. Also, with this attribute the component can deactivate itself.

[ObjectPooling]

If the initialization time of a component is long compared to the time of a method call, an object pool can be configured with the attribute [ObjectPooling]. With this attribute, minimum and maximum values can be defined that influence the number of objects in the pool.

[Transaction]

The attribute [Transaction] defines transactional characteristics of the component. Here, the component defines whether a transaction is required, supported, or not supported.




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net