370 likes | 573 Views
.Net Remoting. Architecture Marshalling Marshalling By Reference (MBR) Marshalling By Value (MBV) Activation by MBR Well-Known Object (WKO) Singleton SingleCall Client-Activated Object (CAO). Architecture Marshalling Marshalling By Reference (MBR) Marshalling By Value (MBV)
E N D
.Net Remoting • Architecture • Marshalling • Marshalling By Reference (MBR) • Marshalling By Value (MBV) • Activation by MBR • Well-Known Object (WKO) • Singleton • SingleCall • Client-Activated Object (CAO)
Architecture • Marshalling • Marshalling By Reference (MBR) • Marshalling By Value (MBV) • Activation by MBR • Well-Known Object (WKO) • Singleton • SingleCall • Client-Activated Object (CAO)
Why remoting? Server.exe (.NET) Client.exe (.NET) • The advanced of remoting is a common platform on both sides: • Allows use of a proprietary communication protocol • Allows use of a proprietary dataformat • Meaning? • More efficient • Examples of proprietary technologies: • COM (MS) • CORBA (omg.com) • RMI (Java) • There might be different OS's on client and server i.e. Windows and Linux
DB DT BT Why use remoting? • To connect tiers within the same application... • client & server is both .NET assemblies • Example: • A typical business app has Business and Data Access tiers • GUI calls into the business tier to get data, calculation & validation • Business tier makes calls to the data tier for writing / reading data Server (.NET) Client (.NET) BusinessTier Data AccessTier
.DLL Remoting seen from the clientand server Client.exe Server • The client sees the server as an assembly .DLL • sets a reference to a object as normally • The server makes .DLL available in one of two ways: • Runs as a service on the server, that responds on remote calls • Runs in the web server & trigger remote calls via URL • advances? • #1 can use proprietary protocols & formats (more efficient) • #2 is firewall-friendly, easy use of Windows security
Comp Data Proxy Comp Stub Data Data Proxy Data Stub Design, more detailed • Business and calculation objects lives on the server • Data objects marshals to the clients Client call Client call Server
Architecture • Marshalling • Marshalling By Reference (MBR) • Marshalling By Value (MBV) • Activation by MBR • Well-Known Object (WKO) • Singleton • SingleCall • Client-Activated Object (CAO)
Marshalling models • Marshalling By Reference (MBR) • The object lives on the server. • It can exist as a well-known object (WKO) that either: • is created each time a service is requested [SingleCall] (and deleted afterwards) • or shared by all clients. [Singleton]It is created first time a service is called • It can also exist as a Client Activated Object (CAO). Here is it the application domain of the client that controls life and death
Marshalling models • Marshalling By Value (MBV) • The object is created on the server then serialized and send to the client. • State changes of the object on the client side are not registried on the server • Is often a return value from a public method from a MBR object
Eksample .... of MBR and MBV
Procedure: • Steps: • Mark data objects as serializable [Serializable] • Define the remote interface (which services are possible) • The remote object have to • implement the remote interface • inherit from MarshalByRefObject • have a default constructor (a parameter less constructor) • On the server side: • Create and register a channel type • Register the remote service • On the client side: • Register server connection • Create a reference to the remote objectNote: The interface but not the implementing class should be available on the client.
Exsample: • Step 1: The data object: [Serializable] //Makes Customer a MBV public class Customer { String _firstName; String _lastName; DateTime _dateOfBirth; public Customer(){...} public String FirstName{ get { return _firstName; } set { _firstName = value; }} public String LastName{.... //omitted on slide} public DateTime DateOfBirth{.... //omitted on slide} public int GetAge() { TimeSpan tmp = DateTime.Today.Subtract(DateOfBirth); return tmp.Days / 365;} }
Step 2: Interface public interface ICustomerManager { Customer GetCustomer(int id); }
Step 3: Implement the remote objekt class CustomerManager : MarshalByRefObject, ICustomerManager { public CustomerManager() { Console.WriteLine("CustomerManager Created"); } public Customer GetCustomer(int id) { Console.WriteLine("CustomerManager.GetCustomer called"); Customer tmp = new Customer(); tmp.FirstName = "John"; tmp.LastName = "Doe"; tmp.DateOfBirth = new DateTime(1970, 7, 4); return tmp; } }
Step 4: Implement the server • Set up the socket communication (protocol and port) class ServerStartup { static void Main(string[] args) { Console.WriteLine("Start service"); HttpChannel channel = new HttpChannel(1234); ChannelServices.RegisterChannel(channel, false); RemotingConfiguration.RegisterWellKnownServiceType(typeof(CustomerManager), "CustomerManager.soap”, WellKnownObjectMode.Singleton); Console.WriteLine("Service startet"); Console.ReadLine(); } }
Step 4: Implement the server • Register the object, CustomerManager, as a remote object, reference id on the server (CustomerManager.soap) and WKO mode class ServerStartup { static void Main(string[] args) { Console.WriteLine("Start service"); HttpChannel channel = new HttpChannel(1234); ChannelServices.RegisterChannel(channel); RemotingConfiguration.RegisterWellKnownServiceType(typeof(CustomerManager), "CustomerManager.soap”, WellKnownObjectMode.Singleton); Console.WriteLine("Service startet"); Console.ReadLine(); } }
Step 5: Implement the client static void Main(string[] args) { try { HttpChannel channel = new HttpChannel(); ChannelServices.RegisterChannel(channel); ICustomerManager mgr = (ICustomerManager)Activator.GetObject(typeof(ICustomerManager), "http://localhost:1234/CustomerManager.soap"); Console.WriteLine("Reference to CustomerManager created "+mgr); Customer cust = mgr.GetCustomer(4711); int age = cust.GetAge(); Console.WriteLine("{0} {1} er {2} år", cust.FirstName, cust.LastName, age); } } • Set up the socket
Step 5: Implement the client static void Main(string[] args) { try { HttpChannel channel = new HttpChannel(); ChannelServices.RegisterChannel(channel, false); ICustomerManager mgr = (ICustomerManager)Activator.GetObject(typeof(ICustomerManager), "http://localhost:1234/CustomerManager.soap"); Console.WriteLine("Reference to CustomerManager created "+mgr); Customer cust = mgr.GetCustomer(4711); int age = cust.GetAge(); Console.WriteLine("{0} {1} er {2} år", cust.FirstName, cust.LastName, age); } } • Get an ICustomerManager object from the given url. • Note that only the interface is given as the type
Step 5: Implement the client static void Main(string[] args) { try { HttpChannel channel = new HttpChannel(); ChannelServices.RegisterChannel(channel, false); ICustomerManager mgr = (ICustomerManager)Activator.GetObject(typeof(ICustomerManager), "http://localhost:1234/CustomerManager.soap"); Console.WriteLine("Reference to CustomerManager created "+mgr); Customer cust = mgr.GetCustomer(4711); int age = cust.GetAge(); Console.WriteLine("{0} {1} is {2} years old", cust.FirstName, cust.LastName, age); } } • Get a copy of the Customer object from the server. • Note that the class is necessary
What is happening? • A stub is generated for both server and client • The stub is responsible for sending messages by a socket • On the client, the stub represents the remote object (located on the server) as a proxy class • The example: The stub implements ICustomerManager like the remote object (CustomerManager). • The course of events is in principle like this: • The client calls a method in the stub. The client doesn't know the stub's implementation, but knows the method because of the interface • The client's stub sends the request to the server's stub. • The server's stub calls the relevant method in remote object. • And sends the return value back
Communication channels • Requests, objects etc. are sent by channels. • Channels differs by protocols • A channel uses a socket stream. • There is 3 types of channels: • TcpChannel: • Uses BinaryFormatter • Advances: Is fast because raw data are sent. • Back draw: Might have problems with firewalls and nat'ing. • HttpChannel: • Uses SoapFormatter • Advances: Sends text by http on port 80, therefore no firewall problems (normally). • Back draw: Is slower, because of overhead from xml-tags. • Do not support generic classes • IpcChannel: • Used for communication by two applications in the same application domain, and therefore on the same machine.
Architecture • Marshalling • Marshalling By Reference (MBR) • Marshalling By Value (MBV) • Activation by MBR • Well-Known Object (WKO) • Singleton • SingleCall • Client-Activated Object (CAO)
WellKnown Objekt (WKO) • As you might remember: A reference to a remote object. • There is two types of WKO's: • SingleCall • Singleton • WKO's should be handled as stateless.
WellKnown Objekt (WKO) • SingleCall: • Every time a call to remote object is made, a new object is created.It is deleted when the call is finished. • One client pr. object • Advances: There is no problems with transactions. • Back draw: The server uses resources creation and deletion of objects. Both activities uses resources for memory management.
WellKnown Objekt (WKO) • Singleton: • First time a call is made, the object is created. It is not deleted before the lease (or timeout period) runs out. • Many clients pr. object • Advances: No time used for memory management • Back draw: Concurrent calls to the same object might break the class invariant, and therefore returning false return values.
Architecture • Marshalling • Marshalling By Reference (MBR) • Marshalling By Value (MBV) • Activation by MBR • Well-Known Object (WKO) • Singleton • SingleCall • Client-Activated Object (CAO)
Client Activated Object (CAO) • CAO objects are MBR objects too. • The client can instantiate objects on the server (even with new) • Often CAO-objects are returned by method calls to MBR objects • There exists a 1-1 relation between object and client • Therefore CAO can be used as state full.
A remote object public class MyRemoteObject: MarshalByRefObject { int myvalue; public MyRemoteObject(int val) { Console.WriteLine("MyRemoteObject.ctor(int) called"); myvalue = val; } public MyRemoteObject() { Console.WriteLine("MyRemoteObject.ctor() called"); } public void setValue(int newval){....} public int getValue(){...} }
Server • "MyServer" is used in the reference on the client static void Main(string[] args) { Console.WriteLine ("ServerStartup.Main(): Server started"); HttpChannel chnl = new HttpChannel(1234); ChannelServices.RegisterChannel(chnl); RemotingConfiguration.ApplicationName = "MyServer"; RemotingConfiguration.RegisterActivatedServiceType( typeof(MyRemoteObject)); // the server will keep running until keypress. Console.ReadLine(); }
Server • Register the CAO on the server • Is not necessary if the object is returned by a remote method static void Main(string[] args) { Console.WriteLine ("ServerStartup.Main(): Server started"); HttpChannel chnl = new HttpChannel(1234); ChannelServices.RegisterChannel(chnl); RemotingConfiguration.ApplicationName = "MyServer"; RemotingConfiguration.RegisterActivatedServiceType( typeof(MyRemoteObject)); // the server will keep running until keypress. Console.ReadLine(); }
Client static void Main(string[] args) { HttpChannel channel = new HttpChannel(); ChannelServices.RegisterChannel(channel); RemotingConfiguration.RegisterActivatedClientType( typeof(Server.MyRemoteObject), "http://localhost:1234/MyServer"); Console.WriteLine("Client.Main(): Creating first object"); Server.MyRemoteObject obj1 = new Server.MyRemoteObject(); obj1.setValue(42); Console.WriteLine("Client.Main(): Creating second object"); Server.MyRemoteObject obj2 = new Server.MyRemoteObject(); obj2.setValue(4711); Console.WriteLine("Obj1.getValue(): {0}",obj1.getValue()); Console.WriteLine("Obj2.getValue(): {0}",obj2.getValue()); Console.ReadLine(); } • Register the CAO. • Note that the class MyRemoteObject is used
Client static void Main(string[] args) { HttpChannel channel = new HttpChannel(); ChannelServices.RegisterChannel(channel); RemotingConfiguration.RegisterActivatedClientType( typeof(Server.MyRemoteObject), "http://localhost:1234/MyServer"); Console.WriteLine("Client.Main(): Creating first object"); Server.MyRemoteObject obj1 = new Server.MyRemoteObject(); obj1.setValue(42); Console.WriteLine("Client.Main(): Creating second object"); Server.MyRemoteObject obj2 = new Server.MyRemoteObject(); obj2.setValue(4711); Console.WriteLine("Obj1.getValue(): {0}",obj1.getValue()); Console.WriteLine("Obj2.getValue(): {0}",obj2.getValue()); Console.ReadLine(); } • Make a new instance using new
Error: Because of security restrictions, the type System.Runtime.Remoting.ObjRef cannot be accessed. • Caused by insufficient security level. • change • to • Easier to do in the config-file (next lesson) HttpChannel channel = new HttpChannel(1234); ChannelServices.RegisterChannel(channel, false); SoapServerFormatterSinkProvider serverProv = new SoapServerFormatterSinkProvider(); serverProv.TypeFilterLevel = TypeFilterLevel.Full; SoapClientFormatterSinkProvider clientPorv = new SoapClientFormatterSinkProvider(); IDictionary props = new Hashtable(); props["port"] = 1234; HttpChannel channel = new HttpChannel(props, clientPorv, serverProv); ChannelServices.RegisterChannel(channel, false);