How to create a serviced .NET component in Visual Basic.NET or Visual Basic 2005

This article was previously published under Q312902
This article has been archived. It is offered "as is" and will no longer be updated.
For a Microsoft Visual C# .NET version of this article, see 306296.
For a Microsoft Visual C++ .NET version of this article, see 309108.
For a Microsoft Visual Basic 6.0 version of this article, see 250292.

IN THIS TASK

SUMMARY
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.

back to the top

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+:
    • Use lazy registration to register serviced components automatically.
    • Use the Regsvcs.exe utility to register serviced components manually. Regsvcs.exe is located in the following folder:
      \WINNT\Microsoft.NET\Framework\Framework Version
      For more information about Regsvcs.exe, see the Microsoft .NET Framework Software Development Kit (SDK) documentation.
  • 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.
back to the top

Create the serviced .NET component

  1. Create a new Visual Basic Class Library project, and name it ServicedCOM.
  2. Rename the default class to VBServCom.
  3. Add a reference to the System.EnterpriseServices namespace.
  4. Type or paste the following code before the class statement:
    Imports System.EnterpriseServicesImports System.Data.SqlClient					
  5. Add the following code after the class statement:
        Inherits EnterpriseServices.ServicedComponent					
  6. Add the following code before the class statement (do not forget the line continuation character "_"):
    <Transaction(TransactionOption.RequiresNew)> _					
  7. 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.
  8. 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.
  9. 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					
  10. Modify the SqlConnection string for your environment.
back to the top

Give your assembly a strong name

  1. 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.
  2. 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.
  3. Copy ServicedCOM.snk to the project folder.
  4. In the AssemblyInfo.vb file, replace the AssemblykeyFile code with the following code:
    <Assembly: AssemblyKeyFile("..\\..\\ServicedCOM.snk")>					
back to the top

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:
  1. Click Start, point to Programs, point to Microsoft Visual Studio .NET, and then click Visual Studio .NET Tools to open a .NET command prompt.
  2. At a .NET command prompt, type regsvcs servicedcom.dll.

    This creates a COM+ library application with the same name as your class name.
back to the top

Test your component

  1. In Microsoft Notepad, open a text file.
  2. Copy the following code into the file:
    set o =createobject("VBServCOM.VBServCOM")MsgBox o.DoTrans()					
  3. On the File menu, click Save.
  4. In the File name box, type Test.vbs. In the Save as type list, click All Files, and then click Save.
  5. Double-click the file to run the sample.
back to the top
Properties

Article ID: 312902 - Last Review: 02/27/2014 21:10:49 - Revision: 5.5

  • Microsoft Enterprise Services (included with the .NET Framework) 1.0
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic 2005
  • kbnosurvey kbarchive kbhowtomaster KB312902
Feedback