COM+ and the .NET Framework
Building a COM+ Client using C# and .NET

Gopalan Suresh Raj

Note
To work with any of these samples, you will need the following:
.........................................Microsoft .NET SDK
.........................................Microsoft Visual Studio.NET Beta 2 or higher

 

1. Develop the Client.cs Application

Create a regular C# client application. Create the COM+ component when required (as in Line 51) and invoke operations on the component (as in Lines 70 and 81).

Client.cs
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
//////////////////////////////////////////////////////
/// The following example illustrates a Client to a
/// COM+ component developed using C# and .NET.
///
/// author: Gopalan Suresh Raj
/// Copyright (c), 2001. All Rights Reserved.
/// URL: https://gsraj.tripod.com/
/// email: gopalan@gmx.net
///
/// <compile>
/// csc /r:Bank.dll /t:exe /out:Client.exe Client.cs
/// </compile>
/// <run>
/// Client create Checking "Athul Raj" 10000
/// Client delete 1
/// </run>
//////////////////////////////////////////////////////

using System;
// Include the AccountManager COM+ Component namespace
using Bank;
// Include the support class's BookKeeper namespace
using BookKeeper;

namespace AccountManager {
  /// <summary>
  /// Summary description for Client.
  /// </summary>
  public class Client {
    /// <summary>
    /// Entry Point to the Client Application
    /// </summary>
    /// <param name="args"></param>
    /// <returns></returns>
    public static int Main (String[] args) {
      int argLength = args.Length;
      if ( argLength < 2) {
        Console.WriteLine ("Usage: Client <create|delete> <accountNumber|Checking|Savings> <customerName> <startingBalance>");
        return -1;
      }

      string operation = "", type = "";
      string []customerNames = new String [1];
      int accountNumber = 0;
      float startingBalance = 0;

      AccountKey key = new AccountKey ();

      try {
        // Create the COM+ component
        Bank.AccountManager manager = new Bank.AccountManager ();
        Console.WriteLine ("Obtained a reference to the Server Object...");

        operation = args [0];

        if (argLength > 2) {
          type = args [1];
          // This can be a create operation
          if (operation.Trim().ToLower() == "create") {
            if (type.Trim().ToLower() == "checking") {
              key.Type = AccountType.CheckingAccount;      
            }
            if (type.Trim().ToLower() == "savings") {
              key.Type = AccountType.SavingsAccount;
            }
            customerNames[0] = args[2];
            startingBalance = (float)System.Double.Parse (args[3]);
            Console.WriteLine ("Invoking createAccount() now ...");
            // Invoke operations on the COM+ component
            int accountKey = manager.create (key.Type, customerNames, startingBalance);
            Console.WriteLine ("Key of new Row is: {0}", accountKey);
          }
        }
        else {
          // This can be a delete operation
          if (operation.Trim().ToLower() == "delete") {
            accountNumber = System.Int32.Parse (args[1]);
            Console.WriteLine ("Invoking deleteAccount() now ...");
            bool result = false;
            // Invoke operations on the COM+ component
            result = manager.delete (accountNumber);
            if (result == true) {
              Console.WriteLine ("deleteAccount() succeeded...");
            }
            else {
              Console.WriteLine ("deleteAccount() Failed !!!");
            }
          }
        }
      }
      catch (Exception exception) {
        Console.WriteLine (exception.ToString());
      }

      return 0;
    }
  }
}

 

2. Build the Application

Build the files that make up the Client.

Command Prompt
C:\MyProjects\Cornucopia\COMplus\BankServer\AccountManager\bin\Debug>csc /r:Bank.dll /t:exe /out:Client.exe Client.cs
Microsoft (R) Visual C# Compiler Version 7.00.9254 [CLR version v1.0.2914]
Copyright (C) Microsoft Corp 2000-2001. All rights reserved.

warning CS1607: Assembly generation -- Referenced assembly 'Bank' is a localized satellite assembly

C:\MyProjects\Cornucopia\COMplus\BankServer\AccountManager\bin\Debug>

Ignore the warning about the assembly being a satellite assembly.

3. Run the Client

Note:
There is an inherent and 'intentional bug' in the way deletes are handled in the BookKeeper Server. You can only delete records in the order that they were created - i.e., you'd have to delete the latest record first !!! If you do try to delete in any other order, you will not be able to create the next new Account !!! This is because of the way new Primary Keys are generated. For explanation, please look at the
BookKeeper::getNextKey() method. I intentionally introduced this bug in the program to demonstrate that even though ADO.NET's DataSet is physically disconnected from the DataBase, it maintains and manages its data internally,  and  still checks Constraints and behaves very much like a regular database.
 

 

Command Prompt
C:\MyProjects\Cornucopia\COMplus\BankServer\AccountManager\bin\Debug>Client create Checking "Athul Raj" 100000
Obtained a reference to the Server Object...
Invoking createAccount() now ...
Key of new Row is: 10

C:\MyProjects\Cornucopia\COMplus\BankServer\AccountManager\bin\Debug>
Client delete 10
Obtained a reference to the Server Object...
Invoking deleteAccount() now ...
deleteAccount() succeeded...

C:\MyProjects\Cornucopia\COMplus\BankServer\AccountManager\bin\Debug>

 

COM+
Building a complete COM+ Server component using C# and .NET
Building a COM+ Client using C# and .NET

 

Download the entire source code as a zip file.

 

click here to go to
My Advanced C#/.NET Tutorial Page...

About the Author...
Gopalan Suresh Raj is a Software Architect, Developer and an active Author. He has co-authored a number of books including "Professional JMS", "Enterprise Java Computing-Applications and Architecture" and "The Awesome Power of JavaBeans". His expertise spans enterprise component architectures and distributed object computing. Visit him at his Web Cornucopia© site (https://gsraj.tripod.com/) or mail him at gopalan@gmx.net.

 


Go to the Component Engineering Cornucopia page

This site was developed and is maintained by Gopalan Suresh Raj

This page has been visited times since December 31, 2001.

Last Updated : Dec 31, '01

If you have any questions, comments, or problems regarding this site, please write to me I would love to hear from you.


Copyright (c) 1997-2001, Gopalan Suresh Raj - All rights reserved. Terms of use.

All products and companies mentioned at this site are trademarks of their respective owners.