.NET
  Home arrow .NET arrow Page 10 - .NET Remoting and Delphi
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

.NET Remoting and Delphi
By: Xavier Pacheco
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 14
    2004-08-09

    Table of Contents:
  • .NET Remoting and Delphi
  • Distributed Architectures
  • Benefits of Multitier Application Development
  • .NET Remoting Basics
  • The ChannelServices Class
  • Object Activation
  • Your First .NET Remoting Application
  • Adding References
  • BankPackage.dll: Contract Between Clients and Servers
  • Implementing the Server
  • Implementing the Client

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    .NET Remoting and Delphi - Implementing the Server


    (Page 10 of 11 )

    The server is composed of two parts:

    • The project source file in which we will open an HTTP remoting channel and register the class that implements the IBankManager interface.

    • A unit that contains the class TBankManager. This class implements the IBankManager interface.

    We've added a new unit to the server project and saved it as BankServer_Impl.pas. This unit is shown in Listing 29.2.

    Listing 29.2 The BankServer_Impl.pas Unit

    1:  unit BankServer_Impl;
    2: 
    3:  interface
    4: 
    5:  uses
    6:   BankShared;
    7: 
    8:  type
    9:   TBankManager = class(MarshalByRefObject, IBankManager)
    10:  private
    11:   fAccount1,
    12:   fAccount2 : TAccount;
    13:  protected
    14:   // IBankManager
    15:   function GetAccountNumbers : TAccountNumberArray;
    16:   function GetAccount(const AccountNumber : integer) : TAccount;
    17:   procedure TransferMoney(const Origin, Destination : integer;
    18:               const Amount : double);
    19:  public
    20:   constructor Create;
    21:  end;
    22:
    23: implementation
    24:
    25: { TBankManager }
    26:
    27: constructor TBankManager.Create;
    28: begin
    29:  inherited Create;
    30:  fAccount1 := TAccount.Create(1, 'John Smith', 1999);
    31:  fAccount2 := TAccount.Create(2, 'Jack Rockwell', 249);
    32: end;
    33:
    34: function TBankManager.GetAccount(const AccountNumber: integer): TAccount;
    35: begin
    36:  case AccountNumber of
    37:   1 : result := fAccount1;
    38:   2 : result := fAccount2;
    39:   else raise Exception.Create('Invalid account number!');
    40:  end;
    41:
    42:  Console.WriteLine('A client requested account '+result.Number.ToString+
    43:       ' ('+result.Name+')');
    44: end;
    45:
    46: function TBankManager.GetAccountNumbers: TAccountNumberArray;
    47: begin
    48:  SetLength(result, 2);
    49:  result[0] := fAccount1.Number;
    50:  result[1] := fAccount2.Number;
    51:
    52:  Console.WriteLine('A client requested the list of accounts');
    53: end;
    54:
    55: procedure TBankManager.TransferMoney(const Origin, Destination: integer;
    56:  const Amount: double);
    57: var origin_acct, destination_acct : TAccount;
    58: begin
    59:  origin_acct := GetAccount(Origin);
    60:  destination_acct := GetAccount(Destination);
    61:
    62:  if (origin_acct.Balance<Amount) or (Amount<0)
    63:   then raise Exception.Create('Insufficient funds or invalid amount specified');
    64:
    65:  destination_acct.Balance := destination_acct.Balance+Amount;
    66:  origin_acct.Balance := origin_acct.Balance-Amount;
    67:
    68:  Console.WriteLine('Transferred ${0} from account {1} to account {2}',
    69:   Amount.ToString, origin_acct.Number.ToString,
    70:   destination_acct.Number.ToString);
    71: end;
    72:
    73: end.


    Find the code on the CD: \Code\Chapter 29\Ex01.


    Pay attention to the declaration of TBankManager at line 9. MarshalByRefObject is the base class for objects that communicate across application domain boundaries by exchanging messages using a proxy.

    The proxy is created the first time the object is accessed. Subsequent calls on the proxy are marshaled back to the object residing in the server application domain.

    Classes must inherit from MarshalByRefObject when their instances need to be used across application domains, and their state doesn't need to be or cannot be copied.

    In the project source file (see Listing 29.3 later), we've added the namespaces System.Runtime.Remoting, System.Runtime.Remoting.Channels, and System.Runtime.Remoting.Channels.HTTP. The project source file now contains

    uses
    BankServer_Impl in 'BankServer_Impl.pas',
    System.Runtime.Remoting,
    System.Runtime.Remoting.Channels,
    System.Runtime.Remoting.Channels.HTTP;

    We've also defined a variable of type HTTPChannel called Channel and initialized it as follows:

    var def_HTTPPort : integer = 8099;
      Channel : HttpChannel;
    Begin
    [..]
    Channel := HttpChannel.Create(def_HTTPPort);
    ChannelServices.RegisterChannel(Channel);

    Finally, we registered the type TBankManager by using the RemotingConfiguration class:

    RemotingConfiguration.RegisterWellKnownServiceType(
    typeOf(TBankManager),
    'BankManager.soap',
    WellKnownObjectMode.Singleton);
    // Starts accepting requests
    Writeln('Waiting for requests...');
    Readln;

    The complete source for the BankServer.dpr file is shown in Listing 29.3.

    Listing 29.3 BankServer.dpr File

    1:  program BankServer;
    2: 
    3:  {$APPTYPE CONSOLE}
    4: 
    5:  {%DelphiDotNetAssemblyCompiler 'BankPackage.dll'}
    6:  {%DelphiDotNetAssemblyCompiler [..]}
    7:  {%DelphiDotNetAssemblyCompiler [..]}
    8:
    9: uses
    10:  BankServer_Impl in 'BankServer_Impl.pas',
    11:  System.Runtime.Remoting,
    12:  System.Runtime.Remoting.Channels,
    13:  System.Runtime.Remoting.Channels.HTTP;
    14:
    15: var def_HTTPPort : integer = 8099;
    16:   Channel : HttpChannel;
    17: begin
    18:  Console.WriteLine('Initializing server...');
    19:
    20:  // Initializes the server to listen for HTTP requests on a specific port
    21:  Channel := HttpChannel.Create(def_HTTPPort);
    22:  ChannelServices.RegisterChannel(Channel);
    23:  Console.WriteLine('HTTP channel created. Listening on port '+
    24:      System.Convert.ToString(def_HTTPPort));
    25:
    26:  // Registers the TBankManager service
    27:  RemotingConfiguration.RegisterWellKnownServiceType(
    28:   typeOf(TBankManager),
    29:   'BankManager.soap',
    30:   WellKnownObjectMode.Singleton);
    31:
    32:  // Starts accepting requests
    33:  Console.WriteLine('Waiting for requests...');
    34:  Console.ReadLine;
    35: end.

     

    Find the code on the CD: \Code\Chapter 29\Ex01.


    Lines 21–22 show how to make the server application create a remoting channel that listens for HTTP messages on port 8099. Lines 27–30 register the object TBankManager as a singleton ready to be remotely accessed.

    Notice how you do not need to associate a class to a channel. If you have other remote classes, you would just need to register them as we did with TBankManager (lines 27–30). The HTTP channel that was previously created listens for any type of remoting message coming from clients and forwards it to the correct target object.

    At this point, you can compile the server application and launch it outside the IDE. It will display the contents as shown in Figure 29.12.

    delphi

    Figure 29.12
    Server application output.

    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.

    More .NET Articles
    More By Xavier Pacheco


       · in time compiling is message:Incompatible types 'IBankManager' and 'Object'where...
       · is in WinformfBankManager := Activator.GetObject( typeof(IBankManager), ...
     

    .NET ARTICLES

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT