The release number of this article has been CHS306296
For Microsoft Visual Basic .NET versions of this article, see
312902.
For Microsoft Visual C .NET versions of this article, see
309108.
This article references the following Microsoft .NET Framework Class Bank Name Space:
System.EnterpriseServices
This task content
summary
Important Description Create .NET Service Components provide a strong name to the assembly to add service components to COM test components.
SUMMARY This step-by-step guide demonstrates how to create a .NET service component that uses transactions. This article will also explain how to create a client for test service components. Microsoft Enterprise Service provides Microsoft COM services for .NET components.
Back to top
important
Service components need a strong name. The service component should be registered in the Global Assembly Cache (GAC) because these components are resource-level resources. The server application requires that the library application is not required (but it is recommended to be installed in the GAC). You can automatically register with the COM registration service component by delaying registration, or can be manually registered with the Regsvcs.exe utility. Regsvcs.exe is located in the following folder: /Winnt/Microsoft.Net/framework/ For more information on Regsvcs.exe, see the Microsoft .NET Framework Software Development Kit (SDK) document. This example assumes that Microsoft SQL Server has been installed on the local computer. This example is provided only for explanation. Strictly said that the selection query of this example is suitable for operation outside the COM transaction because COM uses the highest isolation level for transactions. To increase the throughput of the database, a good programming strategy is to consider a read query at a lower transaction level.
Back to top
Create a .NET service component
Create a new Visual C class library project called Servicedcom. Rename the default class and file name from class1.cs to SimpleTrans.cs. To do this, follow these steps:
In the Class View window, right-click Class1, and then click Properties. In the properties, change the name property to SimpleTrans. Add a reference to a SYSTEM.EnterpriseServices namespace. Add the following statements as the first row to SimpleTrans.cs and AssemblyInfo.cs: Using System.EnterpriseServices; from the ServicesDComponent inherited class (fully qualified name: system.enterprisservices.serventicedComponent). Add the following code to * public class * Before: [Transaction (TransactionOption.RequiresNew] Add the following properties to AssemblyInfo.cs: [Assembly: ApplicationActivation]]
[assmbly: ApplicationName ("SimpleTrans")]
The ActiVationOption property indicates whether the component is activated in the calling process. You can set Activation.Option to libraries or servers. The ApplicationName property is the name of the COM application displayed in the "COM Directory" and "Component Service Management" console. Add the optional properties below to SimpleTrans.cs, place it after the USING statement: [Assembly: Description ("Simple Transactional Application To Show Enterprise Services)] This property is" COM Directory "and" Component Service Management "console The COM application provides instructions. Add the following method to SimpleTrans.cs: // Demos Explicit SetComplete / SetAbortpublic String Dotrans ()
{
SqlConnection Connection;
SQLCommand Command;
SqlDataReader Reader;
String name;
String query;
Try
{
Query = "SELECT AU_LNAME, AU_FNAME FROM AUTHORS";
"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 (Exception EXC)
{
Contextutil.Setabort ();
Throw exc;
}
Return Name;
}
// demo implicit setcomplete / setabort
[AutoComplete]
Public void dotxauto ()
{
// Do Stuff
} Modify the SqlConnection string accordingly according to your environment.
Back to top
Provide a strong name for the assembly
Click Start, point to Programs, point to Microsoft Visual Studio .NET, then point to the Visual Studio .Net tool, and then click the Visual Studio .NET command prompt. At the command prompt, type sn.exe -k servicedcom.snk to create a strong name for the assembly. For information on using strong names, please refer to the .NET Framework Software Development Kit (SDK) document. Copy serviceDcom.snk into the project folder. In AssemblyInfo.cs, replace the AssemblyKeyFile code to the following code: [Assembly: assemblykeyKeyfile ("..//// Servicedcom.snk")]
Back to top
Add service components to COM to make components dynamically register after creating the first instance, or manually register components with Regsvcs.exe. To use regsvcs.exe, follow these steps:
Click Start, point to Programs, point to Microsoft Visual Studio .NET, then point to the Visual Studio .Net tool, and then click the Visual Studio .NET command prompt. Type Regsvcs Servicedcom.dll under the command prompt. This will create a COM library application with the class name. Ignore the warning message. Back to top
Test component
Open a text file in the Notepad. Paste the following code into the file: Set O = CreateObject ("Servicedcom.SIMpleTrans")
Msgbox o.dotrans () On the File menu, click Save. In the File Name text box of Save As dialog, type Test.vbs. In the Save Type list, click All Files, and then click Save. Double-click the file to run an example.
Back to top
The information in this article applies to:
Microsoft Enterprise Services (provided with .NET Framework) Microsoft Visual C # .NET (2002)
Recent Updated: 2002-7-19 (1.0) Keyword Kbhowto KbhowTomaster KB306296