1 / 23

Distributed Systems

Distributed Systems. Tutorial 2 - .NET Remoting. What is Remoting?. Remoting allows you to pass objects or values across servers in different domains using several protocols.

eydie
Download Presentation

Distributed Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Distributed Systems Tutorial 2 - .NET Remoting

  2. What is Remoting? • Remoting allows you to pass objects or values across servers in different domains using several protocols. • Remoting enables client code in one application domain to call methods/properties of objects running in another application domain.

  3. Remoting • If anyone configures the client properly, we need only to create a new instance of the remote object using the new keyword, then client receives a reference to the server object, and rest of the things are as usual (like invoking methods) as though the object were in your process though it is running on a separate computer.

  4. Remoting in general • Server needs to publish an object • For example: • I listen on this TCP channel and that HTTP channel • I have a service called "MathService" • When client connects to MathService using a supported channel, give the client the requested instance • Clients must connect to servers • Clients need to specify the desired object • For example: • Connect to the "MathService" on server “X" using protocol HTTP on port 80

  5. Building the sample application • In the below example, the remote object exposes two methods for adding and subtracting given two numbers. • Building an application that uses .NET Remoting to communicate across application domain boundaries is very straightforward: • You must have an implementation of a remotable type. • A listening or host application domain. • A client or calling application domain. • And you must configure the remoting system in each application domain to use remote activation for the remotable type. • The above process applies no matter how complex or simple the remoting scenario becomes.

  6. Remoting Application Development Flow • Write the component(s) that you wish to make remotely accessible into a .NET DLL (Shared Assembly). • Configure any managed executable to host those components (Server Assembly) • Write the client(s) that call the components (Client Assembly).

  7. Building Remotable Type • To enable objects in other application domains to use an instance of the class, the class must inherit from MarshalByRefObjet and then the runtime creates a proxy to the object • The following code example shows a simple object that can be created and invoked from objects executing in another application domain.

  8. MathLibrary.cs using System; public class MathLibrary : MarshalByRefObject { private int result; public int AddTwoNumber(int num1, int num2) { result = num1 + num2; return result; } public int SubtractTwoNumber(int num1, int num2) { result = num1 - num2; return result; }}

  9. Compile this file as a DLL from the command prompt • csc /noconfig /t:library MathLibrary.cs

  10. Building a Host Application • To create instances of MathLibrary remotely, you must build a host or listener application which does two things: • Choose and register a channel, which is an object that handles the networking protocols and serialization formats. • Register MathLibrary with the .NET Remoting system so that it can use your channel to listen for requests for your type. • The following code implements a simple MathLibrary host application domain that uses a configuration file.

  11. Listener.cs using System; using System.Runtime.Remoting; public class Listener { public static void Main() { RemotingConfiguration.Configure("Listener.exe.config"); Console.WriteLine ("Listening for requests. Press Enter to exit..."); Console.ReadLine(); } }

  12. Listener.exe.config <configuration> <system.runtime.remoting> <application> <service> <wellknown mode="Singleton" type="MathLibrary, MathLibrary" objectUri="MathLibrary.rem"/> </service> <channels> <channel ref="http" port="8989"/> </channels> </application> </system.runtime.remoting> </configuration>

  13. Another option is to do the following: • // Register a listening Channel • HttpChannel channel = new HttprChannel(8989); • ChannelServices.RegisterChannel(channel); • // Register a well-known type • RemotingConfiguration.RegisterWellKnownServiceType( typeof(MathLibrary), "MathLibrary.rem", WellKnownObjectMode.Singleton);

  14. Store the code of Listener.cs in the same directory as where MathLibrary.dll is created. • Compile Listener.cs with reference to MathLibrary.dll as below: • csc /noconfig /r:MathLibrary.dll Listener.cs • Since the code of Listener.cs uses Listener.exe.config file to listen to its remotable type, we need to create the Listener.exe.config file in the same directory where we created the Listener.exe.

  15. Building a Client Application • Till now, we have created MathLibrary and the host application for Remoting. • Our application must register itself as a client for the remote object, and then invoke it as residing in the client application domain. • The .NET Remoting system intercepts the client calls, forwards them to the remote object, and returns the results to your client.

  16. Client.cs using System; using System.Runtime.Remoting; public class Client { public static void Main() { RemotingConfiguration.Configure("Client.exe.config"); MathLibrary lib = new MathLibrary(); Console.WriteLine("Enter Number1:"); string num1 = Console.ReadLine(); Console.WriteLine("Enter Number2:"); string num2 = Console.ReadLine(); Console.WriteLine(lib.AddTwoNumber(Convert.ToInt16(num1), Convert.ToInt16(num2)).ToString()); Console.WriteLine ("Press Enter to exit..."); Console.ReadLine();}}

  17. Compile Client.cs with reference to MathLibrary.dll as below: • csc /noconfig /r:MathLibrary.dll Client.cs • Since the code of Client.cs uses Client.exe.config file to listen to its remotable type, we need to create the Client.exe.config file in the same directory where we created the Client.exe

  18. Client.exe.config <configuration> <system.runtime.remoting> <application> <client> <wellknown type="MathLibrary, MathLibrary" url="http://localhost:8989/MathLibrary.rem" /> </client> </application> </system.runtime.remoting> </configuration>

  19. Running the application • Now, every thing is ready to run your application: • Run Listener.exe • Run Client.exe

  20. Backup • Disclaimer: The following slides were taken from www.wintellect.com

  21. Client-side remoting • Client calls method on TransparentProxy • TransparentProxy builds message • Stack frame to message • TransparentProxy passes msg to RealProxy • RealProxy forwards msg to envoy sink(s) • Last envoy sink forwards msg to context sink(s) • Last context sink forwards msg to channel sink(s) • Channel sinks turn message into a byte stream • Last channel sink sends byte stream to server

  22. Server-side remoting • Channel receives a stream of bytes • Channel forwards stream to channel sink(s) • Last channel sink converts stream into message • Last channel sink forwards msg to context sink(s) • Last context sink forwards msg to server sink(s) • Last server sink is stack frame sink • Stack frame sink builds and issues call to requested method

  23. Message Message Transparent Proxy Real Proxy Remoting chain Envoy Sink TxSink Envoy Sink TxSink Envoy Sinks ContextSinks Formatter Sink 010110… Channel Transport Sink Channel Sink Channel Sink Client-side Server-side Channel 010110… Transport Sink Channel Sink Channel Sink TxSink TxSink TxSink TxSink Serverobject Stack Builder Sink ServerSinks ContextSinks FormatterSink

More Related