306296 how to: create a. net service component (from mkba) in Visual C #. net)

Source: Internet
Author: User
Tags net command
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
  1. Create a New Visual C # class library project named servicedcom.
  2. Set the default class and file name fromClass1.csRenameSimpletrans. CS. To do this, follow these steps:
    1. In the "Class View" window, right-clickClass1And then clickAttribute.
    2. InAttribute, SetNameProperty changedSimpletrans.
  3. AddSystem. enterpriseservicesNamespace reference.
  4. Add the following statementSimpletrans. CSAndAssemblyinfo. CSMedium:
    using System.EnterpriseServices;
  5. SlaveServicedcomponentInheritance class (fully qualified name:System. enterpriseservices. servicedcomponent).
  6. Add the following code* Public class *Before:
    [Transaction(TransactionOption.RequiresNew)]
  7. 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.
  8. 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.

  9. 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}
  10. Modify according to your environmentSqlconnectionString.

Back to Top

Provide strong names for the Assembly
  1. ClickStart, PointingProgram, PointingMicrosoft Visual Studio. NETAnd then pointVisual Studio. NET ToolAnd then clickVisual Studio. NET Command Prompt.
  2. 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.

  3. Copy servicedcom. SNK to the project folder.
  4. 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:

  1. ClickStart, PointingProgram, PointingMicrosoft Visual Studio. NETAnd then pointVisual Studio. NET ToolAnd then clickVisual Studio. NET Command Prompt.
  2. 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
  1. Open a text file in notepad.
  2. Paste the following code into the file:
    set o =createobject("ServicedCOM.SimpleTrans")MsgBox o.DoTrans()
  3. InFileClickSave.
  4. InSaveDialog BoxFile NameIn the text box, typeTest. vbs. InSave typeList, clickAll filesAnd then clickSave.
  5. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.