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

Article translations Article translations
Article ID: 312902 - View products that this article applies to.
This article was previously published under Q312902
This article has been archived. It is offered "as is" and will no longer be updated.
Expand all | Collapse all

On This Page

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.

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.

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.EnterpriseServices
    Imports 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.

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

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.

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.

Properties

Article ID: 312902 - Last Review: February 27, 2014 - Revision: 5.5
APPLIES TO
  • 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
Keywords: 
kbnosurvey kbarchive kbhowtomaster KB312902

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com