Chapter 4: Creating and Consuming .

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

Understanding .NET Remoting 
To enable communication between different applications reside on the same computer or on different computers.  These computers can be part of the same network or different network.  These computers can run the same or different operating systems.

Slide 2

©

Overview of .NET Remoting 
What does .Net remoting do?
activate objects control the lifetime of objects transport messages by using communication channels 

Communication channel are the objects that transport messages between the remote objects  Messages is encoded and decoded using native .NET serialization formatters (binary and SOAP).  There are two ways to encode all message: binary and XML  To enable perform communication between different objects in different application domains orusing different transportation protocols
Slide 3 ©

Remoting in a Distributed System

Client AppDomain
Client Object Channel Proxy Formatter

Server AppDomain
Formatter Server Object

Channel

Remoting Boundary
Slide 4 ©

processes. and processing time Slide 5 © .Accessing Objects Across Remoting Boundaries  The . memory. which include bandwidth.NET Remoting system allows clients to call methods on objects across remoting boundaries  Remoting boundaries include application domains. and computers  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.  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.

NET Remoting Architecture Remoting system Channel Remoting object Proxy Server object Client object Slide 6 © .The .

Remotable and Nonremotable Objects  What 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  There are two types of remotable objects Marshal-by-value objects Marshal-by-reference objects Slide 7 © .

Implementing Server-Activated and Client-Activated Objects  Understanding Remote Object Activation  Server Activation  Client Activation  Remoting Tasks  Using Lifetime Leases  Initializing Lifetime Leases  Renewing Lifetime Leases Slide 8 © .

Remote Activation Concepts  Marshal By Value Object Ca Cb Cb Client Server Slide 9 © .

Remote Activation Concepts  Marshal By Reference Object Ca Proxy Cb Cb Client Server Slide 10 © .

Understanding Remote Object Activation  When you develop remoting object you need to track: Create object Initiate object Slide 11 © .

Server Activation  Objects are created on the server when you call a method in the server class  There are two ways to create a server-activated object : Singleton SingleCall public class SSNServer { public SSNServer() { RemotingConfiguration.Singleton). "MyUri". } } Re Slide 12 © .RegisterWellKnownServiceType( typeof(testService).ApplicationName = "testService".WellKnownObjectMode. motingConfiguration.

SingleCall Server-Activation Ca Proxy Server a Cb Proxy Server b Slide 13 © .

Singleton Server-Activation Ca Proxy Server Cb Proxy Slide 14 © .

ApplicationName RemotingConfiguration. HttpChannel TcpChannel ChannelServices. such as TCP and HTTP.RegisterChannel RemotingConfiguration.RegisterWellKnownClientType Slide 15 © .Select Channel  What does Channels do? To send messages to an application running in a different application domain Send and receive messages using various protocols.RegisterWellKnownServiceType RemotingConfiguration.

RegisterWellKnownClientType(RemoteTier.ObjectMode) RemotingConfiguration.RegisterWellKnownServiceType(RemoteTier.URL) Slide 16 © .U RI.Select Channel RemotingConfiguration.

ApplicationName = _ ³Option" RemotingConfiguration. WellKnownObjectMode.ClassRemote). _ ³URI".RegisterWellKnownServiceType( _ typeOf(RemoteTier.Start Server TcpServerChannel channel. channel = new TcpServerChannel(port) ChannelServices.RegisterChannel(channel. false) RemotingConfiguration.Singleton) Slide 17 © .

ChannelServices.Start Client TcpClientChannel channel.RegisterWellKnownClientType( _ typeOf(RemoteTier. false) RemotingConfiguration.RegisterChannel(channel.ClassRemote). _ "TCP://IPAddress:8066/URI") Slide 18 © .

Configuration  We¶ve been hard-coding configuration Changes require recompile Dynamic Easy to deploy  Use File Configuration XML Easy to read Easy to modify No need to recompile Slide 19 © .

Server Configuration Remoting Server tcp:8085 http:8086 Singleton ByRef Internal Remoting Configuration Tables Slide 20 © .

Runtime.File Config Server Configuration WellKnown Service System.Remoting Application Channels Channel Slide 21 © .

Config Server  Application Thu c tính: Name  Service  Wellknown Thu c tính: mode="Singleton" objectUri=³URI" type="RemoteTier. port.clsRemote. displayName Slide 22 © . clsRemote"  Channels  Channel Thu c tính: ref.

clsRemote.remoting> </configuration> Slide 23 © . clsRemote"/> </service> <channels> <channel ref="http" port="8081" displayName=³Http Channel " /> </channels> </application> </system.remoting> <application name="RemoteName"> <service> <wellknown mode="Singleton" objectUri=³URI" type="RemoteTier.runtime.Config Server <configuration> <system.runtime.

RemotingConfiguration.Configure(³FileNameServer.config") Slide 24 © .exe.

Client Configuration Remoting Client tcp Internal Remoting Configuration Tables http Slide 25 © .

Runtime.File Config Client Configuration WellKnown Client System.Remoting Application Channels Channel Slide 26 © .

Config Client  Application Thu c tính: Name  Service  Wellknown Thu c tính: mode="Singleton" URL=³URL" type="RemoteTier. clsRemote"  Channels  Channel Thu c tính: ref.clsRemote. port. displayName Slide 27 © .

runtime. clsRemote"/> </client> <channels> <channel ref="http" displayName="HTTP Channel " /> <channel ref="tcp" displayName="TCP Channel " /> </channels> </application> </system.remoting> </configuration> Slide 28 © .remoting> <application> <client> <wellknown mode="Singleton" url="http://localhost:8080/RemoteName/URI" type="RemoteTier.clsRemote.runtime.Config Client <configuration> <system.

config") Slide 29 © .exe.RemotingConfiguration.Configure(³FileNameClient.

Slide 30 © .NET Remoting system deletes an object only when it is marked as ready for garbage collection  Each application domain contains a lease manager that administers the leases in its domain .Using Lifetime Leases  The lifetime of a marshal-by-reference object is the duration for which the object resides in memory  The .

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

Slide 32 © . RemoteType obj = new RemoteType().FromSeconds(30)).GetLifetimeService(obj).Renewing Lifetime Leases  The CurrentLeaseTime property returns the amount of time remaining on the lease. A sponsor renews the lease. TimeSpan expireTime = lease.Renew(TimeSpan. ILease lease = (ILease)RemotingServices.  These are the two ways to renew a lease : A client application calls the ILease.Renew method.

 Scope of Publication : Static members Instance fields and accessors Private members Delegates Overriding methods on an object Slide 33 © .NET Remoting system exposes the functionality of server objects to client applications assuming that the objects are contained locally in the client application.Scope of Publication  The .

Transporting Messages Across Application Domains Using Channels  Understanding Channels  Channel Interfaces  Registering a Channel  Selecting Channels for Remoting  HTTP Channels  TCP Channels Slide 34 © .

such as TCP and HTTP. Slide 35 © .Understanding Channels  What does Channels do? To send messages to an application running in a different application domain Send and receive messages using various protocols.

Channels name-space  List Channel Interface: IChannel IChannelReceiver IChannelSender Properties and Methods of Channel Interface.NET Framework provides the System.Channel Interfaces  The . let you see Page 232 Slide 36 © .Remoting.Runtime.

TcpServerChannel channel = new TcpServerChannel(8010). Slide 37 © . ChannelServices. you use the ChannelServices class. boolean).Registering a Channel  To register channels with the remoting infrastructure.RegisterChannel(channel.

Slide 38 © .Selecting Channels for Remoting  You 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.

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

HttpServerChannel channel = New HttpServerChannel(8080) ChannelServices.RegisterChannel(channel.HTTP Channels (cont.RegisterChannel(channel. HttpChannel channel = New HttpChannel(8010) ChannelServices.false)  How to create and register an HttpChannel object to listen at port 8010.)  How to create and register an HttpServerChannel object to listen at port 8080.false) Constructors of HTTP Channel Classes can see Page 235-237 Slide 40 © .

TCP Channels  TCP Channel is the same HTTP Channel. Slide 41 © . You can see page 238-239.

Implementing Events and Delegates  Understanding Events and Delegates  Implementing Events and Delegates in Remoting Applications Slide 42 © .

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

You register a channel to listen for the callback function.Implementing Events and Delegates in Remoting Applications  When you implement events and delegates in remoting applications. Slide 44 © . 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.

Add the delegate to the event.Implementing Events and Delegates in Remoting Applications (cont)  You 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. Initialize a delegate that wraps the event-handler method. Slide 45 © . Define a method that handles the event.

TypeAssembly" </service> </application> </system.runtime.RemoteAssembly" objectUri = "MyApp.remoting> <application> <service> <wellknown mode = "SingleCall" type = "MyRemoteClass.runtime.NET Remoting Objects <configuration> <system.remoting> </configuration> Slide 46 © .rem" /> /> <activated type="ClientActivatedType.Configuring and Securing .

Sign up to vote on this title
UsefulNot useful