distributed systems tutorial 2 -.net remoting. 2 what is remoting? remoting allows you to pass...
Post on 20-Dec-2015
235 views
TRANSCRIPT
![Page 1: Distributed Systems Tutorial 2 -.NET Remoting. 2 What is Remoting? Remoting allows you to pass objects or values across servers in different domains](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649d4b5503460f94a293a2/html5/thumbnails/1.jpg)
Distributed Systems
Tutorial 2 - .NET Remoting
![Page 2: Distributed Systems Tutorial 2 -.NET Remoting. 2 What is Remoting? Remoting allows you to pass objects or values across servers in different domains](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649d4b5503460f94a293a2/html5/thumbnails/2.jpg)
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.
![Page 3: Distributed Systems Tutorial 2 -.NET Remoting. 2 What is Remoting? Remoting allows you to pass objects or values across servers in different domains](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649d4b5503460f94a293a2/html5/thumbnails/3.jpg)
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.
![Page 4: Distributed Systems Tutorial 2 -.NET Remoting. 2 What is Remoting? Remoting allows you to pass objects or values across servers in different domains](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649d4b5503460f94a293a2/html5/thumbnails/4.jpg)
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
![Page 5: Distributed Systems Tutorial 2 -.NET Remoting. 2 What is Remoting? Remoting allows you to pass objects or values across servers in different domains](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649d4b5503460f94a293a2/html5/thumbnails/5.jpg)
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.
![Page 6: Distributed Systems Tutorial 2 -.NET Remoting. 2 What is Remoting? Remoting allows you to pass objects or values across servers in different domains](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649d4b5503460f94a293a2/html5/thumbnails/6.jpg)
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).
![Page 7: Distributed Systems Tutorial 2 -.NET Remoting. 2 What is Remoting? Remoting allows you to pass objects or values across servers in different domains](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649d4b5503460f94a293a2/html5/thumbnails/7.jpg)
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.
![Page 8: Distributed Systems Tutorial 2 -.NET Remoting. 2 What is Remoting? Remoting allows you to pass objects or values across servers in different domains](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649d4b5503460f94a293a2/html5/thumbnails/8.jpg)
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;
}}
![Page 9: Distributed Systems Tutorial 2 -.NET Remoting. 2 What is Remoting? Remoting allows you to pass objects or values across servers in different domains](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649d4b5503460f94a293a2/html5/thumbnails/9.jpg)
9
Compile this file as a DLL from the command prompt csc /noconfig /t:library MathLibrary.cs
![Page 10: Distributed Systems Tutorial 2 -.NET Remoting. 2 What is Remoting? Remoting allows you to pass objects or values across servers in different domains](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649d4b5503460f94a293a2/html5/thumbnails/10.jpg)
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.
![Page 11: Distributed Systems Tutorial 2 -.NET Remoting. 2 What is Remoting? Remoting allows you to pass objects or values across servers in different domains](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649d4b5503460f94a293a2/html5/thumbnails/11.jpg)
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();}
}
![Page 12: Distributed Systems Tutorial 2 -.NET Remoting. 2 What is Remoting? Remoting allows you to pass objects or values across servers in different domains](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649d4b5503460f94a293a2/html5/thumbnails/12.jpg)
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>
![Page 13: Distributed Systems Tutorial 2 -.NET Remoting. 2 What is Remoting? Remoting allows you to pass objects or values across servers in different domains](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649d4b5503460f94a293a2/html5/thumbnails/13.jpg)
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);
![Page 14: Distributed Systems Tutorial 2 -.NET Remoting. 2 What is Remoting? Remoting allows you to pass objects or values across servers in different domains](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649d4b5503460f94a293a2/html5/thumbnails/14.jpg)
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.
![Page 15: Distributed Systems Tutorial 2 -.NET Remoting. 2 What is Remoting? Remoting allows you to pass objects or values across servers in different domains](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649d4b5503460f94a293a2/html5/thumbnails/15.jpg)
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.
![Page 16: Distributed Systems Tutorial 2 -.NET Remoting. 2 What is Remoting? Remoting allows you to pass objects or values across servers in different domains](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649d4b5503460f94a293a2/html5/thumbnails/16.jpg)
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();}}
![Page 17: Distributed Systems Tutorial 2 -.NET Remoting. 2 What is Remoting? Remoting allows you to pass objects or values across servers in different domains](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649d4b5503460f94a293a2/html5/thumbnails/17.jpg)
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
![Page 18: Distributed Systems Tutorial 2 -.NET Remoting. 2 What is Remoting? Remoting allows you to pass objects or values across servers in different domains](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649d4b5503460f94a293a2/html5/thumbnails/18.jpg)
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>
![Page 19: Distributed Systems Tutorial 2 -.NET Remoting. 2 What is Remoting? Remoting allows you to pass objects or values across servers in different domains](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649d4b5503460f94a293a2/html5/thumbnails/19.jpg)
19
Running the application
Now, every thing is ready to run your application: Run Listener.exe Run Client.exe
![Page 20: Distributed Systems Tutorial 2 -.NET Remoting. 2 What is Remoting? Remoting allows you to pass objects or values across servers in different domains](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649d4b5503460f94a293a2/html5/thumbnails/20.jpg)
20
Backup
Disclaimer: The following slides were taken from www.wintellect.comwww.wintellect.com
![Page 21: Distributed Systems Tutorial 2 -.NET Remoting. 2 What is Remoting? Remoting allows you to pass objects or values across servers in different domains](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649d4b5503460f94a293a2/html5/thumbnails/21.jpg)
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
![Page 22: Distributed Systems Tutorial 2 -.NET Remoting. 2 What is Remoting? Remoting allows you to pass objects or values across servers in different domains](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649d4b5503460f94a293a2/html5/thumbnails/22.jpg)
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
![Page 23: Distributed Systems Tutorial 2 -.NET Remoting. 2 What is Remoting? Remoting allows you to pass objects or values across servers in different domains](https://reader035.vdocuments.mx/reader035/viewer/2022062313/56649d4b5503460f94a293a2/html5/thumbnails/23.jpg)
23
TxSinkTx
Sink
EnvoySink
EnvoySink
Remoting chain
Serverobject
EnvoySinks
Message
TransportSink
ChannelSink
ChannelSink
ChannelTransport
SinkChannelSink
ChannelSink
Channel
StackBuilderSink
TransparentProxy
RealProxy
ContextSinks
FormatterSink
Client-side
Server-side
Message
FormatterSink
TxSinkTx
SinkContextSinks
TxSinkTx
SinkServerSinks
010110…
010110…