You are on page 1of 46

| 

| | 
 
 

Understanding .NET Remoting
Implementing Server-Activated and Client-Activated
Objects
Transporting Messages Across Application Domains Using
Channels
Configuring and Securing .NET Remoting Objects
3
  

u To enable communication between different applications reside


on the same computer or on different computers.
u These computers can be part of the same network or different
network.
u These computers can run the same or different operating
systems.

x  0
    

u èhat does .Net remoting do?


activate objects
control the lifetime of objects
transport messages by using communication channels
u Communication channel are the objects that transport messages
between the remote objects
u Messages is encoded and decoded using native .NET
serialization formatters (binary and SOAP).
u There are two ways to encode all message: binary and XML
u To enable perform communication between different objects in
different application domains orusing different transportation
protocols
x  0
 
  


| 
 











 
| 




|

|











  
x  0
½


½

 
  


u The .NET Remoting system allows clients to call methods on


objects across remoting boundaries
u Remoting boundaries include application domains, processes,
and computers
u How do you access to object and call method?
Copy of the server object in the client application
The client can call a method on the local copy of the server object.
u Should you copy server object to the client process ?
objects do not contain a large number of methods and are not large
size
Copying an entire server object to the client process is a waste of client
resources, which include bandwidth, memory, and processing time
x  0
  ½  

 
  

 



x      

x  0
    

u èhat are different between Remotable and Noneremotable


object?
Nonremotable objects do not provide the remoting system with a
method to either copy them or use them in another application
domain
Remotable objects can either be accessed outside their application
domain or context using a proxy or copied and passed outside
their application domain or context
u There are two types of remotable objects
Marshal-by-value objects
Marshal-by-reference objects

x 
0
J    ½
 | ½

u Understanding Remote Object Activation


u Server Activation
u Client Activation
u Remoting Tasks
u Using Lifetime Leases
u Initializing Lifetime Leases
u Renewing Lifetime Leases

x  0
 ½ | 

u Marshal By Value Object

|
|
|

|  

x  0
 ½ | 

u Marshal By Reference Object

|
| |



|  

x   0
3
 ½ 

u èhen you develop remoting object you need to track:


Create object
Initiate object

x  0
  ½ 

u Objects are created on the server when you call a method in the server class
u There are two ways to create a server-activated object :
Singleton
SingleCall
public class SSNServer
{
public SSNServer()
{ RemotingConfiguration.ApplicationName = "testService"; Re
motingConfiguration.RegisterèellKnownServiceType(
typeof(testService), "MyUri",èellKnownObjectMode.Singleton);

}
}

x   0
|  ½ 

|  

|
 

x   0
   ½ 

| 



|


x   0
|

u èhat does Channels do?


To send messages to an application running in a different application
domain
Send and receive messages using various protocols, such as TCP and
HTTP.
HttpChannel
TcpChannel
ChannelServices.RegisterChannel
RemotingConfiguration.ApplicationName
RemotingConfiguration.RegisterèellKnownServiceType
RemotingConfiguration.RegisterèellKnownClientType
x   0
|

RemotingConfiguration.RegisterèellKnownServiceType(RemoteTier,U
RI,ObjectMode)
RemotingConfiguration.RegisterèellKnownClientType(RemoteTier,URL)

x  0
  

TcpServerChannel channel;
channel = new TcpServerChannel(port)
ChannelServices.RegisterChannel(channel, false)
RemotingConfiguration.ApplicationName = _
³Option"
RemotingConfiguration.RegisterèellKnownServiceType( _
typeOf(RemoteTier.ClassRemote), _
³URI", èellKnownObjectMode.Singleton)

x 
0
 |

TcpClientChannel channel;
ChannelServices.RegisterChannel(channel, false)
RemotingConfiguration.RegisterèellKnownClientType( _
typeOf(RemoteTier.ClassRemote), _
"TCP://IPAddress:8066/URI")

x  0
|   

u èe¶ve been hard-coding configuration


Changes require recompile
Dynamic
Easy to deploy
u Use File Configuration
XML
Easy to read
Easy to modify
No need to recompile

x  0
  |   


 

  






 
|  
 

x  0
å|  


  




    ½ 

 

 

x  0
|  
u Application
Thu;c tính: Name
u Service
u èellknown
Thu;c tính:
mode="Singleton"
objectUri=³URI"
type="RemoteTier.clsRemote, clsRemote"
u Channels
u Channel
Thu;c tính: ref, port, displayName

x  0
|  
iconfiguration>
isystem.runtime.remoting>
iapplication name="RemoteName">
iservice>
iwellknown mode="Singleton" objectUri=³URI"
type="RemoteTier.clsRemote, clsRemote"/>
i/service>
ichannels>
ichannel ref="http" port="8081"
displayName=³Http Channel " />
i/channels>
i/application>
i/system.runtime.remoting>
i/configuration>

x  0
RemotingConfiguration.Configure(³FileNameServer.exe.config")

x  0
||   

 
| 




 

|  
 

x  0
å| |


  

 

    ½ 

 

 

x  0
| |
u Application
Thu;c tính: Name
u Service
u èellknown
Thu;c tính:
mode="Singleton"
URL=³URL"
type="RemoteTier.clsRemote, clsRemote"
u Channels
u Channel
Thu;c tính: ref, port, displayName

x 
0
| |
iconfiguration>
isystem.runtime.remoting>
iapplication>
iclient>
iwellknown mode="Singleton"
url="http://localhost:8080/RemoteName/URI" type="RemoteTier.clsRemote,
clsRemote"/>
i/client>
ichannels>
ichannel ref="http" displayName="HTTP Channel " />
ichannel ref="tcp" displayName="TCP Channel " />
i/channels>
i/application>
i/system.runtime.remoting>
i/configuration>
x  0
RemotingConfiguration.Configure(³FileNameClient.exe.config")

x  0
3
! !



u The lifetime of a marshal-by-reference object is the duration for


which the object resides in memory
u The .NET Remoting system deletes an object only when it is
marked as ready for garbage collection
u Each application domain contains a lease manager that
administers the leases in its domain .

x  0
J"! !



u To initialize a lifetime lease, you override the InitializeLifetimeService function of the MarshalByRefObject class
using System;
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Lifetime;
public class MyLifetimeControlObject: MarshalByRefObject
{
public override object InitializeLifetimeService()
{
ILease lease = (ILease)base.InitializeLifetimeService();
if (lease.CurrentState == LeaseState.Initial)
{
lease.InitialLeaseTime = TimeSpan.FromMinutes(2);
lease.SponsorshipTimeout = TimeSpan.FromMinutes(3);
lease.RenewOnCallTime = TimeSpan.FromSeconds(3);
}
return lease;
}
}

x  0
! !



u The CurrentLeaseTime property returns the amount of time


remaining on the lease.
u These are the two ways to renew a lease :
A client application calls the ILease.Renew method.
A sponsor renews the lease.
RemoteType obj = new RemoteType();
ILease lease = (ILease)RemotingServices.GetLifetimeService(obj);
TimeSpan expireTime = lease.Renew(TimeSpan.FromSeconds(30));

x  0
  #  

u The .NET Remoting system exposes the functionality of server


objects to client applications assuming that the objects are
contained locally in the client application.
u Scope of Publication :
Static members
Instance fields and accessors
Private members
Delegates
Overriding methods on an object

x  0
 
 $


½

½ 
 
3
|

u Understanding Channels
u Channel Interfaces
u Registering a Channel
u Selecting Channels for Remoting
u HTTP Channels
u TCP Channels

x  0
3
|


u èhat does Channels do?


To send messages to an application running in a different application domain
Send and receive messages using various protocols, such as TCP and HTTP.

x  0
|J 

u The .NET Framework provides the


System.Runtime.Remoting.Channels name-space
u List Channel Interface:
IChannel
IChannelReceiver
IChannelSender
D  
  
     D

x  0

 |

u To register channels with the remoting infrastructure, you use the


ChannelServices class.

— 

    
— 

  
 
  
   !" 

x 
0
|
  

u $ou should apply the following rules when selecting channels for
remote objects:
Register a server channel:TcpServerChannel or HttpServerChannel,
before registering a remote object
Register a server channel on the client computer
Channel names within an application domain cannot be the same
Two channels registered with the remoting system on a computer cannot
listen on the same port.

x  0
A#|


u The System.Runtime.Remoting.Channels.Http namespace


provides channel classes that use the HTTP protocol to transport
messages between remote objects .
u System.Runtime.Remoting.Channels.Http namespace provides
classes, such as HttpClientChannel, HttpServerChannel, and
HttpChannel
u To create an HttpClientChannel object and register it with the
remoting system :
ChannelServices.RegisterChannel(New HttpClientChannel())

x  0
A#|
%  &

u How to create and register an HttpServerChannel object to listen


at port 8080.
HttpServerChannel channel = New HttpServerChannel(8080)
ChannelServices.RegisterChannel(channel,false)
u How to create and register an HttpChannel object to listen at port
8010.
HttpChannel channel = New HttpChannel(8010)
ChannelServices.RegisterChannel(channel,false)


#——        $%&'$%(

x  0
|#|


u TCP Channel is the same HTTP Channel. $ou can see page
238-239.

x  0
J  
 

u Understanding Events and Delegates


u Implementing Events and Delegates in Remoting Applications

x  0
3



u èhat is Delegate?
u A delegate is a class that holds a reference to the method that is called when an event is
fired .
u To declare Delegates:
public delegate void RetirementHandler(object sender, RetireEventArgs e);
u To define define method to which the event delegate points
public class Action
{
public void RetireEvent (object sender, RetireEventArgs e)
{}
}
u To create an instance of the delegate and store the address of the method whose
reference the delegate holds.
Action a = new Action();
NewEventHandler handler = new RetirementHandler(a.RetireEvent);

x  0
J  


   ½ 

u èhen you implement events and delegates in remoting
applications, you should ensure that:
The delegates you pass to the server for callback functions are
delegates to instance members because you cannot call static
members remotely.
$ou register a channel to listen for the callback function.

x  0
J  


   ½ 
% &
u $ou must perform the following tasks to enable delegates to
listen for events in the remoting application:
Define an event and the extension to the EventArgs class that the event
passes to a delegate.
Define a delegate that wraps an event-handler method of the signature
required by the event.
Define a method that handles the event.
Initialize a delegate that wraps the event-handler method.
Add the delegate to the event.

x  0
|      


iconfiguration>
isystem.runtime.remoting>
iapplication>
iservice>
iwellknown mode = "SingleCall" type = "MyRemoteClass,RemoteAssembly"
objectUri = "MyApp.rem" />
iactivated type="ClientActivatedType, TypeAssembly" />
i/service>
i/application>
i/system.runtime.remoting>
i/configuration>

x  0

You might also like