NET Remoting
Download
Report
Transcript NET Remoting
Presentation 23
.NET Remoting Introduced
Outline
•
•
•
•
•
•
•
.NET Remoting strategies
Architecture
Remoting object types
Activation
Lifetime
Deployment
Example application
High-level introduction only!
Slide 2 af 16
Ingeniørhøjskolen i Århus
.NET Framework Remoting strategies
• Two .NET Remoting strategies:
– Web services for inter-business purposes
• Is heterogeneous across platforms and languages
• Supported by the .NET compact framework
• Relies primarily on HTTP/SOAP protocols, but may be adapted for any
type of protocol
• Heterogeneous
• Is slow when used with HTTP/SOAP
– .NET Remoting for intra-business purposes
• Is only heterogeneous across CLS languages
• Not supported by the .NET compact framework
• Relies on either HTTP/SOAP or TCP/Binary protocols, but may be
adapted for any type of protocol
• Is fast when used with TCP/Binary
• Only heterogeneity within .NET runtime
• In many ways very similar to Java RMI in Java 5
Slide 3 af 16
Ingeniørhøjskolen i Århus
Simplified .NET Remoting Architecture
The Remoting System wraps most of the marshalling/unmarshalling work for you – like Java/CORBA
Using the classic
Proxy pattern
Channel:
Takes a stream of
data and transports
it to another
computer or
process:
Default:
TcpChannel &
HttpChannel
Slide 4 af 16
Ingeniørhøjskolen i Århus
The Remoting Architecture
Proxy created
dynamically by the
CLR. Creates a
message to the
server
All server objects must be of
type MarshalByRefObject or an
descendant hereof
Dispatch to
server object
Serializes the message
into a stream (SOAP or
binary)
Deserializes the
message
Optional extra handling
Writes the
stream to the
wire, e.g. TCP
or HTTP
Developers are free to implement new channels or replace sink elements
Slide 5 af 16
Ingeniørhøjskolen i Århus
Remotable Objects in .NET Remoting
• Marshal-by-reference objects
– By-reference – no state is transferred
– MarshalByRefObject
– Corresponds to CORBA Interface IDL and Java RMI Remote
objects (UnicastRemote objects)
– Proxy created
• Marshal-by-value objects
– By-value – complete object is serialized and transferred
– Implements ISerializable or decorated with Serializable Attribute
[Serializable]
– Very similar to Java RMI Serializable objects
– Some similarity with CORBA valuetypes (Objects by Value)
Slide 6 af 16
Ingeniørhøjskolen i Århus
Activation
• All server objects needs to be activated before a client
proxy may access it
• Two types of activation
– Server Activation (SAO)
• Activated when first client request arrives at server
– Singleton: only one server instance for all clients
– Single-call: a new server object pr. client request
• Lifetime is defined by server
– Client Activation (CAO)
• Activated by the client with the CreateInstance method on the
Activator object
• Server object only associated with creating client
• Lifetime is controlled by client (using leases)
• Very different semantics than CORBA & RMI – closer to
Web services (application, session, request scope)
especially SAO
Slide 7 af 16
Ingeniørhøjskolen i Århus
Lifetime management
• .NET Remoting uses leases for lifetime management
– All server objects has a lifetime lease – a time-to-live
– Lease manager
• controls the server object leases
• If expired – check all sponsors (clients)
• performs garbage collection on server objects
• In DCOM
– reference counting & pinging
• In CORBA
– ORB vendor specific
– Often implemented as “time since last request”
• In Java RMI
– uses leases (similar to .NET Remoting). Clients auto-update lease at 50%
• Web services
– Toolkit specific (HTTP primitives: Application, Session, Request)
– Application scope = runs for-ever / singleton
Slide 8 af 16
Ingeniørhøjskolen i Århus
Configuration
• Configuration:
– Need to inform runtime which servers are available and
at which address (URL)
• Two types of configuration
– Programmatic (shown in example next)
– Configuration file
• Web.config (e.g. with IIS) or Machine.config
Slide 9 af 16
Ingeniørhøjskolen i Århus
Deployment
• Server objects may be deployed as:
–
–
–
–
Windows Form application
Windows Console application
Windows Service
Internet Information Server deployment
• no need for a server bootstrapping application
Slide 10 af 16
Ingeniørhøjskolen i Århus
Development Steps – Remoting vs. CORBA & Java RMI
CORBA
.NET
Remoting
Design
J2SE JDK
Start with Server
Interface Coding: JAVA
Server Stub
Generation
Interface
Definition
CORBA: IDL
CLS Interface
RMI: JAVA interface
Implicit stub gen.
CORBA: IDL
Client Stub
Generation
Java RMI: rmic
CLS (C# …)
C++, Java …
Server
Coding
Client
Coding
RMI: JAVA
CLS (C# …)
C++, Java …
Remoting
Configuration
with CLR
Server
Registration
ORB
rmiregistry
Slide 11 af 16
Ingeniørhøjskolen i Århus
Making the HelloWorld application
• Using Microsoft Visual Studio .NET
– may of course be done with .NET Framework alone
• Make Client & Server solutions
• Server:
–
–
–
–
IHelloWorld.cs interface
HelloWorld.cs class implementation
Server.cs class implementation for boot-strapping
Add Reference to assembly System.Runtime.Remoting
• Client
– Must add IHelloWorld.cs
– Client.cs class implementation
– Add Reference to assembly System.Runtime.Remoting
Slide 12 af 16
Ingeniørhøjskolen i Århus
The IHelloWorld interface
The “IDL” of .NET Remoting – similar to Java RMI
using System;
namespace RemotingHelloServer
{
// IHelloWorld is the interface for the HelloWorld server class.
// It is the interface that is shared across the Internet
public interface IHelloWorld
{
string sayHello(string name);
}
}
Slide 13 af 16
Ingeniørhøjskolen i Århus
HelloWorld Implementation code
using System;
using System.Runtime.Remoting;
namespace RemotingHelloServer
{
// HelloWorld is a server object that is available
// "by-reference". It contains a constructor and a the
// "sayHello" method taking a string parameter "name"
public class HelloWorld : MarshalByRefObject, IHelloWorld
{
private string greeting;
A remote object
“by-reference” that
implements the IHelloWorld
interface
public HelloWorld()
{
greeting = "OOMI Christmas greetings from the server to: ";
}
public string sayHello(string name)
{
return (greeting + name);
}
Implementing the
sayHello method
}
}
Like in Java RMI (& CORBA) – we need to have an implementation of the interface
Slide 14 af 16
Ingeniørhøjskolen i Århus
Server code – Console bootstrapping
using System;
Like in Java RMI (& CORBA) – we need some
using System.Runtime.Remoting;
bootstrapping code – a server process
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace RemotingHelloServer
This may become a Windows NT service or a simple
{
application, e.g. a console or Windows Form application
public class Server
{
[STAThread]
static void Main(string[] args)
{
//Create a TCP channel
Register the channel
TcpChannel theChannel = new TcpChannel(8085)
on port 8085
/* Register the channel so that clients can
* connect to the server */
ChannelServices.RegisterChannel(theChannel);
//Register the service on the channel
RemotingConfiguration.ApplicationName = "HelloWorld App";
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(HelloWorld),
"HelloWorld App",
WellKnownObjectMode.SingleCall);
Register the
object
/*Start the server and keep it running so that clients
* can connect to it. May be aborted by keypress */
System.Console.WriteLine("Press Enter to end this server process");
System.Console.Read();
}
}
}
Slide 15 af 16
Ingeniørhøjskolen i Århus
Client code – Console bootstrapping
… include all the Remoting stuff
namespace RemotingHelloClient
{
public class Client
{
[STAThread]
static void Main(string[] args)
{
TcpChannel theChannel = new TcpChannel();
ChannelServices.RegisterChannel(theChannel);
Optional (may be done implicitly)
/* Activate the server object. Activation will bring
* the server object to life, and create a proxy
* stub class of the HelloWorld. In fact, as this is a
* server-activated application, the call to the
* server is NOT performed now, but instead waits until the
* first request. It is thus the server who performs the
* activation. This is the "Lazy-activation pattern" known
* from e.g. CORBA */
IHelloWorld helloWorld = (IHelloWorld) Activator.GetObject(
typeof(RemotingHelloServer.IHelloWorld),
"tcp://localhost:8085/HelloWorld App");
Create Proxy
System.Console.WriteLine("Please enter your name and press Enter");
string name = System.Console.ReadLine();
//Make the call
string greeting = helloWorld.sayHello(name);
System.Console.WriteLine("We recieved from server: "+greeting);
System.Console.WriteLine("Press Enter to end");
System.Console.Read();
Call via Proxy object
}
}
}
Slide 16 af 16
Ingeniørhøjskolen i Århus