-
Launch Visual Studio .NET and create a new blank solution.
-
Add a new Visual Basic class library project to the solution. Name the new project InventoryService.
-
Right-click the Class1.vb file and rename it Inventory.vb.
-
Right-click the References folder and select Add Reference. Select the System.EnterpriseServices reference from the .NET tab of the Add Reference dialog box and click OK.
-
Add code to create the Inventory class as a serviced component:
Imports System.EnterpriseServices Public Interface ISell Sub Sell(ByVal Amount As Integer) End Interface <ComponentAccessControl(), _ SecureMethod(), _ SecurityRole("InventoryUsers")> _ Public Class Inventory Inherits ServicedComponent Implements ISell <AutoComplete(), _ SecurityRole("InventoryUsers")> _ Public Sub Sell(ByVal Amount As Integer) _ Implements ISell.Sell ' Work of the component would go here End Sub End Class
-
Open a Visual Studio .NET command prompt. Navigate to the directory containing the InventoryService project and create a key file by entering this command:
sn k InventoryService.snk
-
Open the AssemblyInfo.vb file. Add a line of code at the top of the file:
Imports System.EnterpriseServices
-
Add attributes at the end of the AssemblyInfo.vb file, replacing the default AssemblyVersion attribute:
<Assembly: AssemblyVersion("1.0.0.0")> <Assembly: ApplicationName("InventoryComponent")> <Assembly: AssemblyKeyFile(_ "..\..\InventoryService.snk")> <Assembly: ApplicationAccessControl(_ AccessChecksLevel:= _ AccessChecksLevelOption.ApplicationComponent)> <Assembly: SecurityRole("InventoryUsers")>
-
Build the project to create InventoryService.dll.
-
Switch to the Visual Studio .NET command prompt and navigate to the project's bin directory. Enter this command to register the assembly with COM+:
regsvcs InventoryService.dll
-
Add a new Visual Basic .NET Windows application project to the solution. Name the new project InventoryClient . Set the new project as the startup project for the solution.
-
Right-click the References folder and select Add Reference. Select the Projects tab and create a reference to the InventoryService project.
-
Place a Label control, a TextBox control named txtAmount , and a Button control named btnSell on the default form in the project.
-
Double-click the button to switch to the form's module. Add a statement at the top of the module:
Imports InventoryService
-
Add code to handle the button's Click event:
Private Sub btnSell_Click(_ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnSell.Click Try Dim inv As Inventory = New Inventory() inv.Sell(txtAmount.Text) MessageBox.Show("Sale succeeded") Catch ex As Exception MessageBox.Show(ex.Message, _ "Exception occurred") End Try End Sub
-
Run the solution, enter a number in the text box, and click the button. You'll receive an exception message as shown in Figure 11.10 because you're not a member of the InventoryUsers COM+ role. Close the form.
Figure 11.10. Enterprise service not available to the current user .
-
Select Start, Programs, Administrative Tools, Component Services to launch the Component Services Tool.
-
In the Component Services tool, expand the tree to locate the Users node for the InventoryUsers role, as shown in Figure 11.11. You can get there by drilling down through Component Services, COM+ Applications, InventoryComponent, Roles, InventoryUsers, Users. Right-click the node and select New, User. In the Select Users or Groups dialog box, locate your own user account, click Add, and then OK.
Figure 11.11. Configuring roles for a serviced component.
-
Run the solution again, enter a number in the text box, and click the button again. This time, the call will succeed because your account is a member of the proper COM+ role.