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:
- TransactionattributeApply
Account
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