Session 15 TP 8

Embed Size (px)

Citation preview

  • 7/31/2019 Session 15 TP 8

    1/22

    An Introduction to .NETRemoting-I

  • 7/31/2019 Session 15 TP 8

    2/22

    Advanced .NET Programming/Session 15/Slide 2 of 22

    Review Object Serialization is the process of reducing an object instance into a format that can

    either be stored to disk or transported over a network. This process is achieved so that the object can be recreated with its current state at a

    later point of time, or at a different location. The following are required to serialize an object:

    The object that is to be serialized itself A Stream to contain the serialized object A formatter which is used to serializes the object.

    System.Runtime.Serialization is the namespace which contains the classes that arerequired to serialize an object.

    Deserialization involves the object being restored back to the state it was serialized to (orin other words, saved as).

    To deserialize an object in .NET, we use the DeSerialize() method of the BinaryFormatterclass.

    For serialization, the class should be marked by the [Serializable] attribute. SOAP format represents the object's state in XML-SOAP based tags. To reduce the amount of data serialized to the stream, one can selectively serialize

    members. Members that are not required to reconstitute the class can be marked with a[NonSerialized] attribute.

    The main benefit of SOAP serialization is portabilityone can share the serializationinformation with any .NET application, on any platform.

  • 7/31/2019 Session 15 TP 8

    3/22

    Advanced .NET Programming/Session 15/Slide 3 of 22

    Objectives Discuss .NET Remoting

    Explain the need for .NET Remoting

    Discuss the .NET Remoting Architecture

    Explain Channels

    Build simple clients and services

  • 7/31/2019 Session 15 TP 8

    4/22

    Advanced .NET Programming/Session 15/Slide 4 of 22

    Introduction .NET Remoting

    How .NET remoting enables distributed computing

    Differentiating .NET Remoting with XML-based webservices

    Concept of channels

    Build a simple client and a server using Remoting

  • 7/31/2019 Session 15 TP 8

    5/22

    Advanced .NET Programming/Session 15/Slide 5 of 22

    Evolution of Distributed Computing

    (1) The complications involved in the repetitious processing of business

    logic led to the advancement in computing in the business sector. The more complexity the software tried to process, the larger the

    systems hardware requirements became.

    This led to the development of Scale-Up strategy, in which, as thesoftware grew complex and larger, the hardware system needed to bereplaced.

  • 7/31/2019 Session 15 TP 8

    6/22

    Advanced .NET Programming/Session 15/Slide 6 of 22

    Evolution of Distributed Computing

    (2) The Scale-Up strategy soon gave way to the Scale-Out strategy. In the Scale-Out strategy, it was no longer needed to discard the old

    system for a new one.

  • 7/31/2019 Session 15 TP 8

    7/22Advanced .NET Programming/Session 15/Slide 7 of 22

    Evolution of Distributed Computing

    (3) The Scale-Out strategy has the following

    advantages and disadvantages:

    Advantages No need to replace the entire system; just add new

    hardware

    More fault tolerant systems

    Disadvantages For extra modules purchase of new hardware needed

    The network was needed to be owned by thebusiness.

  • 7/31/2019 Session 15 TP 8

    8/22Advanced .NET Programming/Session 15/Slide 8 of 22

    Distributed Computing Application of Vendor A interacting with the applications

    of Vendor B

    Technologies like DCOM & CORBA failed in their attemptto enable interaction between applications.

    .NET Remoting offers seamless interaction using XML &SOAP.

  • 7/31/2019 Session 15 TP 8

    9/22Advanced .NET Programming/Session 15/Slide 9 of 22

    An Introduction to .NET Remoting Saves developers time and effort in coding

    Provides developers with a framework thatallows objects to interact with one anotheracross application domains, whether onthe same physical system or over anetwork

  • 7/31/2019 Session 15 TP 8

    10/22Advanced .NET Programming/Session 15/Slide 10 of 22

    .NET Remoting Terminology Remote Object

    Formatter provider

    Messages

    Message Sink

    RemotingConfiguration

    Formatter

    Channel

    Proxy

    Activator

    ChannelServices

  • 7/31/2019 Session 15 TP 8

    11/22Advanced .NET Programming/Session 15/Slide 11 of 22

    Client calling a Remote Object Proxy is a local object, which is an image of a remote object

  • 7/31/2019 Session 15 TP 8

    12/22

    Advanced .NET Programming/Session 15/Slide 12 of 22

    Deserialization of a message on the

    server side The channel on the server side receives the formatted

    message from the network.

    The channel deserializes the message with the help of the

    associated formatter.

  • 7/31/2019 Session 15 TP 8

    13/22

    Advanced .NET Programming/Session 15/Slide 13 of 22

    Building a simple client & server (1) Example 1

    using System;

    namespace RemObjEx1

    {public class MyClass : System.MarshalByRefObject

    {

    public MyClass()

    {

    Console.WriteLine("This is the Constructor ofMyClass");

    }public void HelloWorld()

    {

    Console.WriteLine("A Big Hello To The World!");

    }

    }

    }

  • 7/31/2019 Session 15 TP 8

    14/22

    Advanced .NET Programming/Session 15/Slide 14 of 22

    Building a simple client & server (2) Example 2

    using System;

    using System.Runtime.Remoting;

    using System.Runtime.Remoting.Channels;using System.Runtime.Remoting.Channels.Tcp;

    using RemObjEx1;

    namespace RemObjEx2

    {

    class MyServer

    {

    static void Main(string[] args){

    TcpServerChannel channel = new TcpServerChannel(8888);

    ChannelServices.RegisterChannel(channel);

    RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyClass), "HelloWorld", WellKnownObjectMode.SingleCall);

  • 7/31/2019 Session 15 TP 8

    15/22

  • 7/31/2019 Session 15 TP 8

    16/22

    Advanced .NET Programming/Session 15/Slide 16 of 22

    Building a simple client & server (4) Example 3

    using System;

    using System.Runtime.Remoting.Channels;

    using System.Runtime.Remoting.Channels.Tcp;using RemObjEx1;

    namespace RemObjEx3

    {

    class MyClient

    {

    static void Main(string[] args)

    {ChannelServices.RegisterChannel(new TcpClientChannel());

    MyClass obj =(MyClass)Activator.GetObject(typeof(MyClass),"tcp://localhost:8888/HelloWorld");

    int cnt = 0;

  • 7/31/2019 Session 15 TP 8

    17/22

    Advanced .NET Programming/Session 15/Slide 17 of 22

    Building a simple client & server (5)for(cnt = 0;cnt < 5;cnt++)

    {

    obj.HelloWorld();

    }

    }}

    }

  • 7/31/2019 Session 15 TP 8

    18/22

    Advanced .NET Programming/Session 15/Slide 18 of 22

    Building a simple client & server (6) Output after starting the server in Example 2

    and executing Example 3 -

  • 7/31/2019 Session 15 TP 8

    19/22

    Advanced .NET Programming/Session 15/Slide 19 of 22

    HTTP implementation of Example 2

    (1) Example 4

    using System;

    using System.Runtime.Remoting;

    using System.Runtime.Remoting.Channels;using System.Runtime.Remoting.Channels.Http;

    using RemObjEx1;

    namespace RemObjEx4

    {

    class MyServer

    {

    static void Main(string[] args)

    {

    HttpServerChannel channel = new HttpServerChannel(8099);

    ChannelServices.RegisterChannel(channel);

    RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyClass), "HelloWorld", WellKnownObjectMode.SingleCall);

  • 7/31/2019 Session 15 TP 8

    20/22

    Advanced .NET Programming/Session 15/Slide 20 of 22

    HTTP implementation of Example 2

    (2)Console.WriteLine("Press ENTER To Stop Server!");

    Console.ReadLine();

    }

    }

    }

  • 7/31/2019 Session 15 TP 8

    21/22

  • 7/31/2019 Session 15 TP 8

    22/22

    Advanced NET Programming/Session 15/Slide 22 of 22

    Summary In the Scale-Up strategy, as the software grew complex and larger, the hardware system

    needed to be replaced. In the Scale-Out strategy, businesses could continue to use existing hardware, and simply plug

    in new computer systems and hook them up through a network. The Scale-Out strategy has the following advantages:

    No need to replace the entire system; Just add new hardware More Fault Tolerant System

    .NET Remoting provides developers with a framework that allows objects to interact with oneanother across application domains, whether on the same physical system, or over a network.

    An application domain can be thought of as a sub-process within another process. A remote object is an object that runs on the server. The formatter defines how messages are transferred across the network (channel). A formatter provider is used to associate a formatter with a channel. A channel is used for communication between the client and the server. Messages are sent across channels; these are created for and facilitate communication between

    the client and the server. A proxy is called by the client instead of a remote object. The proxy, in turn, speaks to the

    remote object on the server. A message sink is associated with a channel. A client uses an activator to create remote objects on the server.