HOW TO: Create a Serviced .NET Component in Visual C++ .NET (309108)



The information in this article applies to:

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

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

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\Framework Version

      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.
back to the top

Create the Serviced .NET Component

  1. Follow these steps to create a new Managed C++ Class Library project named ServicedCOM in Visual C++ .NET:
    1. Start Microsoft Visual Studio .NET.
    2. On the File menu, point to New, and then click Project.
    3. Click Visual C++ Projects under Project Types, and then click Managed C++ Class Library under Templates.
    4. In the Name box, type ServicedCOM, and then click OK.
  2. In Solution Explorer, double-click ServicedCOM.h, and then replace the default code with the following code:
    #pragma once
    
    #using "System.EnterpriseServices.dll"
    
    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();
    	};
    }
    					
  3. Add the following code to the top of the AssemblyInfo.cpp file:
    //Import definitions for Enterprise Services.
    #using "System.EnterpriseServices.dll"
    using namespace System::EnterpriseServices;
    
    //Enterprise Services does not require this code, but this code is required
    // if you are using ADO, which this samples uses.
    #using "System.Data.dll"
    using namespace System::Data;
    
    //This is optional for this example.
    using namespace System::Data::SqlClient;
    					
  4. Replace the default code in the ServicedCOM.cpp file with the following code:
    #define null 0
    #include <Stdafx.h>
    #include "ServicedCOM.h"
    
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.EnterpriseServices.dll>
    #using <System.Data.dll>
    #using <System.Xml.dll> 
       
    using namespace System;
    using namespace System::EnterpriseServices;
    using namespace System::Data;
    
    //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 =  "Server=<your server>;Trusted_Connection=yes;Database=<your database>";
    
    	  //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("select * from Customers", cn);
    
    	  //Execute the Fill method of the SqlDataAdapter to populate the DataSet.
               da->Fill(CustomersDataSet, "Customers");
     		   
    	  //Write out the value in the CompanyName field before you update the data by using the DataSet.
    	  DataRow *rowCust = CustomersDataSet->Tables->Item["Customers"]->Rows->Item[0];
       		   
    	  //Display the data.
    	  Console::WriteLine("Company Name : {0} ", rowCust->Item["CompanyName"]);
    
               //Close the database connection.
               cn->Close();
    	}
    
    }
    					
  5. Modify the connection string as appropriate for your environment.
  6. Add the following recommended attributes to the AssemblyInfo.cpp file:
    [assembly: ApplicationActivation(ActivationOption.Library)];
    [assembly: ApplicationName("Simple C++ Serviced Component Example")];	
    					
back to the top

Give Your Assembly a Strong Name

  1. 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.
  2. At the command prompt, type the following command to give your assembly a strong name:

    sn.exe -k ServicedCOM.snk

  3. Copy the ServicedCOM.snk file to the project folder.
  4. In the AssemblyInfo.cpp file, replace the line of code for the AssemblyKeyFileAttribute class with the following code:
    [assembly:AssemblyKeyFileAttribute("ServicedCOM.snk")];
    					
  5. Build your assembly.
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:
  1. 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.
  2. Locate the directory that contains the ServicedCOM.dll assembly.
  3. 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

back to the top

Test Your Component

  1. In Solution Explorer, right-click your solution, point to Add, and then click New Project.
  2. Click Visual Basic Projects under Project Types, and then click Console Application under Templates.
  3. In the Name box, type VBClient.
  4. On the Project menu, click Add Reference.
  5. On the .NET tab, click System.EnterpriseServices, and then click Select. On the Projects tab, click ServicedCOM, and then click Select.
  6. 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("Starting")
            myServicedComponent.SelectData()
            Console.WriteLine("Done")
            Console.ReadLine()
        End Sub
    
    End Module
    					
  7. In Solution Explorer, right-click the VBClient project, and then click Set as StartUp Project.
back to the top

REFERENCES

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

316247 HOW TO: Perform a Distributed Transaction with a .NET Provider by Using ServicedComponent in Visual C# .NET

315707 HOW TO: Use COM+ Transactions in a Visual Basic .NET Component

For more information about ADO.NET objects and syntax, visit the following Microsoft Developer Network (MSDN) Library Web site: back to the top

Modification Type:MajorLast Reviewed:9/22/2003
Keywords:kbHOWTOmaster KB309108 kbAudDeveloper