Upload
opal-whitehead
View
236
Download
3
Tags:
Embed Size (px)
Citation preview
Agenda
•What is .net Remoting?•Explanation of terms•Architecture •Implementation of Remoting•Sample example
.net Security
What is Remoting in .Net?•It is a mechanism for communicating
between objects which are not in the same process.
•Enables different applications located on same computer or different computer to communicate with objects.
•Provides a framework that allows objects to interact with each other across application domains.
.net Security
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: This is the host of the remotable object also called as the 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.
.net Security
Remote Objects
•It is an object outside the application domain of the caller application.
•Any object can be changed into a remote object by deriving it from MarshalByRefObject, or by making it serializable either by adding the [Serializable]tag or by implementing the ISerializable interface.
.net Security
Types of .NET Remotable ObjectsSingle Call object: Service one and only one request
coming in.
Singleton Call object : Service multiple clients and is useful when data needs to be shared explicitly between several clients.
Client-Activated object : Richer than singleton in many aspects as they can store the state information between method calls for its specific client.
.net Security
Proxy object
•A proxy object is an object that acts in place of some other object.
•Created when client activates a remote object.
• It is an instance of the TransparentProxy class, directly available to the client to communicate with the remote object.
.net Security
Marshalling
•Specifies how a remote object is exposed to the client application.
•It is the process of packaging an object access request in one application domain and passing that request to another domain.
.net Security
Two methods • Marshalling objects by value
▫ analogous to having a copy of the server object at the client.▫ Marshal by value classes must either be marked with
the [Serilaizable] attribute in order to use the default serialization, or must implement the ISerializable interface.
• Marshalling objects by reference▫ analogous to having a pointer to the object.▫ Marshal by reference classes must inherit
from System.MarshalByRefObject.
.net Security
Channels
•It is a mechanism by which a stream of bytes is sent from one point to the other.
•Two pre-defined .NET Remoting channels existing in System.Runtime.Remoting.Channels are TCPChannel and the HTTPChannel.
.net Security
Serialization formatters
•.NET Remoting uses serialization to copy marshal-by-value objects, and to send reference of objects which are marshal-by-reference, between application domains.
•Formatters are used for encoding and decoding the messages before they are transported by the channel.
.net Security
Two types of formatters•binary(System.Runtime.Serialization.Formatte
rs.Binary) ▫Use binary encoding where performance is
critical▫Small size
•SOAP (System.Runtime.Serialization.Formatters.Soap)▫Use XML encoding where interoperability with
other remoting frameworks is essential.
.net Security
Architecture
• Client’s call• proxy to remote object• method invocation on
remote object• Message encoded • Call sent to the server
using channel
.net Security
Implementation of Remoting
• Create a Remotable Object
MyRemotableObject.cs namespace RemotableObjects{
public class MyRemotableObject : MarshalByRefObject {
public MyRemotableObject() { } public void SetMessage(string message) {
Cache.GetInstance().MessageString = message; }
}}
.net Security
.net Security
Create a Server to Expose the Remotable Object
namespace RemotableObjects {
public class frmRServer : System.Windows.Forms.Form, IObserver {
private System.Windows.Forms.TextBox textBox1;private MyRemotableObject remotableObject; private System.ComponentModel.Container components = null; public frmRServer() {
InitializeComponent(); remotableObject = new MyRemotableObject(); // using TCP
protocol TcpChannel channel = new TcpChannel(8080); ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType( typeof(MyRemotableObject),"HelloWorld",
WellKnownObjectMode.Singleton); RemotableObjects.Cache.Attach(this);
}}
}
.net Security
Create a Client to Use the Remotable Objectnamespace RemotableObjects {
public class frmRCleint : System.Windows.Forms.Form {
private System.Windows.Forms.TextBox textBox1; MyRemotableObject remoteObject; private System.ComponentModel.Container components = null; public frmRCleint() {
InitializeComponent(); // using TCP protocol
// running both client and server on same machines TcpChannel chan = new TcpChannel(); ChannelServices.RegisterChannel(chan);
// Create an instance of the remote object remoteObject = (MyRemotableObject) Activator.GetObject( typeof(MyRemotableObject),"tcp://localhost:8080/HelloWorld");
} private void textBox1_TextChanged(object sender, System.EventArgs e) { remoteObject.SetMessage(textBox1.Text); }
} }
.net Security
Step 1: Creating the Server Server.cs
using System;using System.IO;using System.Runtime.Remoting;using System.Runtime.Remoting.Channels.HTTP;namespace Server {
public class ServiceClass : MarshalByRefObject {
public void AddMessage (String msg) {
Console.WriteLine (msg);}
}public class ServerClass {
public static void Main () {
HTTPChannel c = new HTTPChannel (1095); ChannelServices.RegisterChannel (c);RemotingServices.RegisterWellKnownType ("Server","Server.ServiceClass","ServiceClass",WellKnownObjectMode.Singleton);Console.WriteLine ("Server ON at 1095");Console.WriteLine ("Press enter to stop the server...");Console.ReadLine ();
}}
}
.net Security
Compile this file using
csc /r:system.runtime.remoting.dll /r:system.dll Server.cs
This will generate a executable Server.exe , run this file and on the console u should see
Server ON at 1095 Press enter to stop the server...
.net Security
Step 2: a) Creating Client Proxy
soapsuds -url:http://<Machine Name >:1095/ServiceClass -oa:Server.dll
This will create a proxy called Server.dll which will be used to access the remote object
.net Security
Step 2: b) Creating Client Code TheClient.cs
using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels.HTTP;using Server;public class TheClient {public static void Main (string[] args) {HTTPChannel c = new HTTPChannel (1077); ChannelServices.RegisterChannel (c);ServiceClass sc = (ServiceClass) Activator.GetObject (typeof (ServiceClass),http://<Machine where Service is Running >:1095/ServiceClass); sc.AddMessage ("Hello From Client"); }}
.net Security
Compile it using
csc /r:system.runtime.remoting.dll /r:system.dll /r:Server.dll TheClient.cs
The output will be TheClient.exe, run it and check the server console on Machine 1, you will see
"Hello From Client".
References
•http://msdn.microsoft.com/en-us/library/2e7z38xb(v=VS.71).aspx
•http://www.codeproject.com/KB/IP/Net_Remoting.aspx
.net Security