To create a .NET class that can be configured with Enterprise Services, you have to reference the assembly System. Enterprise Services and add the namespace System. Enterprise Services 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 Simple Server. Reference the assembly System. Enterprise Services and add the declaration using System. Enterprise Services to the assmblyinfo.cs and class.cs files.
The ServicedComponent Class
Every serviced component class must derive from the base class ServicedComponent. ServicedComponent itself derives from the class Context Bound Object, 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.
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 17, “Assemblies.”
Some Enterprise Services attributes are also needed. The attribute Application Name 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.
Application Activation allows you to define whether the application should be configured as a library application or a server application, using the options Activation Option. Library or Activation Option. 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 Application Access Control, you can turn off security so that every user is allowed to use the component.
Rename the file Class.cs to Simple Component.cs and add these attributes outside the namespace declaration:
The following table lists the most important assembly attributes that can be defined with Enterprise Services applicat.ions.
Creating the Component
In the Simple Component.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 Greeting with the method Welcome(). The attribute [ComVisible] is required for serviced component classes and interfaces that can be accessed from Enterprise Services features.
The class Simple component derives from the base class ServicedComponent and implements the interface Greeting. 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 [Description] attribute only specifies text that shows up in the Explorer:
The method Welcome() returns only’ 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:
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.
Assemblies with serviced components must be configured with COM+. This configuration can be done automatically or by registering the assembly manually.
If a .NET client application that uses the serviced component is started, the COM+ application is configured automatically. This is true for all classes that are derived from the class ServicedComponent. Application and class attributes such as [EventTrackingEnabled] define the characteristics of the configuration.
Automatic deployment has an important drawback. For automatic deployment to work, the client application needs administrative rights. IT the client application that invokes the serviced component is ASP.NET, the ASP.NET runtime usually doesn’t have administrative rights. With this drawback, automatic deployment is useful only during development time. However, during development, automatic deployment is an extremely advantageous feature because it is not necessary to do manual deployment after every build.
You can deploy the assembly manually with the command-line utility .NET Services installation tool regsvcs.exe. Starting the command
Registers the assembly Simple Server as a COM+ application and configures the included components according to their attributes it also creates a type library that can be used by COM clients accessing the .NET component.
After you’ve configured the assembly, you can start the Component Services Explorer by selecting Administrative Tools~ Component Services from the Windows menu on Windows XP or Windows Server 2003.On Windows Vista you have to start the MMC and add the Component Services snap-in to see the Component Services Explorer. In the left tree view of this application, you can select .. Component Services > Computers > My Computer > COM+ Applications to verify that the application
Creating an Installer Package
With the Component Services Explorer, you can create Windows installer packages for server or client .systems. An installer package for the server includes the assemblies and configuration settings to install the application on a different server. If the serviced component is invoked from applications running on different systems, a proxy must be installed on the client system. The installer package for the-client includes assemblies and configuration for proxies.
To create an installer package, you can start the Component Services Explorer, select the COM+ application, select the menu options Action ~ Export, and click the Next button in the first dialog.
The dialog shown in Figure 44-3 opens. In this dialog, you can export either a Server application or an application proxy. With the option Server application you can also configure to export user identities with roles. This option should be selected only if the target system is in the same domain as the system where the package is created, because the configured user identities are put into the installer package. With the option application proxy, an installer package for the client system is created.
The option to create an application proxy is not available if the application is configured as a library application.
To install the proxy, you just have to start setup.exe from the installer package. Be aware that an application proxy cannot be installed on the same system where the application is installed. After installation of the application proxy, you can see an entry in Component Services Explorer that represents the application proxy. With the application proxy the only option that can be configured is the name of the server in the Activation tab, as discussed in the next section.
Component Services Explorer
After a successful configuration, you can see Wrox Enterprise Demo as an application name in the.tree view of the Component Services Explorer. This name was set by the attribute [ApplicationName]. Selecting Action > Properties opens the dialog box shown in Figure 44-4. Both the name and the description have been configured by using attributes. When you select the Activation tab, yon can see that the application is configured as a server application because this has been defined with the [ApplicationActivation] attribute, and selecting the Security tab shows that the “Enforce access checks for this application” option is not selected because the attribute [ApplicationAccessControl] was set to false.
The following is a list of some more options that can be set with this application:
Security – With the security configuration, you can enable or disable access checks. U security is enabled, you can set access checks to the application level, the component, the interface, and to the method level. It is also possible to encrypt messages that are sent across the network using packet privacy as an authentication level for calls of course, this also increases the overhead.
Identity – With server applications, you can use the Identity tab to configure the user account that will be used for the process that hosts the application. By default, this is the interactive user. This setting is very useful while debugging the application but cannot be used on a production system if the application is running on a server, because there might not be anybody logged on. Before installing the application on the production system you should test the application by using a specific user. for the application.
Activation – The Activation tab allows you to configure the application either as a library or as a server application. Two new options with COM+ 1.5 are the option to run the application as a Windows Service and to use SOAP to access the application. Windows Services are discussed in Chapter 23, “Windows Services.” Selecting the SOAP option uses .NET Remoting configured within Internet Information Server to access the component, Instead of using .NET Remoting,
later in this chapter the component will be accessed using WCF. WCF is discussed in Chapter 42, “Windows Communication Foundation.”
With an application proxy, the option “Remote server name” is the only option that can be configured. This option sets the name of the server. By default, the DCOM protocol is used as the network protocol. However, if SOAP is selected in the server configuration, the communication happens through .NET Remoting. .
Queuing – The Queuing configuration is required for service components that make use of message queuing.
Advanced – On the Advanced tab, you can specify whether the application should be shut down after a certain period of client inactivity. You can also specify whether to lock a certain configuration so that no one can change it accidentally.
Dump – If the application crashes, you can specify the directory where the dumps should be stored. This is useful for components developed with C++.
Pooling and Recycling – Pooling and recycling is a new option with COM+ 1.5. With this option, you can configure whether the application should be restarted (recycled) depending on application lifetime, memory needs, number of calls, and so on.
With the Component Services Explorer, you can also view and configure the component itself. When opening child elements of the application, you can view the component Wrox. Pro-CSharp. Enterprise Services. SimpleComponent. Selecting Action > Properties opens the dialog box shown in Figure 44-5.
Using this dialog box, you can configure these options:
Transactions – On the Transactions tab, you can specify whether the component requires transactions. You use this feature in the next example.
Security – lf security is enabled for the application, with this configuration you can define what roles-are allowed to use the component.
Activation – The Activation configuration enables you to set object pooling and to assign a construction string.
Concurrency – If the component is not thread-safe, concurrency can be set to Required or Requires New. This way the COM+ runtime allows only one thread at a time to access the component.