.NET Remoting and Delphi - Implementing the Client
(Page 11 of 11 )
In the client project, the main form's uses clause contains the following namespaces:
uses
System.Drawing, System.Collections, System.ComponentModel,
System.Windows.Forms, System.Data,
// Remoting and Bank related
BankShared,
System.Runtime.Remoting,
System.Runtime.Remoting.Channels,
System.Runtime.Remoting.Channels.HTTP;
The form's OnLoad event handler contains the following code:
fChannel := HttpChannel.Create();
ChannelServices.RegisterChannel(fChannel);
fBankManager := Activator.GetObject(
typeof(IBankManager),
'http://localhost:8099/BankManager.soap');
As you probably guessed, the preceding code creates an instance of the remote object BankManager, which we can now use from inside the client application.
It is now possible to test the remote methods. Figure 29.13 shows the form of the example application that does this at design-time.

Figure 29.13
Main form of Client application.
The Refresh button's Click event handler contains code shown in Listing 29.4.
Listing 29.4 Refresh Button's Click Event Handler
1. procedure TWinForm2.bRefresh_Click(sender: System.Object;
2. e: System.EventArgs);
3. var accountarray : TAccountNumberArray;
4. i : integer;
5. begin
6. accountarray := fBankManager.GetAccountNumbers;
7. cbBankAccounts.Items.Clear;
8. cbOrigin.Items.Clear;
9. cbDestination.Items.Clear;
10.
11. for i := 0 to High(accountarray) do begin
12. cbBankAccounts.Items.Add(accountarray[i].ToString);
13. cbOrigin.Items.Add(accountarray[i].ToString);
14. cbDestination.Items.Add(accountarray[i].ToString);
15. end;
16. cbBankAccounts.SelectedIndex := 0;
17. end;
Find the code on the CD: \Code\Chapter29\Ex01.
As you can see from the first line of this method, the use of the remote object pointed by fBankManager is not any different from using a local copy. We declared a variable of type TAccountNumberArray at line 3, and we use it at line 6.
Keep in mind that the similarities end at the code level, especially when it comes to performance.
When the code
accountarray := fBankManager.GetAccountNumbers;
is executed, the client application is creating a SOAP message, sending it to the server via HTTP and unpacking the response. This overhead is not obvious when making a single method call, but you will immediately see a big difference in performance as soon as you put that code inside a loop.
The Retrieve button's Click event handler contains the code shown in Listing 29.5.
Listing 29.5 Retrieve Button's Click Event Handler
1. procedure TWinForm2.bRetrieve_Click(sender: System.Object;
2. e: System.EventArgs);
3. var acctnumber : integer;
4. account : TAccount;
5. begin
6. acctnumber := System.Convert.ToInt32(cbBankAccounts.Text);
7. account := fBankManager.GetAccount(acctnumber);
8. tbAcctNumber.Text := account.Number.ToString;
9. tbAcctName.Text := account.Name;
10. tbAcctBalance.Text := account.Balance.ToString;
11. end;
Find the code on the CD: \Code\Chapter29\Ex01.
At line 6, we grab the selected account number from the combo box cbBankAccounts and assign it to the integer variable acctnumber. We then pass acctnumber to the BankManager.GetAccount method (line 7), which will generate a remote call to the BankManager object running on the server. Finally (lines 8–10), we display the information contained in the account returned by the server.
Finally, the Transfer button's Click event handler contains
procedure TWinForm2.bTransfer_Click(sender: System.Object;
e: System.EventArgs);
var originacct_acct, destination_acct : integer;
amount : double;
begin
originacct_acct := System.Convert.ToInt32(cbOrigin.Text);
destination_acct := System.Convert.ToInt32(cbDestination.Text);
amount := System.Convert.ToDouble(tbAmount.Text);
fBankManager.TransferMoney(originacct_acct,
destination_acct,
amount);
MessageBox.Show('Transfer completed');
end;
The client, when launched, displays the form shown in Figure 29.14.

Figure 29.14
Client application—remoting in action.
This chapter is from Delphi for .NET Developer's Guide, by Xavier Pacheco (Sams, 2004, ISBN: 0-672-32443-1). Check it out at your favorite bookstore today.
Buy this book now. |
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |