Microsoft KB Archive/312902

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

Article ID: 312902

Article Last Modified on 12/6/2006

-

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

-



This article was previously published under Q312902



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
 * Important notes
 * Create the serviced .NET component
 * Give your assembly a strong name
 * Add your serviced component to COM+
 * Test your component



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\

For more information about Regsvcs.exe, see the Microsoft .NET Framework Software Development Kit (SDK) documentation. </li> Install Microsoft SQL Server on the local computer to use the example in this article.</li> 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.</li></ul>

back to the top

Create the serviced .NET component
<ol> Create a new Visual Basic Class Library project, and name it ServicedCOM .</li> Rename the default class to VBServCom .</li> Add a reference to the System.EnterpriseServices namespace.</li>  Type or paste the following code before the class statement: Imports System.EnterpriseServices Imports System.Data.SqlClient </li>  Add the following code after the class statement: Inherits EnterpriseServices.ServicedComponent </li>  Add the following code before the class statement (do not forget the line continuation character &quot;_&quot;): <Transaction(TransactionOption.RequiresNew)> _ </li>  Add the following attributes to the AssemblyInfo.vb file: <assembly: ApplicationActivation(ActivationOption.Library)> <assembly: ApplicationName(&quot;SimpleTrans&quot;)> 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.</li> The ApplicationName attribute is the name that appears for the COM+ application in the COM+ Catalog and the Component Services Administration console.</li></ul> </li>  Add the following optional attribute to the AssemblyInfo.vb file: <Assembly: Description(&quot;Simple Transactional application to show Enterprise Services&quot;)> This attribute provides a description for the COM+ application in the COM+ Catalog and Component Services Administration console. </li>  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 = &quot;SELECT au_lname, au_fname FROM authors&quot; connection = New SqlConnection(&quot;data source=localhost;initial catalog = pubs;UID=sa;PWD=&quot;) command = New SqlCommand(query, connection)

connection.Open reader = command.ExecuteReader

reader.Read name = reader.GetString(0) & &quot;, &quot; & 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 </li> Modify the SqlConnection string for your environment.</li></ol>

back to the top

Give your assembly a strong name
<ol> 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.</li> <li>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.</li> <li>Copy ServicedCOM.snk to the project folder.</li> <li> In the AssemblyInfo.vb file, replace the AssemblykeyFile code with the following code: <Assembly: AssemblyKeyFile(&quot;..\\..\\ServicedCOM.snk&quot;)> </li></ol>

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
<ol> <li>In Microsoft Notepad, open a text file.</li> <li> Copy the following code into the file: set o =createobject(&quot;VBServCOM.VBServCOM&quot;)

MsgBox o.DoTrans </li> <li>On the File menu, click Save.</li> <li>In the File name box, type Test.vbs. In the Save as type list, click All Files, and then click Save.</li> <li>Double-click the file to run the sample.</li></ol>

back to the top

Keywords: kbhowtomaster KB312902

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.