Componentes .net
Johann Oberleitner Zürich 2007
Goal:
◦ Develop a Sample Application for .NET ◦ Let the application use .NET Remoting
.NET Remoting
Comparable to Java RMI
◦ more flexible ◦ more extensible
Example
AppDomain (on one host)
ICalculator
Client
CalculatorImpl
Example
public interface ICalculator { int Add(int a, int b); // a+b int Sub(int a, int b); //a-c } public class CalculatorImpl: ICalculator { int Add(int a, int b) { return a+b; } int Sub(int a, int b) { return a-b; } }
Make Example Remoteable
AppDomain (on one host) AppDomain (on other host)
ICalculator
Client
CalculatorImpl
Example
public Client { ICalculator calculator; public void Init() { calculator = new CalculatorImpl(); } public void Testcalcs() { int a=3, b=5;Console.Out.WriteLine("{0}+{1}={2}",a, b, calculator.Add(a,b)); } }
Make Calculator Example Remote
public class CalculatorImpl: MarshalByRefObject, ICalculator { … // unchanged }
Make Example Remoteable
Server requires application that
◦ Registers available Remoteable classes
Only done once per server
Client registration
Client needs to get informed of services
◦ Once per client ◦Programmatically or via configuration file
◦ Stays in memory
Eg. Waits until user hits a key on server machine
Example - Server
public class Server { static void Main(string[] args) { HttpChannel myChannel = new HttpChannel(3333); ChannelServices.RegisterChannel (myChannel); RemotingConfiguration.RegisterWellKnownServiceType( typeof(CalculatorImpl), "MyCalculator",WellKnownObjectMode.Singleton); System.Console.WriteLine("Press Enter to exit!"); System.Console.ReadLine(); } }
Example - Client
public class Client { static void Main(string[] args) { HttpChannel myChannel = new HttpChannel(0); ChannelServices.RegisterChannel (myChannel); WellKnownClientTypeEntry remoteType = new WellKnownClientTypeEntry( typeof(CalculatorImpl), "http://localhost:4000/MyCalculator"); … // as before} }
Alternatives for Server
Instead of programmatic registering
◦ Loading registration from a Remoting Configuration file (XML) ◦ Can also be done at the client
.NET Application Domains
Application Domain like a logical process
◦ cannot access directly data (objects) in another application domain
.NET Remoting has to be used for communication across application domains
Same hostDifferent host
Instead of Waiting for a key
◦ Implement Calculator as a Windows Service started at system startup-time ◦ .NET Remoting Services may be hosted in Internet Information Server (IIS)
Restricted to HTTP transformation model
◦ Relevant when object references are transfered
.NET Contexts
◦ Similar to Application Domain but at smaller granularity ◦ Objects extendsContextBoundObject ◦ Special treatment when context is entered/left on calls
.NET Remoting
Microsoft's technology for distributed object communication in .NET
◦ ◦ ◦ ◦ Very flexible Very extensible No relation to COM/DCOM(!) Simple
.NET Remoting Object Types - Server activated
Singleton
◦ No more than one instance can be active
Implicitly created on first client call
◦ Can maintain state betweenmethod calls ◦ Multiple clients may be supported via multithreading ◦ Lifetime management
Single-Call
◦ New instance for every invocation of a method call ◦ After method call instance will be recycled ◦ No state may be maintained between calls
Stateless programming model improves scalability
◦ In theory load balancing possible
.NET Remoting Object Types - Client activatedClient-Activated
◦ Instance is created by client ◦ Instance reference is maintained by client
Client maintains a proxy Each instance is Referred to by a unique URI Server runs still on server
Object Lifetime
Lifetime management of .NET remoting objects based on leases
◦ Lease time (default 5minutes) expires
Object gets garbage collected
◦ When client makes a call lease gets increased (default...
Regístrate para leer el documento completo.