1 Nordjyllands Erhvervakademi 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 Nordjyllands Erhvervakademi Architecture Marshalling –Marshalling By Reference(MBR) –Marshalling By Value(MBV) Activation by MBR –Well-Known Object(WKO) Singleton SingleCall –Client-Activated Object(CAO)
3 Nordjyllands Erhvervakademi Why remoting? 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 Server.exe (.NET) Client.exe (.NET)
4 Nordjyllands Erhvervakademi 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 DB DTBT Client (.NET) Server (.NET) Business Tier Data Access Tier
5 Nordjyllands Erhvervakademi Remoting seen from the client and 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: 1.Runs as a service on the server, that responds on remote calls 2.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 Client.exe.DLL Server
6 Nordjyllands Erhvervakademi In principle
7 Nordjyllands Erhvervakademi Proxy pattern
8 Nordjyllands Erhvervakademi Design, more detailed Business and calculation objects lives on the server Data objects marshals to the clients Client Data call ProxyDataStubComp Client DataProxy Server call DataStubComp
9 Nordjyllands Erhvervakademi Architecture Marshalling –Marshalling By Reference(MBR) –Marshalling By Value(MBV) Activation by MBR –Well-Known Object(WKO) Singleton SingleCall –Client-Activated Object(CAO)
10 Nordjyllands Erhvervakademi 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 Nordjyllands Erhvervakademi 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 Nordjyllands Erhvervakademi Eksample.... of MBR and MBV
13 Nordjyllands Erhvervakademi Procedure: Steps: 1.Mark data objects as serializable [Serializable] 2.Define the remote interface (which services are possible) 3.The remote object have to –implement the remote interface –inherit from MarshalByRefObject –have a default constructor (a parameter less constructor) 4.On the server side: 1.Create and register a channel type 2.Register the remote service 5.On the client side: 1.Register server connection 2.Create a reference to the remote object Note: The interface but not the implementing class should be available on the client.
14 Nordjyllands Erhvervakademi 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 Nordjyllands Erhvervakademi Step 2: Interface public interface ICustomerManager { Customer GetCustomer(int id); }
16 Nordjyllands Erhvervakademi 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 Nordjyllands Erhvervakademi 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 Nordjyllands Erhvervakademi 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 Nordjyllands Erhvervakademi Step 5: Implement the client Set up the socket static void Main(string[] args) { try { HttpChannel channel = new HttpChannel(); ChannelServices.RegisterChannel(channel); ICustomerManager mgr = (ICustomerManager)Activator.GetObject( typeof(ICustomerManager), " 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); }
20 Nordjyllands Erhvervakademi Step 5: Implement the client Get an ICustomerManager object from the given url. Note that only the interface is given as the type static void Main(string[] args) { try { HttpChannel channel = new HttpChannel(); ChannelServices.RegisterChannel(channel, false); ICustomerManager mgr = (ICustomerManager)Activator.GetObject( typeof(ICustomerManager), " 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); }
21 Nordjyllands Erhvervakademi Step 5: Implement the client Get a copy of the Customer object from the server. Note that the class is necessary static void Main(string[] args) { try { HttpChannel channel = new HttpChannel(); ChannelServices.RegisterChannel(channel, false); ICustomerManager mgr = (ICustomerManager)Activator.GetObject( typeof(ICustomerManager), " 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); }
22 Nordjyllands Erhvervakademi What is happening? A stub is generated for both server and client The stub is responsible for sending messages by a socket a proxy classOn 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: 1.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 2.The client's stub sends the request to the server's stub. 3.The server's stub calls the relevant method in remote object. 4.And sends the return value back
23 Nordjyllands Erhvervakademi 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 Nordjyllands Erhvervakademi Architecture Marshalling –Marshalling By Reference(MBR) –Marshalling By Value(MBV) Activation by MBR –Well-Known Object(WKO) Singleton SingleCall –Client-Activated Object(CAO)
25 Nordjyllands Erhvervakademi 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 Nordjyllands Erhvervakademi 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 Nordjyllands Erhvervakademi 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 Nordjyllands Erhvervakademi Architecture Marshalling –Marshalling By Reference(MBR) –Marshalling By Value(MBV) Activation by MBR –Well-Known Object(WKO) Singleton SingleCall –Client-Activated Object(CAO)
29 Nordjyllands Erhvervakademi 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 Nordjyllands Erhvervakademi 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 Nordjyllands Erhvervakademi 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 Nordjyllands Erhvervakademi 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 Nordjyllands Erhvervakademi Client Register the CAO. Note that the class MyRemoteObject is used static void Main(string[] args) { HttpChannel channel = new HttpChannel(); ChannelServices.RegisterChannel(channel); RemotingConfiguration.RegisterActivatedClientType( typeof(Server.MyRemoteObject), " 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(); }
34 Nordjyllands Erhvervakademi Client Make a new instance using new static void Main(string[] args) { HttpChannel channel = new HttpChannel(); ChannelServices.RegisterChannel(channel); RemotingConfiguration.RegisterActivatedClientType( typeof(Server.MyRemoteObject), " 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(); }
35 Nordjyllands Erhvervakademi 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);