3
Creating and
Managing Microsoft
Windows–Serviced
Components
CERTIFICATION OBJECTIVES
3.01 Creating and consuming a Serviced
Component
3.02 Implementing a Serviced Component
3.03 Creating Interfaces That Are Visible
to COM
3.04 Creating a strongly Named Assembly
3.05 Registering the Component in the
Global Assembly Cache
3.06 Managing the Component by Using
the Component Services Tool
✓
Two-Minute Drill
Q&A
Self Test
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 3
P:\010Comp\CertPrs8\653-6\ch03.vp
Wednesday, October 30, 2002 9:44:07 AM
Color profile: Generic CMYK printer profile
Composite Default screen
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind /
222653-6 / Chapter 3
Y
ou may suppose that now that you have the .NET Framework and assemblies that
don’t need to be registered in the Registry, you have finally moved beyond the COM
Chapter 3: Creating and Managing Microsoft Windows–Serviced Components
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind /
222653-6 / Chapter 3
P:\010Comp\CertPrs8\653-6\ch03.vp
Wednesday, October 30, 2002 9:44:07 AM
Color profile: Generic CMYK printer profile
Composite Default screen
Component Services
3
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 3
Transactions
A transaction is defined as a group of operations that either completes as a whole
(commits) or returns all operations to the state prior to the transaction (rolls back) if
it fails to complete. Transactions are described as an all-or-nothing workflow.
Components in the Component Services environment have the ability to participate
in transactions by voting on the outcome of the operations. The voting is performed
by the code of the component calling methods that signify successful completion or
failure of the process the component performs. When a component is instantiated in
Component Services, the component’s participation in transactions can be found in
the Transaction attribute. The following list describes the possible values for the
Transaction attribute:
■
Disabled The class will not use transactions and will ignore any
transactions from the parent object.
■
NotSupported The class will not be instantiated within the context of a
transaction.
■
Required The class requires a transaction. If no transaction exists, one will
SetAbort( ) Vote for failure of the transaction. If any part of the transaction
calls SetAbort() and votes for a failure, the object is destroyed at the return of
the method call.
■
SetComplete( ) Vote for success of the transaction. In this instance, the
object is destroyed at the return of the method call.
■
EnableCommit( ) Vote for success of the transaction. The object will not be
destroyed after the method call, allowing us to keep state across method calls.
■
DisableCommit( ) Vote for the unsuccessful completion of the transaction.
The object will not be destroyed after the return of the method call.
The object is deactivated if you call
SetComplete()
or
SetAbort()
. If you
need to keep state (maintain an active object) between calls to the object,
use
EnableCommit()
or
DisableCommit()
instead.
The following code sample shows how to use the transaction methods:
Public Sub Debit(ByVal id As Long, ByVal amount As Decimal)
Try
' Update the account
…
' Signal success by calling SetComplete()
ContextUtil.SetComplete()
To enable object pooling, you use the ObjectPooling attribute—this attribute has
two parameters that control the initial size of the pool as well as the maximum size.
■
MinPoolSize Controls the initial size of the pool.
■
MaxPoolSize Sets the maximum number of objects that can be created in
the pool.If the number of objects in the pool has reached the MaxPoolSize
value and additional requests for objects are received, the requests will be
queued until objects are made available.
Always set a MaxPoolSize if you enable pooling. That way, the server will not
be affected by a client application that keeps requesting more objects.
The return of objects to the object pool is controlled by the CanBePooled()
method. If your object supports object pooling and can safely be returned to the
pool, return True; if not, return False.
The following code segment illustrates the use of the ObjectPooling attribute:
<ObjectPooling(Enabled:=True,MinPoolSize:=3,MaxPoolSize:=42)> _
Public Class Savings
Inherits ServicedComponent
Public Function GetBalance() As Decimal
...
End Function
Protected Overrides Function CanBePooled() As Boolean
Return True
End Function
End Class
P:\010Comp\CertPrs8\653-6\ch03.vp
Wednesday, October 30, 2002 9:44:07 AM
Color profile: Generic CMYK printer profile
Composite Default screen
Set the size of the pool to meet the expected number of concurrent accesses
6
Chapter 3: Creating and Managing Microsoft Windows–Serviced Components
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind /
222653-6 / Chapter 3
P:\010Comp\CertPrs8\653-6\ch03.vp
Wednesday, October 30, 2002 9:44:08 AM
Color profile: Generic CMYK printer profile
Composite Default screen
Component Services
7
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 3
There has always been an issue with how to give components in an application
access to common data, and the Component Services solution is to use the Shared
Property Manager (SPM). The following code gives an example of how to create and
use a shared property:
Friend Function GetSharedProperty() As SharedProperty
Dim bFlag As Boolean
Dim strValue As String
Dim spm As New SharedPropertyGroupManager()
Dim spg As SharedPropertyGroup
Dim sp As SharedProperty
'create a group called "Messages"
spg = spm.CreatePropertyGroup("Messages", PropertyLockMode.SetGet, _
PropertyReleaseMode.Process, bFlag)
'create a property called "History" for storing the event history
sp = spg.CreateProperty("History", bFlag)
FIGURE 3-1
Setting the
Construction
You can right-click any of the objects to display a context menu that includes,
among other things, a menu to the properties of the object. It is through this
properties dialog that you can modify the settings for the component.
Assembly Configuration
In order to support the installation of a .NET assembly into Component Services,
four entries must be made in a file that is stored as part of the Visual Basic .NET
P:\010Comp\CertPrs8\653-6\ch03.vp
Wednesday, October 30, 2002 9:44:08 AM
Color profile: Generic CMYK printer profile
Composite Default screen
Component Services
9
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 3
project for the component. The AssemblyInfo.vb file contains settings that affect the
metadata of the assembly, and the following four entries must be used in order to
support a .NET assembly:
■
ApplicationName This attribute is used to specify the application that the
component will be installed in.
■
Description Sets the description of the Component Services application.
■
ApplicationActivation Specifies whether the Component Services application
is implemented as a library or server application. A library application will work
only with clients on the local computer. When the client requests the object, it
will execute outside of Component Services. The server activation manages the
execution of the component in Component Services.
■
AssemblyKeyFile Set this attribute to the file that contains the strong-name
if there is a preexisting entry, the existing entry will be deleted. This ensures that you
have only current information in the Registry.
The Windows Registry is very much a part of Component Services, but
through the use of Regsvcs.exe you do not have to directly edit the Registry.
To be on the safe side when updating components, I usually delete the
application in the Component Services console before reinstalling,
When all the components in the assembly are registered in Component Services,
you can optionally defer the registration until the first time a client application
requests a component that inherits from ServiceComponent. This is called
automatic registration.
Now that you have covered the theory of serviced components, it is time to start
building and using them.
P:\010Comp\CertPrs8\653-6\ch03.vp
Wednesday, October 30, 2002 9:44:08 AM
Color profile: Generic CMYK printer profile
Composite Default screen
Build a Serviced Component
11
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 3
CERTIFICATION OBJECTIVE 3.02
Build a Serviced Component
You will build a serviced component that highlights the use of the object pool.
EXERCISE 3-1
Build the Serviced Component
1.
Create a new Visual Basic .NET project based on a Class Library template.
Name the project PoolComponent:
P:\010Comp\CertPrs8\653-6\ch03.vp
Wednesday, October 30, 2002 9:44:08 AM
Import the following namespaces: System.EnterpriseServices and System
.Runtime.InteropServices. These namespaces give us support for Component
Services as well as COM interfaces.
9.
Define the Utils class as follows:
Imports System.EnterpriseServices
Imports System.Runtime.InteropServices
<ClassInterface(ClassInterfaceType.AutoDual)> _
Public Class Utils
Inherits ServicedComponent
...
End Class
The attribute declares the class to have a dual COM interface so that
the component can be use by clients built with Visual Basic 6.0 or
VBScript.
10.
In the Util class, define a method named GetHistory() as shown in this
code segment:
'use the shared property manager to retrieve current event history
Public Function GetHistory() As String
Dim strValue As String
Dim sp As SharedProperty = GetSharedProperty()
strValue = sp.Value
sp.Value = "" 'clear report string
Return strValue
End Function
The GetSharedProperty() method is defined further down in this exercise; it
returns a reference to the shared property for this component.
11.
Declare a module named modUtil, and place it after the class definition for
Next you will implement the Pool class:
13.
Open the Pool.vb file in the editor.
14.
Import the System.EnterpriseServices and System.Runtime.InteropServices
namespaces.
15.
Define the Pool class as follows:
<Transaction(TransactionOption.Required), _
ClassInterfaceAttribute(ClassInterfaceType.AutoDual), _
ObjectPooling(Enabled:=True, MinPoolSize:=5, MaxPoolSize:=42), _
ConstructionEnabled(True)> _
Public Class Pool
Inherits ServicedComponent
End Class
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind /
222653-6 / Chapter 3
14
Chapter 3: Creating and Managing Microsoft Windows–Serviced Components
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind /
222653-6 / Chapter 3
P:\010Comp\CertPrs8\653-6\ch03.vp
Wednesday, October 30, 2002 9:44:09 AM
Color profile: Generic CMYK printer profile
Composite Default screen
Build a Serviced Component
15
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 3
The attributes define this class to require transactions, to support a dual
Declare a protected overridden CanBePooled() method. Call the
AddToSharedProperty() method to indicated you were called and return True.
Protected Overrides Function CanBePooled() As Boolean
AddToSharedProperty("CanBePooled")
Return True
End Function
P:\010Comp\CertPrs8\653-6\ch03.vp
Wednesday, October 30, 2002 9:44:09 AM
Color profile: Generic CMYK printer profile
Composite Default screen