Home > SKIP > Visual Studio .Net > Serialization, Remoting And Threading

Serialization, Remoting And Threading

Object

What is an Object?
Object is an instance of a class.

What is an Instance?
Memory which is reserved for the contents of exe file (which is present in the Hard Disk) in RAM at run time
Ex: Hello h = new Hello();

Serialization

Serialization is the process of taking an object and converting it to a format in which it can be transported across a network or persisted to a storage location.

Deserialization is the process of using the serialized state information to reconstruct the object from the serialized state to its original state.

How to make a class serializable?
Add the [Serializable] attribute in front of the class' definition.

[Serializable]
public class Store
{
private int sCount;
[NonSerialized] private int temp;
}

Serialization Formatters

  • There are three formats provided by the Microsoft .NET framework to which objects can be serialized. The formats are binary, SOAP, and XML.
  • The format is controlled based upon what object is used to perform the serialization.
  • The XML format is produced by using the System.Xml.Serialization.XmlSerializer class.
  • The SOAP and binary formats are produced by using classes under the System.Runtime.Serialization.Formatters namespace.

Serialization Demo

» Click here to download sample code

What Is Remoting?

Remoting is a means by which one operating system process, or program, can communicate with another process. The two processes can exist on the same computer or on two computers connected by a LAN or the Internet. Communicating between two programs may seem like a big "so what," but it's a rather involved process.

Remoting Terminology

  • Marshalling – Serializing an object and passing it over net work and again deserializing an object
  • Sink - an object that allows custom processing of messages during remote invocation.
  • Channel – an object that transports messages across remoting boundaries
  • Remotable object – the object that can be marshaled across application domains.
  • Formatter – an object that is responsible for encoding/decoding and serializing data into messages.
  • AppDomain – an Application Domain, a .NET concept for a unit of isolation for an application.

Remotable Objects

  • In order for an object to be remotable it has to meet one of the following criteria:
    • Derived from MarshalByRefObject.
    • Derived from Component which is derived from MarshalByRefObject.
    • Derived from MarshalByValueComponent.
    • Implements ISerializable.
    • Marked with the Serializable attribute.

Marshalling Choices

  • Marshall By Value –
    • All the data for the object is passed over the network to the client.
    • Once the data is on the client all subsequent calls are made to the local copy on the client.
    • Changes to state are not reflected on the server.
    • Initial call might be lengthy due to the transmission of the entire object’s data but subsequent calls will be faster.

MBV vs MBR

Writing Serializable Components

  • To make an object marshall itself by value, make it serializable
    • Use the [Serializable] attribute or implement ISerializable

[Serializable]
public class SampleRemoteObject2
{
}

// function that returns a Serializable object
public SampleRemoteObject2 GetFun()
{
}

MarshalByRef objects:

  • The object lives on the server; its data is not copied to the client.
  • Initial object creation is quick due to very little network traffic.
  • Each method call involves a round trip over the network making calls slower.

To make an object remotable, simply derive it from MarshallByRefObject

public class SampleRemoteObj: MarshalByRefObject
{
}

Activation Choices

  • Server Activated Object (SAO) – Singleton – all clients access the same object
  • Single Call – object is created, used for one method call and then disposed
  • Client Activated Object (CAO) – Explicitly created by the client on the server, maintains state between method calls

Remoting Server

The following can all act as Remoting Servers..

  • IIS
  • Windows Service
  • WinForms App
  • Console Application

Remoting Client

  • WinForm App
  • Web App
  • Web Service
  • Console App
  • Windows Service

How Does Remoting Work?

How Does Remoting Work?

Local Proxy Object

Local Proxy Object

Formatter Sink

Formatter Sink

Transport Sink

Transport Sink

Transport Sink - Server

Transport Sink - Server

Formatter Sink - Server

Formatter Sink - Server

Dispatcher

Dispatcher

Remoting Demo

» Click here to download sample code

Threading

Process: The memory space, where a given application is executed is called - process. A Process is the memory set aside for an application to be executed in. Within this process the thing, which is really executed is the thread. The operation system allocates CPU's time to the thread. So a process contains at least one thread but the process can contain many threads, which can be executed "simultaneously" by sharing the CPU.

Thread: So a thread is the compound unit of a process, which the operating system allocates CPU's time to. A process always has at least one thread running, but it also can have many threads running, which share the CPU's time.

Single threaded versus Multi threaded application

For example if the application consists of only one thread and there is an operation which can take much time such as requesting data from a remote server. The CPU will send the request to the server and wait for the response. This can result in much idle time and the user will have to wait without doing anything else. On the other hand if the application uses more than one thread, the operation of requesting data from a remote computer can fire a new thread, and the running one will still be on focus, allowing the user to perform what action wants, while the request and data travel.

Methods for creating threads in .NET There are two methods for creating threads in a .NET application:

  • Thread class
  • Thread pool

Threading Demo

» Click here to download sample code

Synchronization

Synchronization is the process of ensuring that only one thread can access a given Resource at a time. There are many ways of synchronizing thread and two of them are through Monitor class and lock keyword.

References