This step-by-step article demonstrates how to create a
serviced .NET component that uses transactions. This article also demonstrates
how to create a client that tests your serviced component. Microsoft Enterprise
Services provides Microsoft COM+ services to .NET components.
Important notes
- Use a strong name for a serviced components.
- Register a serviced components in the Global Assembly Cache
(GAC) because they are system-level resources. Install server applications in
the GAC. Microsoft recommends that you register library applications in the
GAC, although this is not a requirement.
- You can use either of the following methods to register
serviced components with COM+:
- Install Microsoft SQL Server on the local computer to use
the example in this article.
- This sample is intended only for illustration purposes. You
can use the SELECT query shown in the sample outside a COM+ transaction because
COM+ uses the highest isolation level for the transaction. To improve database
throughput, use a READ query for lower transaction levels.
Create the serviced .NET component
- Create a new Visual Basic Class Library project, and name
it ServicedCOM.
- Rename the default class to
VBServCom.
- Add a reference to the System.EnterpriseServices namespace.
- Type or paste the following code before the class
statement:
Imports System.EnterpriseServices
Imports System.Data.SqlClient
- Add the following code after the class statement:
Inherits EnterpriseServices.ServicedComponent
- Add the following code before the class statement (do not
forget the line continuation character "_"):
<Transaction(TransactionOption.RequiresNew)> _
- Add the following attributes to the AssemblyInfo.vb file:
<assembly: ApplicationActivation(ActivationOption.Library)>
<assembly: ApplicationName("SimpleTrans")>
NOTES:
- The ActivationOption attribute indicates whether the component will be started in the
caller's process. You can set Activation.Option to either Library or Server.
- The ApplicationName attribute is the name that appears for the COM+ application in
the COM+ Catalog and the Component Services Administration console.
- Add the following optional attribute to the AssemblyInfo.vb
file:
<Assembly: Description("Simple Transactional application to show Enterprise Services")>
This attribute provides a description for the COM+ application in the
COM+ Catalog and Component Services Administration console. - Add the following method to the default Class1.vb or VBServCom.vb file if you renamed the file and the class:
' Shows Explicit SetComplete/SetAbort.
Public Function DoTrans() As String
Dim connection As SqlConnection
Dim command As SqlCommand
Dim reader As SqlDataReader
Dim name As String
Dim query As String
Try
query = "SELECT au_lname, au_fname FROM authors"
connection = New SqlConnection("data source=localhost;initial catalog = pubs;UID=sa;PWD=")
command = New SqlCommand(query, connection)
connection.Open()
reader = command.ExecuteReader()
reader.Read()
name = reader.GetString(0) & ", " & reader.GetString(1)
Catch exc As Exception
ContextUtil.SetAbort()
Throw exc
End Try
DoTrans = name
End Function
' Show implicit SetComplete/SetAbort.
<AutoComplete(True)> _
Public Sub DoTxAuto()
'Do stuff
End Sub
- Modify the SqlConnection string for your environment.
Give your assembly a strong name
- Click Start, point to Programs, point to Microsoft Visual Studio .NET, and then
click Visual Studio .NET Tools to open a Visual Studio .NET
command prompt.
- At the command prompt, type the following command:
sn.exe -k ServicedCOM.snk
This gives your assembly a strong name.
For more
information about Regsvcs.exe, see the Microsoft .NET Framework Software
Development Kit (SDK) documentation. - Copy ServicedCOM.snk to the project folder.
- In the AssemblyInfo.vb file, replace the AssemblykeyFile code with the following code:
<Assembly: AssemblyKeyFile("..\\..\\ServicedCOM.snk")>
Add your serviced component to COM+
You register the component dynamically when the first instance is
created. Or, you can manually register the component with Regsvcs.exe. To use
Regsvcs.exe, follow these steps:
- Click Start, point to Programs, point to Microsoft Visual Studio .NET, and then
click Visual Studio .NET Tools to open a .NET command
prompt.
- At a .NET command prompt, type regsvcs
servicedcom.dll.
This creates a COM+ library application
with the same name as your class name.
Test your component
- In Microsoft Notepad, open a text file.
- Copy the following code into the file:
set o =createobject("VBServCOM.VBServCOM")
MsgBox o.DoTrans()
- On the File menu, click Save.
- In the File name box, type Test.vbs. In the Save as type list, click All Files, and then click Save.
- Double-click the file to run the sample.