Microsoft KB Archive/309108

= HOW TO: Create a Serviced .NET Component in Visual C++ .NET =

Article ID: 309108

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft Enterprise Services (included with the .NET Framework) 1.0
 * Microsoft Visual C++ .NET 2002 Standard Edition

-



This article was previously published under Q309108



For a Microsoft Visual Basic .NET version of this article, see 312902.

For a Microsoft Visual C# .NET version of this article, see 306296.

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

REFERENCES



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
 Serviced components require strong names. Serviced components should be registered in the Global Assembly Cache (GAC). Server applications require installation in the GAC, but library applications do not (although it is a good idea to install library applications in the GAC).

You must be in the GAC if you are accessing an assembly from managed code and if the assembly is not in the same directory as the executable file. If you want to find an assembly from a managed activation, you must be in the GAC unless it is in your application directory. For unmanaged activations, you can use codebase instead of the GAC. 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.  The sample in this article is intended for illustration purposes only.</li></ul>

back to the top

Create the Serviced .NET Component
<ol> Follow these steps to create a new Managed C++ Class Library project named ServicedCOM in Visual C++ .NET: <ol style="list-style-type: lower-alpha;"> Start Microsoft Visual Studio .NET.</li> On the File menu, point to New, and then click Project.</li> Click Visual C++ Projects under Project Types, and then click Managed C++ Class Library under Templates.</li> In the Name box, type ServicedCOM, and then click OK.</li></ol> </li>  In Solution Explorer, double-click ServicedCOM.h, and then replace the default code with the following code:
 * 1) pragma once


 * 1) using &quot;System.EnterpriseServices.dll&quot;

using namespace System; using namespace System::EnterpriseServices;

namespace ServicedCOM {   public  __gc __interface    IServicedCOM {       void    SelectData; };

[       TransactionAttribute(TransactionOption::RequiresNew) ]   public __gc class ServicedCOMClass : public ServicedComponent, public IServicedCOM {   public: void   SelectData; }; }                   </li>  Add the following code to the top of the AssemblyInfo.cpp file: //Import definitions for Enterprise Services. using namespace System::EnterpriseServices;
 * 1) using &quot;System.EnterpriseServices.dll&quot;

//Enterprise Services does not require this code, but this code is required // if you are using ADO, which this samples uses. using namespace System::Data;
 * 1) using &quot;System.Data.dll&quot;

//This is optional for this example. using namespace System::Data::SqlClient; </li>  Replace the default code in the ServicedCOM.cpp file with the following code:
 * 1) define null 0
 * 2) include <Stdafx.h>
 * 3) include &quot;ServicedCOM.h&quot;

using namespace System; using namespace System::EnterpriseServices; using namespace System::Data;
 * 1) using <mscorlib.dll>
 * 2) using <System.dll>
 * 3) using <System.EnterpriseServices.dll>
 * 4) using <System.Data.dll>
 * 5) using <System.Xml.dll>

//This is optional for this example. using namespace System::Data::SqlClient;

namespace ServicedCOM { void  ServicedCOMClass::SelectData {          SqlConnection *cn = new SqlConnection; DataSet *CustomersDataSet = new DataSet; SqlDataAdapter *da; //Set the connection string of the SqlConnection object. cn->ConnectionString = &quot;Server= ;Trusted_Connection=yes;Database= &quot;;

//Open the ADO.NET connection. cn->Open; //Initialize the SqlDataAdapter object by specifying a Select command //that retrieves data from the sample table. da = new SqlDataAdapter(&quot;select * from Customers&quot;, cn);

//Execute the Fill method of the SqlDataAdapter to populate the DataSet. da->Fill(CustomersDataSet, &quot;Customers&quot;); //Write out the value in the CompanyName field before you update the data by using the DataSet. DataRow *rowCust = CustomersDataSet->Tables->Item[&quot;Customers&quot;]->Rows->Item[0]; //Display the data. Console::WriteLine(&quot;Company Name : {0} &quot;, rowCust->Item[&quot;CompanyName&quot;]);

//Close the database connection. cn->Close; }

}                   </li> Modify the connection string as appropriate for your environment.</li>  Add the following recommended attributes to the AssemblyInfo.cpp file: [assembly: ApplicationActivation(ActivationOption.Library)]; [assembly: ApplicationName(&quot;Simple C++ Serviced Component Example&quot;)]; </li></ol>

back to the top

Give Your Assembly a Strong Name
<ol> Click Start, point to All Programs, point to Microsoft Visual Studio .NET, point to Visual Studio .NET Tools, and then click Visual Studio .NET Command Prompt.</li> At the command prompt, type the following command to give your assembly a strong name:

sn.exe -k ServicedCOM.snk

</li> Copy the ServicedCOM.snk file to the project folder.</li>  In the AssemblyInfo.cpp file, replace the line of code for the AssemblyKeyFileAttribute class with the following code: [assembly:AssemblyKeyFileAttribute(&quot;ServicedCOM.snk&quot;)]; </li> Build your assembly.</li></ol>

back to the top

Add Your Serviced Component to COM+
You can allow the component to register dynamically when the first instance is created, or you can manually register the component with Regsvcs.exe. To use Regsvcs.exe, follow these steps: <ol> <li>Click Start, point to All Programs, point to Microsoft Visual Studio .NET, point to Visual Studio .NET Tools, and then click Visual Studio .NET Command Prompt.</li> <li>Locate the directory that contains the ServicedCOM.dll assembly.</li> <li>At the Visual Studio .NET command prompt, type the following command to create a COM+ library application with the same name as your class name:

regsvcs ServicedCOM.dll

</li></ol>

back to the top

Test Your Component
<ol> <li>In Solution Explorer, right-click your solution, point to Add, and then click New Project.</li> <li>Click Visual Basic Projects under Project Types, and then click Console Application under Templates.</li> <li>In the Name box, type VBClient .</li> <li>On the Project menu, click Add Reference.</li> <li>On the .NET tab, click System.EnterpriseServices, and then click Select. On the Projects tab, click ServicedCOM, and then click Select.</li> <li> In Module1.vb, replace the default code with the following code: Imports System.EnterpriseServices

Module Module1

Sub Main Dim myServicedComponent As New ServicedCOM.ServicedCOMClass Console.WriteLine(&quot;Starting&quot;) myServicedComponent.SelectData Console.WriteLine(&quot;Done&quot;) Console.ReadLine End Sub

End Module </li> <li>In Solution Explorer, right-click the VBClient project, and then click Set as StartUp Project.</li></ol>

back to the top

<div class="references_section">