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

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

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

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 [...]... MCAD/MCSD XML 32 Chapter 3: ✓ Web Services and Server Components Development with Visual Basic NET / Lind / 222653-6 / Chapter 3 Creating and Managing Microsoft Windows–Serviced Components TWO-MINUTE DRILL Build a Serviced Component ❑ It is the System.EnterpriseServices namespace that contain all the support for serviced components ❑ Component Services gives components the ability to be transactional,... PoolComponent.snk Microsoft (R) NET Framework Strong Name Utility P:\010Comp\CertPrs8\653-6\ch03.vp Wednesday, October 30, 2002 9:44:09 AM Version 1.0.3705.0 Color profile: Generic CMYK printer profile CertPrs8 Composite Default screen / MCAD/MCSD XML 18 Chapter 3: Web Services and Server Components Development with Visual Basic NET / Lind / 222653-6 / Chapter 3 Creating and Managing Microsoft Windows–Serviced Components. .. component behaves when pooled and not pooled P:\010Comp\CertPrs8\653-6\ch03.vp Wednesday, October 30, 2002 9:44:10 AM Color profile: Generic CMYK printer profile CertPrs8 Composite Default screen / MCAD/MCSD XML 22 Chapter 3: Web Services and Server Components Development with Visual Basic NET / Lind / 222653-6 / Chapter 3 Creating and Managing Microsoft Windows–Serviced Components In order for the client... CMYK printer profile CertPrs8 Composite Default screen / MCAD/MCSD XML 24 Chapter 3: Web Services and Server Components Development with Visual Basic NET / Lind / 222653-6 / Chapter 3 Creating and Managing Microsoft Windows–Serviced Components 13 Add a Button to the form, setting the name to btnFtoC, and change the Text property to “F->C” 14 Add a CheckBox control to the form, naming the control chkPooled... MCAD/MCSD XML 26 Chapter 3: Web Services and Server Components Development with Visual Basic NET / Lind / 222653-6 / Chapter 3 Creating and Managing Microsoft Windows–Serviced Components lblFahrenheit.Text = d.CtoF(CType(txtCelsius.Text, Decimal)) UpdateStatus() End If End Sub 18 Implement the click event for the btnFtoC button to check the chkPool control and call the methods on the Pool class if... control is cleared, and click C->F The result is shown next P:\010Comp\CertPrs8\653-6\ch03.vp Wednesday, October 30, 2002 9:44:11 AM Color profile: Generic CMYK printer profile CertPrs8 Composite Default screen / MCAD/MCSD XML 28 Chapter 3: Web Services and Server Components Development with Visual Basic NET / Lind / 222653-6 / Chapter 3 Creating and Managing Microsoft Windows–Serviced Components The status... modUtil, and place it after the class definition for the Utils class P:\010Comp\CertPrs8\653-6\ch03.vp Wednesday, October 30, 2002 9:44:09 AM Color profile: Generic CMYK printer profile CertPrs8 Composite Default screen / MCAD/MCSD XML 14 Chapter 3: Web Services and Server Components Development with Visual Basic NET / Lind / 222653-6 / Chapter 3 Creating and Managing Microsoft Windows–Serviced Components. .. screen / MCAD/MCSD XML 36 Chapter 3: Web Services and Server Components Development with Visual Basic NET / Lind / 222653-6 / Chapter 3 Creating and Managing Microsoft Windows–Serviced Components ConstructionEnabled(True)>_ Public Class MyClass Inherits ServicedComponent End Class How many objects will be in the pool after one object has been instantiated and released? A 42 B 5 C 1 D 0 Consume a Serviced... 2002 9:44:08 AM Color profile: Generic CMYK printer profile CertPrs8 Composite Default screen / MCAD/MCSD XML 12 Chapter 3: Web Services and Server Components Development with Visual Basic NET / Lind / 222653-6 / Chapter 3 Creating and Managing Microsoft Windows–Serviced Components 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,... 2002 9:44:12 AM Color profile: Generic CMYK printer profile CertPrs8 Composite Default screen / MCAD/MCSD XML 34 Chapter 3: Web Services and Server Components Development with Visual Basic NET / Lind / 222653-6 / Chapter 3 Creating and Managing Microsoft Windows–Serviced Components Public Class MyClass Inherits ServicedComponent Public Sub MySub() UpdateDatabase() End Sub End Class . 3 Creating and Managing Microsoft Windows–Serviced Components CERTIFICATION OBJECTIVES 3.01 Creating and consuming a Serviced Component. namespace. 2 Chapter 3: Creating and Managing Microsoft Windows–Serviced Components CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development

Ngày đăng: 11/12/2013, 14:15

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan