Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced .NET Programming II 10th Lecture

Similar presentations


Presentation on theme: "Advanced .NET Programming II 10th Lecture"— Presentation transcript:

1 Advanced .NET Programming II 10th Lecture
Pavel Ježek Some of the slides are based on University of Linz .NET presentations. © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License (

2 AppDomains

3 Remotable vs. Nonremotable
Nonremotable objects Do not provide any way to copied to or represented in another application Basically all objects that are not remotable Remotable objects Marshal-By-Value Objects Marshal-By-Reference Objects

4 Marshal-By-Reference Objects Overview
Must extend System.MarshalByRefObject class References are represented by ObjRef class (when an object is marshaled) – itself serializable Contains information about type and location (identified by URL) Proxy is created on client side – redirects calls to server Reference to remote object is got by new (needs to be preconfigured) or by System.Activator methods

5 Marshal-By-Reference Objects Activation
Server-activated objects Client proxy is created by calling new or Activator.GetObject() Creation of server instance is controlled by server Can be: Dynamically marshaled (activated independently on client requests) Singleton Single Call Client-activated objects Both client proxy and server instance are created when client calls new or Activator.CreateInstance()

6 Marshal-By-Reference Objects Activation
public class RemotingConfiguration { public static void Configure(string filename); public static void RegisterActivatedServiceType( Type type ); public static void RegisterActivatedClientType( Type type, string appUrl public static void RegisterWellKnownServiceType( string objectUri, WellKnownObjectMode mode public static void RegisterWellKnownClientType( string objectUrl } Reads Remoting configuration from a XML file Server: register client-activated class Client: register remote client-activated class Server: register server-activated class WellKnownObjectMode.SingleCall WellKnownObjectMode.Singleton Client: register remote server-activated class

7 Request/Reply Transport
In messages Messages are transported through Channels: HttpChannel TcpChannel IpcChannel (in .NET 2.0) Messages are created from requests by Formatters: SoapFormatter BinaryFormatter Messages, Channels, Formatters, etc. are classes – new ones can be created to implement user specific transports and protocols

8 Server-activated Objects: Client/Server Example ServiceClass.cs
using System; using System.Diagnostics; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; public class ServiceClass : MarshalByRefObject { private DateTime starttime; public ServiceClass(){ Console.WriteLine("A ServiceClass has been created."); starttime = DateTime.Now; } ~ServiceClass(){ Console.WriteLine("ServiceClass being collected after " + (new TimeSpan(DateTime.Now.Ticks - starttime.Ticks)).ToString() + " seconds."); public DateTime GetServerTime(){ Console.WriteLine("Time requested by client."); return DateTime.Now;

9 Server-activated Objects: Dynamic marshaling Server.cs
using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using System.Runtime.Remoting.Services; public class ServerProcess{ public static void Main(string[] Args){ TcpChannel channel = new TcpChannel(8080); ChannelServices.RegisterChannel(channel); ServiceClass service = new ServiceClass(); ObjRef obj = RemotingServices.Marshal(service, "TcpService"); Console.WriteLine("Press Enter to disconnect the object."); Console.ReadLine(); RemotingServices.Disconnect(service); }

10 Server-activated Objects Client.cs #1
using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; public class ClientProcess{ public static void Main(string[] Args){ ChannelServices.RegisterChannel(new TcpChannel()); ServiceClass service = (ServiceClass) Activator.GetObject( typeof(ServiceClass), "tcp://localhost:8080/TcpService“ ); Console.WriteLine("Server time: " + service.GetServerTime().ToLongTimeString()); }

11 Server-activated Objects Client.cs #2
using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; public class ClientProcess{ public static void Main(string[] Args){ ChannelServices.RegisterChannel(new TcpChannel()); WellKnownClientTypeEntry remotetype = new WellKnownClientTypeEntry( typeof(ServiceClass), "tcp://localhost:8080/TcpService“ ); RemotingConfiguration.RegisterWellKnownClientType(remotetype); ServiceClass service = new ServiceClass(); Console.WriteLine("Server time: " + service.GetServerTime().ToLongTimeString()); }

12 Server-activated Objects Singleton/SingleCall server
using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using System.Runtime.Remoting.Services; public class ServerProcess{ public static void Main(string[] Args){ TcpChannel channel = new TcpChannel(8080); ChannelServices.RegisterChannel(channel); RemotingConfiguration.RegisterWellKnownServiceType( typeof(ServiceClass), “TcpService", WellKnownObjectMode.Singleton ); Console.WriteLine("Press Enter to exit."); Console.ReadLine(); }

13 Request/Reply Transport
In messages Messages are transported through Channels: HttpChannel TcpChannel IpcChannel (in .NET 2.0) Messages are created from requests by Formatters: SoapFormatter BinaryFormatter Messages, Channels, Formatters, etc. are classes – new ones can be created to implement user specific transports and protocols

14 Marshal-By-Reference Objects Lifetime
Lifetime is NOT controlled by reference counting, but lifetime leases are used When creating object its new lease is created Managed by Lease Manager Lease has list of its sponsors – they are asked to renew lease ILease.Register(ISponsor), ILease.Unregister(ISponsor) TimeSpan ISponsor.Renewal(ILease) Lease properties – can be changed only during init of lease: InitialLeaseTime (5 minutes) – can be infinite CurrentLeaseTime RenewOnCallTime (2 minutes) SponsorshipTimeout (2 minutes) To define custom lease properties MarshalByRefObject.InitializeLifetimeService needs to be overriden (can return null reference to ILease).

15 Marshal-By-Reference Objects Lifetime – Lease renewal
Client can get lease of remote object and explicitly require lease renewal RemoteType obj = new RemoteType(); ILease lease = (ILease) RemotingServices.GetLifetimeService(obj); TimeSpan expireTime = lease.Renew(TimeSpan.FromSeconds(20));

16 Chat Example ChatCoordinator.cs
using System; using System.Runtime.Remoting; using System.Collections; [Serializable] public class SubmitEventArgs : EventArgs { private string _string = null; private string _alias = null; public SubmitEventArgs(string contribution, string contributor) { this._string = contribution; this._alias = contributor; } public string Contribution{ get{ return _string; public string Contributor{ get { return _alias;

17 Chat Example ChatCoordinator.cs, cont.
public delegate void SubmissionEventHandler(object sender, SubmitEventArgs submitArgs); public class ChatCoordinator : MarshalByRefObject { public ChatCoordinator(){ Console.WriteLine("ChatCoordinator created."); } public override object InitializeLifetimeService(){ return null; public event SubmissionEventHandler Submission; public void Submit(string contribution, string contributor){ Console.WriteLine("{0} sent: {1}.", contributor, contribution); SubmitEventArgs e = new SubmitEventArgs(contribution, contributor); if (Submission != null){ Console.WriteLine("Broadcasting..."); Submission(this, e);

18 Chat Example ChatCentral.cs
using System; using System.Runtime.Remoting; public class ServerProcess{ // This simply keeps the ChatCoordinator application domain alive. public static void Main(string[] Args){ RemotingConfiguration.Configure("ChatCentral.exe.config"); Console.WriteLine("The host application is running. Press Enter to stop."); Console.ReadLine(); }

19 Chat Example ChatClient.cs
using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; public class ChatClient : MarshalByRefObject { private string _alias = null; public override object InitializeLifetimeService() { return null; } public ChatClient(string alias){ this._alias = alias; public static void Main(string[] Args){ if (Args.Length != 1){ Console.WriteLine("You need to type an alias."); return; } ChatClient client = new ChatClient(Args[0]); client.Run();

20 Chat Example ChatClient.cs, cont.
public void Run(){ RemotingConfiguration.Configure("ChatClient.exe.config"); // Create a proxy to the remote object. ChatCoordinator chatcenter = new ChatCoordinator(); chatcenter.Submission += new SubmissionEventHandler(this.SubmissionReceiver); String keyState = ""; while (true){ Console.WriteLine("Press 0 (zero) and ENTER to Exit:\r\n"); keyState = Console.ReadLine(); if (String.Compare(keyState,"0", true) == 0) break; chatcenter.Submit(keyState, _alias); } chatcenter.Submission -= new SubmissionEventHandler(this.SubmissionReceiver); public void SubmissionReceiver(object sender, SubmitEventArgs args){ if (String.Compare(args.Contributor, _alias, true) == 0){ Console.WriteLine("Your message was broadcast."); } else Console.WriteLine(args.Contributor + " says: " + args.Contribution);

21 Chat Example ChatCentral.exe.config
<configuration> <system.runtime.remoting> <application> <service> <wellknown mode="Singleton" type="ChatCoordinator, ChatCoordinator" objectUri="Chat" /> </service> <channels> <channel ref=“http" port="8080" </channels> </application> </system.runtime.remoting> </configuration>

22 Chat Example ChatClient.exe.config
<configuration> <system.runtime.remoting> <application> <client> <wellknown type="ChatCoordinator, ChatCoordinator" url=“ /> </client> <channels> <channel ref=“http" port="0" </channels> </application> </system.runtime.remoting> </configuration>

23 Chat Example Machine.config
Default location: %SystemRoot%\Microsoft .NET\Framework\<version>\Config <configuration> <system.runtime.remoting> <channels> <channel id="http" type="System.Runtime.Remoting.Channels.Http.HttpChannel, System.Runtime.Remoting, Version= , Culture=neutral, PublicKeyToken=b77a5c561934e089"/> <channel id="tcp" type="System.Runtime.Remoting.Channels.Tcp.TcpChannel, System.Runtime.Remoting, Version= , Culture=neutral, PublicKeyToken=b77a5c561934e089"/> </channels> </system.runtime.remoting> </configuration>


Download ppt "Advanced .NET Programming II 10th Lecture"

Similar presentations


Ads by Google