Tải bản đầy đủ (.pdf) (39 trang)

Tài liệu Creating and Managing Microsoft Windows–Serviced Components pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (403.37 KB, 39 trang )

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


components. Nothing can be further from the truth; there are millions of COM
components in production, and a large number of applications use MTS and Component Services.
So, what are you to do? Throw away all the COM components and rewrite them in Visual
Basic .NET? I don’t think so! You need to find a way that will allow the two environments to
coexist, and that is where serviced components come in.
A serviced component is a .NET component that has been configured to work
in the Component Services environments of Windows 2000 and later. This chapter
will explore the world of Component Services, which includes COM and serviced
components.
CERTIFICATION OBJECTIVE 3.01
Component Services
The Component Services environment of Windows 2000 or later provides the
services of an object resource broker (ORB). The ORB allows clients to make requests
to Component Services for a component, which is hosted by Component Services
on behalf of the client; thus, the client’s method calls and data are marshaled by
Component Services.
In order to be able to install a .NET component in Component Services, you
need to have a reference to the System.EnterpriseServices.dll library as well as import
the System.EnterpriseServices namespace. This namespace provides the main classes,
interfaces, and attributes for communicating with Component Services. The System
.EnterpriseServices namespace gives us the ContextUtil class that is used in order for
an object to participate in transactions and interact with the security information
relating to the object. The ServicedComponent class is what gives the .NET component
the ability to be hosted in Component Services. All .NET component classes that
are to be hosted within Component Services must inherit from this class.
There are also numerous attributes, classes, and method attributes defined in
the namespace; for example, the TransactionAttribute, AutoCompleteAttribute,
and ConstructionEnabledAttribute are contained in the System.EnterpriseServices
namespace.
2

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

be created.

RequiresNew The class will always create a new transaction regardless of
whether one already exists.

Supported The class will participate in an existing transaction but will not
create a new one if one is not present.
The <Transaction()> attribute must be set to Required, RequiresNew,
or Supported for the component to be able to vote on the transaction
outcome.
The following code segment shows how to use the Transaction attribute:
Imports System.EnterpriseServices
<Transaction(TransactionOption.Required)> Public Class Savings
Inherits ServicedComponent
Public Function GetBalance() as Decimal
' Get balance code
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
4
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
The class needs a means of voting on the outcome of the transaction. This is done
by using methods of the ContextUtil object. The following methods are used to vote
on the transaction:


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()

Catch e As Exception
' Signal failure by calling SetAbort()
ContextUtil.SetAbort()
' Pass the exception to the caller
Throw e
End Try
End Sub
This code segment can be rewritten by using the AutoComplete attribute of the
method as in this code segment:
<AutoComplete()>Public Sub Debit(ByVal id As Long, ByVal amount As Decimal)
' Update the account
...
End Sub
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
5
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 3
The AutoComplete attribute makes it possible for us to write code that will signal
success if the method returns normally and failure if an exception is thrown.
Object Pooling
Object pooling allows a preset number of objects to be created in advance. This way,
the objects are ready for the client when called for. When a client requests an object,
it is taken from the pool of available objects, and when the client request is finished,
the object is returned to the pool.
The object pool will improve the performance for objects that require large amounts
of processing time to create and bind to resources during creation of the object.

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

to the object.
Constructor Strings
One issue with the Component Services environment is that there is no possibility
to take advantage of custom constructors—only the default constructor of the class
will be executed. That means that you cannot take advantage of the ability to pass
values to the constructor to initialize the instance. To solve this problem, the
Construct() method provided in the ServicedComponent class gives us the ability to
pick up a string from the Component Service environment that is used during the
construction phase.
The ConstructionEnabled attribute indicates that the class is using the construction
string from the Component Services environment. By overriding the Construct()
method, the class can be customized during instantiation. The following code listing
shows how to implement the Construct() method:
<ConstructionEnabled(True)> Public Class Savings
Inherits ServicedComponent
Private strX As String
Protected Overrides Sub Construct(ByVal s As String)
' This method is called by the Component Service after the
' constructor.
strX = s
End Sub
End Class
Figure 3-1 shows how the string is entered in the Component properties dialog
from Component Services console:
There are additional methods that can be overridden from the ServicedComponent
class; these include Activate() and Deactivate(). The Activate() method will be called
as the object is used from the pool, and the Deactivate() method is called every time
the object is returned to the pool.
Use the Deactivate() method to clean up any changes to initialized members
that might have taken place during the use of the object.

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

String
P:\010Comp\CertPrs8\653-6\ch03.vp
Wednesday, October 30, 2002 9:44:08 AM
Color profile: Generic CMYK printer profile
Composite Default screen
8
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
If bFlag = False Then 'if nothing exists, set default value
sp.Value = ""
End If
Return sp 'return the shared property object
End Function
Use the Shared Property Manager whenever there is a need for serviced
components in the same application to share data.
Component Services
Component Services is managed through the Components Services console from the
Administrative Tools folder (Start | Settings | Control Panel | Administrative Tools |
Component Services). The console is shown next.
The console is also used to read and manage the event log as well as the services
running on a computer. By expanding Components Services in the left panel, you
will see a Computers folder that lists the computers this console can manage.
Expand Computers and then My Computer and then COM+ Applications to see
the installed component applications.
Each application contains the components that make up that particular
application. To view these components, expand System Application and then
Components as shown in Figure 3-2.
If a component is in use, the icon in the right panel will rotate to indicate that the
component is running.

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

key pair generated by the sn.exe utility.
The following is a sample of how to set these attributes in the AssemblyInfo.vb file:
<Assembly: ApplicationName("Masiw Components")>
<Assembly: Description("VB .NET Vineyard Components")>
FIGURE 3-2
The components
in the System
application
P:\010Comp\CertPrs8\653-6\ch03.vp
Wednesday, October 30, 2002 9:44:08 AM
Color profile: Generic CMYK printer profile
Composite Default screen
10
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
<Assembly: ApplicationActivation(ActivationOption.Server)>
<Assembly: AssemblyKeyFile("KeyFile.snk")>
Without the strong-name key, the assembly will not install in
Component Services.
Once the assembly is built, it must be registered—yes, you heard right, you need
to register the assembly in the Registry. The Component Services environment is
still the old COM environment, so you must comply with its rules by entering the
relevant information in the Registry to make our component available. To that end,
the .NET Framework ships with a utility—Regsvcs.exe—that eases registration. To
register an application, use the following syntax:
Regsvcs.exe myApplication.dll
The result is that the components in the myApplication.dll library are registered
in the Registry and installed in Component Services. Regsvcs.exe verifies that there is
no entry for the components in the Registry before performing the registration, and

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

Color profile: Generic CMYK printer profile
Composite Default screen
12
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
2.
After the project is created, delete the Class1.vb file.
3.
Add a new Class file, naming it Pool.vb.
4.
Add a new Class file, naming it NPool.vb.
5.
Add a new Class file, naming it Util.vb. The Solution Explorer should
resemble this image after the Class files are created:
P:\010Comp\CertPrs8\653-6\ch03.vp
Wednesday, October 30, 2002 9:44:09 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
Next, you will implement the utilities in the Util.vb source file. This is where
you keep the common shared properties methods.
6.
Set a reference to the System.EnterpriseServices.dll library—on the Project
menu, select Add Reference…; on the .NET tab, select System.EnterpriseServices
and click Select; and then click OK.
7.
Open the Util.vb source file.
8.

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

the Utils class.
Build a Serviced Component
13
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
12.
In the modUtil module, declare a function named GetSharedProperty() that
returns a SharedProperty. Mark the function as a friend. The code should
look this way:
Module modUtil
Friend Function GetSharedProperty() As SharedProperty
Dim bFlag As Boolean
Dim spm As New SharedPropertyGroupManager()
Dim spg As SharedPropertyGroup, sp As SharedProperty
spg = spm.CreatePropertyGroup("Messages", PropertyLockMode.SetGet, _
PropertyReleaseMode.Process, bFlag)
sp = spg.CreateProperty("History", bFlag)
If bFlag = False Then
sp.Value = ""
End If
Return sp
End Function
End Module
The GetSharedProperty() method will return a reference to the same shared
property for every call. This way, you have a means of sharing this property
between multiple instances of the same class.

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

COM interface, to have pooling enabled with a minimum number of objects
set to 5 and the maximum to 42, and to have the constructor string enabled.
16.
Declare a private string variable in the Pool class, naming the variable strX.
17.
Declare a private sub method AddToSharedProperty(ByVal strY As String)
to the Pool class. Add code to the method as follows:
Private Sub AddToSharedProperty(ByVal strY As String)
'get the "History" shared property
Dim sp As SharedProperty = GetSharedProperty()
sp.Value = sp.Value.ToString & "Pool-" & strY & "(" & strX & ")" & vbCrLf
End Sub
This method gets a reference to the shared property and appends the
parameter to that property.
18.
Declare a default constructor, and call the AddToSharedProperty() method
with a string to indicate that the constructor executed.
Public Sub New()
AddToSharedProperty("Constructor")
End Sub
19.
Declare a Construct() method. It should be protected and overridden from
the ServicedComponent base class. Assign the parameter to the strX variable,
and call the AddToSharedProperty() method to indicate that Construct()
executed.
Protected Overrides Sub Construct(ByVal s As String)
strX = s
AddToSharedProperty("Contruct")
End Sub
20.

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

×