1 / 35

.Net Remoting

.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)

yael
Télécharger la présentation

.Net Remoting

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. .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)

  2. Architecture • Marshalling • Marshalling By Reference (MBR) • Marshalling By Value (MBV) • Activation by MBR • Well-Known Object (WKO) • Singleton • SingleCall • Client-Activated Object (CAO)

  3. 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

  4. 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

  5. .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

  6. In principle

  7. Proxy pattern

  8. 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

  9. Architecture • Marshalling • Marshalling By Reference (MBR) • Marshalling By Value (MBV) • Activation by MBR • Well-Known Object (WKO) • Singleton • SingleCall • Client-Activated Object (CAO)

  10. 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

  11. 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

  12. Eksample .... of MBR and MBV

  13. 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.

  14. 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;} }

  15. Step 2: Interface public interface ICustomerManager { Customer GetCustomer(int id); }

  16. 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; } }

  17. 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(); } }

  18. 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(); } }

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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.

  24. Architecture • Marshalling • Marshalling By Reference (MBR) • Marshalling By Value (MBV) • Activation by MBR • Well-Known Object (WKO) • Singleton • SingleCall • Client-Activated Object (CAO)

  25. 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.

  26. 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.

  27. 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.

  28. Architecture • Marshalling • Marshalling By Reference (MBR) • Marshalling By Value (MBV) • Activation by MBR • Well-Known Object (WKO) • Singleton • SingleCall • Client-Activated Object (CAO)

  29. 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.

  30. 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(){...} }

  31. 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(); }

  32. 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(); }

  33. 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

  34. 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

  35. 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);

More Related