Share via


Serviced Component Example

The following example is a serviced component that has two parts, a client and a server. In the example, the Account class derives from the ServicedComponent class, which ensures that the contexts of Account objects are hosted in COM+. The following attributes are applied in this example:

  • TransactionAttribute   Applied to the Account class to set the transaction to Required, which is equivalent to using the Component Services administrative tool (available from the Windows Control Panel) to set the transaction support on a COM+ component.
  • AutoCompleteAttribute   Applied to the Post method. This attribute instructs the runtime to automatically call the SetAbort function on the transaction if an unhandled exception is generated during the execution of the method; otherwise, the runtime calls the SetComplete function.

Besides the attributes used in this example, various assembly-level attributes are also used to supply COM+ registration information. A serviced component must be strong-named and should be placed in the global assembly cache (GAC) for manual registration. For additional information about assemblies, see Strong-Named Assemblies.

Note   Assemblies that are placed in the GAC cannot use dynamic registration. If you create a server application, the assembly and any assemblies on which it depends must be added to the GAC by using Windows Installer before the server application can be used; otherwise, the application generates an exception.

BankComponent Server

Imports System.EnterpriseServices
Imports System.Runtime.CompilerServices
Imports 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

Imports BankComponent
Public Class Client 
   Shared Sub Main()
      Dim Account As New Account()
      ' Post money into the account. 
      Account.Post(5, 100)
   End Sub
End 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:

sn –k BankComponent.snk
vbc /t:library /r:System.EnterpriseServices.dll Bank.vb
vbc /r:Bank.dll /r:System.EnterpriseServices.dll BankClient.vb
[C#]
sn –k BankComponent.snk
csc /t:library /r:System.EnterpriseServices.dll Bank.cs
csc /r:Bank.dll BankClient.cs

See Also

Writing Serviced Components | Serviced Component Overview | Applying Attributes to Configure Services | Registering Serviced Components | Summary of Available COM+ Services | ServicedComponent | System.EnterpriseServices Namespace