In this section,
we
explain how to configure COM+ security with .NET attributes; we do
this by creating and deploying an example serviced component. We
demonstrate how to use COM+ RBS and PAS to control access to a simple
component that tracks software defects for an imaginary software
product called SecurityPro.
We begin by defining the
IDefectTracker interface, which contains the three
methods we use to simulate defect tracking. We briefly describe the
purpose of each interface method in the list below. We are developing
this component to demonstrate COM+ security, so we do not define
types to represent defects, or specify method arguments:
ViewAllDefects
-
Returns all of the pending defects for the product
CreateNewDefect
-
Creates a new record representing a pending defect
CloseDefect
-
Closes a defect and removes it from the pending list
The following statements define our simple
IDefectTracker interface:
# C#
using System;
public interface IDefectTracker {
void ViewAllDefects( );
void CreateNewDefect( );
void CloseDefect( );
}
# Visual Basic .NET
Imports System
Public Interface IDefectTracker
Sub ViewAllDefects( )
Sub CreateNewDefect( )
Sub CloseDefect( )
End Interface
In the following sections, we create a serviced component that
implements the IDefectTracker interface, and apply
.NET attributes to configure COM+ RBS and PAS.
1. Creating the Serviced Component
A serviced
component
must inherit from the
System.EnterpriseServices.ServicedComponent class and must be a member of a
strong-named assembly. The following
statements list the SecurityProTracker class,
which extends ServicedComponent and implements the
IDefectTracker interface.
# C#
using System;
using System.EnterpriseServices;
// Specify the file containing the key for assembly signing
[assembly: System.Reflection.AssemblyKeyFile("mykey.key")]
public class SecurityProTracker: ServicedComponent, IDefectTracker {
public void ViewAllDefects( ) {}
public void CreateNewDefect( ) {}
public void CloseDefect( ) {}
}
# Visual Basic .NET
Imports System
Imports System.EnterpriseServices
' Specify the file containing the key for assembly signing
<Assembly: System.Reflection.AssemblyKeyFile("mykey.key")>
Public Class SecurityProTracker
Inherits ServicedComponent
Implements IDefectTracker
Public Sub ViewAllDefects( ) Implements IDefectTracker.ViewAllDefects
End Sub
Public Sub CreateNewDefect( ) Implements IDefectTracker.CreateNewDefect
End Sub
Public Sub CloseDefect( ) Implements IDefectTracker.CloseDefect
End Sub
End Class
2. Specifying the COM+ Application Type
The System.EnterpriseServices.ApplicationActivation
class is the first COM+ attribute we apply, allowing us to specify
whether we want a COM+ server or library application. This attribute
accepts a single parameter, which must be a value from the
System.EnterpriseServices.ActivationOption
enumeration, the values of which are detailed in Table 1. Application proxies are created with the
Component Services tool; consult the Windows documentation for
details.
Table 1. The enumeration values
Value
|
Description
|
---|
Library
|
Specifies that serviced components are activated in the client
application process
|
Server
|
Specifies that serviced components are activated in a process
provided by COM+
|
The emphasized statements in the code fragment below demonstrate the
use of the ApplicationActivation attribute to
specify a COM+ server application for our example component:
# C#
using System;
using System.EnterpriseServices;
// Specify the file containing the key for assembly signing
[assembly: System.Reflection.AssemblyKeyFile("mykey.key")]
// Specify that we want a COM+ Server application
[assembly: ApplicationActivation(ActivationOption.Server)]
public class SecurityProTracker: ServicedComponent, IDefectTracker {
# Visual Basic .NET
Imports System
Imports System.EnterpriseServices
' Specify the file containing the key for assembly signing
<Assembly: System.Reflection.AssemblyKeyFile("mykey.key")>
' Specify that we want a COM+ Server application
<Assembly: ApplicationActivation(ActivationOption.Server)>
Public Class SecurityProTracker
Inherits ServicedComponent
Implements IDefectTracker