The release number of this article was chs306296. For the Microsoft Visual Basic. Net version in this article, see 312902.
For the Microsoft Visual C ++. Net version in this article, see 309108.
This document references the following Microsoft. NET Framework class library namespace:
- System. enterpriseservices
Content of this task
- Summary
-
- Important
- Create. net service components
- Provide strong names for the Assembly
- Add service components to COM +
- Test Components
Summary
This step-by-step guide demonstrates how to create. net service components that use transactions. This article also describes how to create a client for testing service components. Microsoft Enterprise Service provides Microsoft COM + services for. NET components.
Back to Top
Important
- The service component must have a strong name.
- Service components should be registered in the Global Assembly Cache (GAC) because these components are system-level resources. The server application must be installed in GAC, but the library application does not (but it is recommended to be installed in GAC ).
- You can automatically register a service component with COM + or manually register a service component with regsvcs.exe by means of delayed registration. 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.
- This example assumes that Microsoft SQL Server is installed on the local computer.
- This example is provided only for illustration. Strictly speaking, the SELECT query in this example is suitable for running outside the COM + transaction, because COM + uses the highest isolation level for transactions. To increase the database throughput, a good programming strategy is to consider reading queries at a lower transaction level.
Back to Top
Create. net service components
- Create a New Visual C # class library project named servicedcom.
- Set the default class and file name fromClass1.csRenameSimpletrans. CS. To do this, follow these steps:
- In the "Class View" window, right-clickClass1And then clickAttribute.
- InAttribute, SetNameProperty changedSimpletrans.
- AddSystem. enterpriseservicesNamespace reference.
- Add the following statementSimpletrans. CSAndAssemblyinfo. CSMedium:
using System.EnterpriseServices;
- SlaveServicedcomponentInheritance class (fully qualified name:System. enterpriseservices. servicedcomponent).
- Add the following code* Public class *Before:
[Transaction(TransactionOption.RequiresNew)]
- Add the following recommended attributesAssemblyinfo. CSMedium:
[assembly:ApplicationActivation(ActivationOption.Library)][assembly:ApplicationName("SimpleTrans")]
- ActivationoptionProperty indicates whether a component is activated in the caller's process. You canActivation. OptionSetLibraryOrServer.
- ApplicationnameAttribute is the name of the COM + application displayed on the "COM + directory" and "Component Service Management" console.
- Add the following optional attributesSimpletrans. CSAfter the using statement:
[assembly:Description("Simple Transactional application to show Enterprise Services")]
This attribute provides instructions for COM + applications in the "COM + directory" and "Component Service Management" console.
- Add the following methodSimpletrans. CS:
// Demos Explicit SetComplete/SetAbortpublic string DoTrans() {SqlConnectionconnection;SqlCommandcommand; SqlDataReaderreader;stringname;stringquery;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 (Exception exc) {ContextUtil.SetAbort();throw exc; }return name; }// Demo implicit SetComplete/SetAbort[AutoComplete]public void DoTxAuto(){// Do stuff}
- Modify according to your environmentSqlconnectionString.
Back to Top
Provide strong names for the Assembly
- ClickStart, PointingProgram, PointingMicrosoft Visual Studio. NETAnd then pointVisual Studio. NET ToolAnd then clickVisual Studio. NET Command Prompt.
- At the command prompt, typeSn.exe-K servicedcom. SNKCreates a strong name for the Assembly.
For information about signing an assembly with strong names, see. NET Framework software development kit (SDK) documentation.
- Copy servicedcom. SNK to the project folder.
- In assemblyinfo. CS, SetAssemblykeyfileReplace the Code with the following code:
[assembly:AssemblyKeyFile("..//..//ServicedCOM.snk")]
Back to Top
Add service components to COM +
You can register the component dynamically after creating the first instance, or manually register the component with regsvcs.exe. To use regsvcs.exe, follow these steps:
- ClickStart, PointingProgram, PointingMicrosoft Visual Studio. NETAnd then pointVisual Studio. NET ToolAnd then clickVisual Studio. NET Command Prompt.
- At the command prompt, typeRegsvcs servicedcom. dll. This will create a COM + Library application with the same name as the class. Ignore warning messages.
Back to Top
Test Components
- Open a text file in notepad.
- Paste the following code into the file:
set o =createobject("ServicedCOM.SimpleTrans")MsgBox o.DoTrans()
- InFileClickSave.
- InSaveDialog BoxFile NameIn the text box, typeTest. vbs. InSave typeList, clickAll filesAnd then clickSave.
- Double-click the file to run the example.
The information in this article is applicable:
- Microsoft Enterprise Services (provided with the. NET Framework)
- Microsoft Visual C #. Net (2002)
Latest updates: |
2002-7-19 (1.0) |
Keywords |
Kbhowto kbhowtomaster kb306296 |