Service component example (zz)

Source: Internet
Author: User

ZZ from

Http://msdn.microsoft.com/library/chs/default.asp? Url =/library/CHS/cpguide/html/cpconservicedcomponentexample. asp

The following example shows a service component that consists of a client and a server. In this example, the account class is derived from the servicedcomponent class, which ensures that the account object context is carried in COM +. The following attributes are applied in this example:

  • TransactionattributeApplyAccount Class to set the transactionRequiredThis operation is equivalent to using the "component service" management tool in the Windows "Control Panel" to set transaction support for the COM + component.
  • AutocompleteattributeApplyPostMethod. If an unprocessed exception is generated during execution of this method, this attribute indicates that the runtime automatically calls the transactionSetabortFunction; otherwise, the Runtime Library callsSetcompleteFunction.

In addition to the attributes used in this example, various Assembly-level attributes are also used to provide COM + registration information. For manual registration, the service component must have a strong name and should be placed in the Global Assembly Cache (GAC. For more information about the Assembly, see Assembly with a strong name.

Note:The Assembly placed in GAC cannot use dynamic registration. If a server application is created, you must use the Windows installer to add the Assembly and all its dependent assembly to the Global Assembly Cache (GAC) before the application can be used. Otherwise, the application generates an exception.

Bankcomponent Server

[Visual Basic]Imports System.EnterpriseServicesImports System.Runtime.CompilerServicesImports System.Reflection' Supply the COM+ application name. <assembly: ApplicationName("BankComponent")>' Supply a strong-named assembly.<assembly: AssemblyKeyFileAttribute("BankComponent.snk")>Namespace BankComponent      <Transaction(TransactionOption.Required)> _      Public Class Account Inherits ServicedComponent            <AutoComplete()> _            Public Sub  Post(accountNum As Integer, amount As Double)                  ' Updates the database; no need to call SetComplete.                  ' Calls SetComplete automatically if no exception is generated.            End Sub       End Class End Namespace [C#]using System.EnterpriseServices;using System.Runtime.CompilerServices;using System.Reflection;// Supply the COM+ application name.[assembly: ApplicationName("BankComponent")]// Supply a strong-named assembly.[assembly: AssemblyKeyFileAttribute("BankComponent.snk")]namespace BankComponent{      [Transaction(TransactionOption.Required)]      public class Account : ServicedComponent      {            [AutoComplete]             public bool Post(int accountNum, double amount)            {                /* Updates the database; no need to call SetComplete.                   Calls SetComplete automatically if no exception is                   generated. */            return false;                 }       }}

Bankcomponent Client

[Visual Basic]Imports BankComponentPublic Class Client    Shared Sub Main()      Dim Account As New Account()      ' Post money into the account.       Account.Post(5, 100)   End SubEnd Class[C#]using BankComponent;namespace BankComponentConsoleClient{      class Client      {            public static int Main()             {                  try                  {                        Account act = new Account();                        // Post money into the account.                        act.Post(5, 100);                        return(0);                  }                  catch                  {                        return(1);                  }            }      }}

Makefile. bat

You can compile the server and client as follows:

[Visual Basic]sn –k BankComponent.snkvbc /t:library /r:System.EnterpriseServices.dll Bank.vbvbc /r:Bank.dll /r:System.EnterpriseServices.dll BankClient.vb[C#]sn –k BankComponent.snkcsc /t:library /r:System.EnterpriseServices.dll Bank.cscsc /r:Bank.dll BankClient.cs

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.