Export (0) Print
Expand All

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.

NoteNote

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

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

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
csc /t:library /r:System.EnterpriseServices.dll Bank.cs
csc /r:Bank.dll BankClient.cs

See Also

Community Additions

ADD
Show:
© 2014 Microsoft