This document discusses .NET remoting and serialization. It begins by introducing application domains and distributed applications in .NET. It then covers the key concepts and components of .NET remoting including remoting namespaces, remotable objects, channels, formatters, and object lifetime management using leases. The document also compares .NET remoting to web services and discusses object marshalling and serialization.
3. An Application domain, or AppDomain, is the smallest execution unit for a .NET
application.
The CLR allows several application domains to run within a single Windows process.
Distributed Applications enable communication between objects that run in different
application domains and processes.
System.Net, System.Runtime.Remoting and System.Web.Services namespaces
enable the .NET Framework to support distributed application development.
4. .NET Remoting
The System.Runtime.Remoting Namespace —This namespace includes the classes
that constitutes the .NET remoting framework.
The .NET remoting framework enables communication between objects living in
different Appdomains whether or not they are on the same computer.
Remoting provides an abstraction over the complexities of network programming and
exposes a simple mechanism for inter application domain communication.
The key objectives of .NET remoting are flexibility and extensibility.
6. .NET Remoting layer revolves around a careful orchestration that takes place between
four key players:
• Proxies
• Messages
• Channels
• Formatters
Key Players
7. .NET Remoting Components
A remotable object: Which is an object that contain some properties and methods
located in one application domain and you need to call its methods or properties from
another application domain or process.
A host application (server application): The main task of this host is to listen to
requests for the hosted remotable object.
A client application: This is the application which makes requests for the remotable
object.
8. Types of Remotable Objects
There are 3 types of remotable objects that you can configure and choose from
depending on the requirements of your application.
Single Call: Service one and only one request coming in
Singleton Call: Service multiple clients and is useful when data needs to be shared
explicilty between several clients.
Client Activation: Richer than singleton in many aspects as they can store the state
information between method calls for its specific client.
9. .NET Remoting Architecture
1. When a client object wants to create an
instance of the server object, the remoting
system at the client side instead creates a
proxy of the server object.
2. When the client object calls a method on the
server object, the proxy passes the call
information to the remoting system on the
client. This remoting system in turn sends the
call over the channel to the remoting system
on the server.
10. Cont.
3. The remoting system on the server receives the call
information and, on the basis of it, invokes the method
on the actual object on the server (creating the object if
necessary).
4. The remoting system on the server collects all the
results of the method invocation and passes them
through the channel to the remoting system on the
client.
5. The remoting system at the client receives the
response of the server and returns the results to the
client object through the proxy.
11. .Net Remoting Vs Web Services
XML Web Services:
• Simple, loosely coupled programming model.
• Can be accessed only over HTTP and work in a stateless environment.
• Services hosted in IIS.
.Net Remoting:
• Tightly coupled and more complex programming model.
• Can be accessed over any protocol and support both stateful and stateless
environments.
• Services hosted in any AppDomain.
13. Marshalling or Marshaling is the process of transforming the memory representation
of an objet to a data format suitable for storage or transmission.
Used when data must be moved between different parts of a computer program or from
one program to another.
Marshalling is similar to serialization and is used to communicate to remote objects with
an object, in this case a serialized object.
14. Cont.
It simplifies complex communication, using custom/complex objects to communicate
instead of primitives.
Marshalling is the process of packaging and unpackaging parameters so a remote
procedure call can take place.
Remotable objects are objects that can be marshalled across the application domains.
In contrast, all other objects are known as nonremotable objects.
15. Usage
Used within implementation of different remote procedure call mechanisms, where it is
necessary to transport data between processes and/or between threads.
In Microsoft's Component Object Model interface pointers must be marshalled when
crossing Component Object Model apartment boundaries.
16. Example
Marshaling an integer parameter involves simply copying the value into the message
buffer.
Marshaling an array is a more complex process. Array members are copied in a specific
order so that the other side can reconstruct the array exactly. When a pointer is
marshaled, the data that the pointer is pointing to is copied following rules and
conventions for dealing with nested pointers in structures. Unique functions exist to
handle the marshaling of each parameter type.
17.
18. Types of Remotable Objects
Marshal-by-value (MBV) Objects — These objects are copied and passed out of the
server application domain to the client application domain.
Marshal-by-reference (MBR) Objects — These objects are accessed on the client
side using a proxy. The client just holds a reference to these objects.
19. Marshal-by-value (MBV) Objects
MBV objects reside on the server.
When the client invokes a method on the MBV object, the MBV object is serialized,
transferred over the network, and restored on the client as an exact copy of the server-side
object. The method is then invoked directly on the client. When this happens, the MBV object
is no longer a remote object. Any method calls to the object do not require any proxy object
or marshalling because the object is locally available.
The MBV objects can provide faster performance by reducing the number of network
roundtrips, but in the case of large objects, the time taken to transfer the serialized object
from the server to the client can be very significant.
MBV objects do not allow you the flexibility to run the remote object in the server
environment.
A MBV object can be created by declaring a class with the Serializable attribute.
20. Marshal-by-reference (MBR) Objects
They always reside on the server.
All methods invoked on these objects are executed at the server side.
The client communicates with the MBR object on the server using a local proxy object
that holds the reference to the MBR object.
Although the use of MBR objects increases the number of network roundtrips, they are
a good choice when the objects are prohibitively large or when the functionality of the
object is only available in the server environment on which it is created.
An MBR object can be created by deriving from the System.MarshalByRefObject
class.
22. Channel is an object that transports messages across remoting boundaries
Channels are responsible for passing messages across remoting boundaries.
Channels must be registered before `objects are created as per application domain.
A channel has two endpoints.
The channel object at the receiving end of a channel (the server) listens to a particular
protocol using the specified port number, whereas the channel object at the sending end
of the channel (the client) sends information to the receiving end using the protocol and
port number specified by the channel object on the receiving end.
23. The .NET Framework provides implementations for two channels.
HTTP (Hypertext Transfer Protocol)
TCP (Transmission Control Protocol) channels.
If you want to use a different protocol, you can define your own channel by
implementing the IChannelReceiver and IChannelSender interfaces.
24. HTTP Channels
HTTP channels use HTTP for establishing communication betweenthe two ends.
These channels are implemented through the classes of the
System.Runtime.Remoting.Channels.Http namespace.
The ChannelServices class is used to register a sender-receiver HTTP channel.
One of its static methods used there is RegisterChannel(), which helps in registering a
channel with the remoting framework.
25. The HTTP Channel Classes
CLASS INTERFACE PURPOSE
HttpServerChannel IChannelReceiver An implementation for a server channel
that uses the HTTP protocol to receive
messages
HttpClientChannel IChannelSender An implementation for a client channel
that uses the HTTP protocol to send
messages
HttpChannel IChannelReceiver
IChannelSender
An implementation of a combined and
channel that provides the functionality of
both the HttpServerChannel and the
HttpClientChannel classes
26. TCP Channels
A TCP channel uses TCP for establishing communication between the two ends.
The TCP channel is implemented through various classes of the
System.Runtime.Remoting.Channels.Tcp namespace.
27. The TCP Channel Classes
CLASS INTERFACE PURPOSE
TcpServerChannel IChannelReceiver An implementation for a server channel
that uses the TCP protocol to receive
messages
TcpClientChannel IChannelSender An implementation for a client channel
that uses the TCP protocol to send
messages
TcpChannel IChannelReceiver An implementation of a combined and
channel that provides the
IChannelSender functionality for both
the TcpServerChannel and
TcpClientChannel classes
28. Choosing Between HTTP Channel
and TCP Channels
Channel Scope Efficiency Security
HttpChannel Wide Less More
TcpChannel Narrow More Less
30. The objects used to encode and serialize data into messages.
A formatter class must implement the IFormatter interface.
The .NET Framework packages two native formatter classes;
1. BinaryFormatter class
2. SoapFormatter class
31. The SOAP Formatter
SOAP - Simple Object Access Protocol
Straightforward, XML-based protocol for exchanging types and messages between
applications
An extensible and modular protocol
Implemented in the SoapFormatter class of the
System.Runtime.Serialization.Formatters.Soap namespace
32. The Binary Formatter
Can be understood only within .NET applications
Compact and efficient
Implemented in the BinaryFormatter class of the
System.Runtime.Serialization.Formatters.Binary namespace
33. Channels and Formatters
HTTP channel uses the SOAP formatter.
HTTP channel uses;
- SoapClientFormatterSinkProvider class
- SoapServerFormatterSinkProvider class
to serialize and deserialize messages
Can create industry-standard XML Web services
34. Cont.
TCP channel uses the binary formatter.
TCP channel uses;
- BinaryClientFormatterSinkProvider class
- BinaryServerFormatterSinkProvider class
to serialize and deserialize messages.
37. Remote Object Activation
Only MBR objects can be activated remotely.
No remote activation is needed for of MBV objects because
-the MBV object itself is transferred to the client side.
Based on the activation mode, an MBR object is classified into one of the following two
categories:
Server-activated objects
Client-activated objects
39. 1. Server-Activated Objects
Server-activated objects (SAO) are those remote objects whose lifetime is directly
controlled by the server.
When a client requests an instance of a server-activated object, a proxy to the remote
object is created in the client’s application domain. The remote object is only instantiated (or
activated) on the server when the client calls a method on the proxy object.
Limited flexibility.
Two activation modes for a server-activated object:
1.SingleCall activation mode 2.Singleton activation mode
40. SingleCall Activation Mode
An object is instantiated for responding to just one client request.
After the request is fulfilled, the .NET remoting framework deletes the object and
reclaims its memory.
Also known as stateless because the objects are created and destroyed with each
client request; therefore, they do not maintain state across requests.
Allows for greater server scalability as an object consumes server resources only for a
small period, therefore allowing the server to allocate resources to other objects.
41.
42. Examples of the SingleCall activation mode
Applications in which the object is required by the client to do a small amount of work and
then the object is no longer required.
retrieving the inventory level for an item
displaying tracking information for a shipment
43. Singleton Activation Mode
There is one instance of the remote object regardless of the number of clients accessing it.
Maintain state information across method calls.
Such objects are also sometimes known as Stateful objects.
Object is globally shared by all of its clients.
A Singleton object does not exist on the server forever.
Its lifetime is determined by the lifetime lease of the object
44.
45. Examples of the Singleton activation mode
Applications in which multiple clients talk to the same remote object and share data between
one another through this object.
Ex: Chat server
46. 2. Client-Activated Objects
Lifetime is directly controlled by the client.
Client, has the complete control over the lifetime of the objects.
Client-activated objects are instantiated on the server as soon as the client requests the
object to be created.
A CAO can be created using any of the available constructors for the class.
49. Lifetime Leases
A lifetime lease is the period of time that a particular object can be active in memory before
the .NET framework deletes it and reclaims its memory.
Both Singleton SAO and CAO use lifetime leases to determine how long they should
continue to exist.
A lifetime lease is represented using an object that implements the ILease interface defined
in the System.Runtime.Remoting.Lifetime namespace.
50. Important Members of the ILEASE Interface
Member Name Type Description
CurrentLeaseTime Property Returns the amount of time remaining on the
lease as a TimeSpan object.
InitialLeaseTime Property Indicates the default lifetime of the object. If
the object does not receive any method calls,
it will only live for this period.
Register() Method Specifies an object to be notified when a lease
needs to be renewed.
Renew() Method Renews a lease for the specified TimeSpan
RenewOnCallTime Property Each time the remote object is called, the
lease is renewed for this much time.
SponsorshipTimeout Property Specifies the amount of time to wait for a
sponsor object to respond to a renewal
request.
51. How leases work?
When an object is created, its lifetime lease (CurrentLeaseTime) is set using the value
of the InitialLeaseTime property.(which is 5 minutes by default).
Whenever the object receives a call, its CurrentLeaseTime is reset to the time
specified by the value of the RenewOnCallTime property. (which is 2 minutes by
default).
The client can also renew a lease for a remote object by directly calling the
ILease.Renew() method
52. When the value of CurrentLeaseTime reaches 0, the .NET Framework contacts any
sponsors registered with the lease to check if they are ready to sponsor renewing the
object’s lease.
If the sponsor does not renew the object or the server cannot contact the sponsor within the
duration specified by the SponsorshipTimeout property, the object is marked for garbage
collection.
54. Serialization is the process of converting an object into a stream of bytes in order to
store the object or transmit it to memory, a database, or a file.
Its main purpose is to save the state of an object in order to be able to recreate it when
needed.
The reverse process is called Deserialization.
55. How Serialization Works
The object is serialized to a stream, which carries not
just the data, but information about the object's type,
such as its version, culture, and assembly name.
From that stream, it can be stored in a database, a
file, or memory.
56. Uses of Serialization
Allows the developer to save the state of an object and recreate it as needed, providing
storage of objects as well as data exchange.
Sending the object to a remote application by means of a Web Service.
Passing an object from one domain to another.
Passing an object through a firewall as an XML string.
Maintaining security or user-specific information across applications.
57. Making an Object Serializable
To serialize an object, you need the object to be serialized, a stream to contain the serialized
object, and a Formatter.System.Runtime.Serialization contains the classes necessary for
serializing and deserializing objects.
Apply the SerializableAttribute attribute to a type to indicate that instances of this type can be
serialized. A SerializationException exception is thrown if you attempt to serialize but the type
does not have the SerializableAttribute attribute.
58. If you do not want a field within your class to be serializable, apply
the NonSerializedAttribute attribute.
If a field of a serializable type contains a pointer, a handle, or some other data structure
that is specific to a particular environment, and the field cannot be meaningfully
reconstituted in a different environment, then you may want to make it nonserializable.
If a serialized class contains references to objects of other classes that are
marked SerializableAttribute, those objects will also be serialized.
59. Binary and XML Serialization
Binary Serialization
Uses binary encoding to produce compact serialization for uses such as storage or socket-
based network streams.
All members, even those that are read-only, are serialized, and performance is enhanced.
XML serialization provides more readable code, as well as greater flexibility of object sharing
and usage for interoperability purposes.
60. XML Serialization
XML serialization serializes the public fields and properties of an object, or the
parameters and return values of methods, into an XML stream that conforms to a
specific XML Schema definition language (XSD) document.
XML serialization results in strongly typed classes with public properties and fields that
are converted to XML. System.Xml.Serialization contains the classes necessary for
serializing and deserializing XML.
You can apply attributes to classes and class members in order to control the way
the XmlSerializer serializes or deserializes an instance of the class.
61. Basic and Custom Serialization
Basic Serialization
Basic serialization uses the .NET Framework to automatically serialize the object.
The only requirement in basic serialization is that the object has
the SerializableAttribute attribute applied. The NonSerializedAttribute can be used to
keep specific fields from being serialized.
When you use basic serialization, the versioning of objects may create problems, in which
case custom serialization may be preferable. Basic serialization is the easiest way to perform
serialization, but it does not provide much control over the process.
62. Custom Serialization
In custom serialization, you can specify exactly which objects will be serialized and how
it will be done. The class must be marked SerializableAttribute and implement
the ISerializable interface.
If you want your object to be deserialized in a custom manner as well, you must use a
custom constructor.
63. Designer Serialization
Designer serialization is a special form of serialization that involves the kind of object
persistence usually associated with development tools.
Designer serialization is the process of converting an object graph into a source file that can
later be used to recover the object graph.
A source file can contain code, markup, or even SQL table information.