Title: Introduction to .NET Remoting
1Introduction to .NET Remoting
Brent Rector Wise Owl Consulting, Inc. www.wiseo
wl.com
2Brent Rector www.wiseowl.com
- System software background
- Designed and written operating systems
- Designed simulation and modeling languages and
compilers
- Windows expert since Windows 1.x beta - 1985
- Wintellect instructor and speaker
- Conferences
- WinDev, GeekCruises, WinSummit, VSLive
- Publications
- ATL Internals Addison-Wesley
- Win32 Programming Addison-Wesley
- MSDN Magazine
3Motivation
- Application domain is a hard boundary
- Acts much like a unmanaged process
- Also includes process/machine boundaries
- No direct access to objects across boundary
- Remoting services required to access an object
across a hard boundary
4Non-remotable objects
- CLR objects cannot, by default, be used outside
their AppDomain
- No way to copy them
- No way to reference them
- Exception occurs when you try to pass an object
reference to a different domain
5Remotable objects
- Can request that instances of a class be
accessible outside original AppDomain
- Should a client get a copy of the object?
- Should a client get a reference (proxy) to the
original object
6Serializable objects
- When runtime can make a copy of the object, an
object can marshal-by-value to other AppDomains
- Add SerializableAttribute
- Or implement the ISerializable interface
- Clients in foreign AppDomains receive clone
Serializable Class Foo . . .
7MarshalByRef objects
- When the object's class derives, directly or
indirectly, from MarshalByRefObject, the runtime
creates a proxy to the object
- Clients in foreign AppDomains receive proxy
- How does a client specify what proxy?
Serializable class Foo System.MarshalByRefObj
ect // MBRO overrides . . .
// Serializable
8Remoting in general
- Clients must connect to servers
- Server needs to publish an object
- I listen on this TCP channel and that HTTP
channel
- I have a service called "MathService"
- When client connects to MathService using a
supported channel, give the client a this
Calculator instance
- Clients need to specify the desired object
- Connect to the "MathService" on server
"LightningFast" using protocol HTTP on port 80
9Writing a server
- Assumption You have an assembly containing
MarshalByRefObject types you wish to make
available for use via remoting
- A server then becomes simply a host app
- Loads the managed types
- Configures remoting to expose the types
- Can write your own host
- Can use IIS as an already available HTTP host
10Server design and implementation
- Design considerations
- Decide which channels/ports to support
- Select an activation model
- Decide how clients get type metadata
- Implementation
- Select/write appropriate host
- Configure remoting system activation mode
- Register channels
11Channels
- A channel transports messages to and from a
remote object
- A server selects the channels upon which it
listens for requests
- A client selects the channels it wishes to use to
communicate with the server
- Runtime provides built-in channel types
- HTTP and TCP channels
- You can write custom channels
12ChannelServices.RegisterChannel
using System.Runtime.Remoting
using System.Runtime.Remoting.Channels
using System.Runtime.Remoting.Channels.Http
using System.Runtime.Remoting.Channels.Tcp
. . . ChannelServices.RegisterChannel (new HttpCh
annel()) ChannelServices.RegisterChannel (new Tc
pChannel(4242))
13Activation requests
- Server also chooses how it wants to process
activations requests for a type
- Two forms of server activation
- WellKnownObjectMode.SingleCall
- WellKnownObjectMode.Singleton
- One form of client activation
- Client activated object (CAO)
14SingleCall objects
- Server's remoting layer creates one SingleCall
object per method call
- Each object services one and only one request
- Created on-demand as method calls arrive
- Lifetime limited to method call
- Useful in stateless applications
- Best choice for load-balanced applications
15Singleton objects
- Server's remoting layer creates one Singleton
object
- Sole object handles method calls from all
clients
- Lifetime equals server lifetime
- Useful in stateful applications
- Can only store client-independent state
- Best choice where overhead of creating and
maintaining objects is substantial
16Well-known objects
- Server activated types are "well-known"
- Server tells remoting
- Here's a type
- Here's how and when to instantiate the type
- Here's the name (end point) a client will use to
contact the type
- Clients tell remoting
- Connect to this server machine
- Get the (known) type at this end point (name)
17Well-known objects
- Server registers a well-known type using
RegisterWellKnownServiceType specifying
- The type being registered
- The end point name known to clients
- The activation model
18RegisterWellKnownServiceType
using System.Runtime.Remoting
. . . WellKnownServiceTypeEntry WKSTE new Wel
lKnownServiceTypeEntry(typeof(WiseOwl.Calculator),
"EphemeralCalc",
WellKnownObjectMode.Single
Call) RemotingConfiguration.Register
WellKnownServiceType(WKSTE)
19Server activation example
using System using System.Runtime.Remoting usi
ng System.Runtime.Remoting.Channels
using System.Runtime.Remoting.Channels.Http
using System.Runtime.Remoting.Channels.Tcp
class RemotingHost static void Main(string
args) RemotingConfiguration.ApplicationNa
me "WiseOwlMathService" WellKnownServiceTy
peEntry WKSTE new WellKnownServiceTypeEn
try(typeof(WiseOwl.Calculator),
"SharedCalc", W
ellKnownObjectMode.Singleton)
RemotingConfiguration.RegisterWellKnownServi
ceType(WKSTE) ChannelServices.RegisterChanne
l(new HttpChannel(9000)) ChannelServices.Reg
isterChannel(new TcpChannel(4242))
Console.ReadLine()
20Well-known object URLs
- Server-activated objects are published at a URL
- The URL is "well-known" to the client
- Such types are called well-known types
- The URL is called the well-known object URL
- When IIS is the server's host
- PossibleApplicationName becomes virtual dir name
- ObjectUri should end in ".rem" or ".soap"
- A TcpChannel requires the port number
ProtocolScheme//ComputerNamePort/PossibleApplica
tionName/ObjectUri
21Remoting config file
- All hard-coded remoting information in prior
example can reside in external config file
- Default filename is executable plus ".config"
- E.g. RuntimeHost.exe is RuntimeHost.exe.config
- Host must tell remoting to use the config file
- RemotingConfiguration.Configure (file)
- Server code simplifies
22Server remoting config file
ll" type"WiseOwl.Calculator,MathObjects"
objectUri "EphemeralCalc"
/ Owl.Calculator,MathObjects" ob
jectUri "SharedCalc" /
p" /
runtime.remoting
23Revised Server Example
using System using System.Reflection using Sys
tem.Runtime.Remoting class RemotingHost s
tatic void Main(string args)
String s Assembly.GetExecutingAssembly().Loc
ation RemotingConfiguration.Configure (s "
.config") Console.ReadLine()
24Client activated objects
- Server activated is one activation model
- Client activated is quite different
- Each client activation creates one object
- Object's lifetime extends until the earliest
event
- Client drops reference to object
- Object's lease expires
- Similar to COM coclass activation semantics
- Can store per-client state, receive constructor
args
25Lease based lifetime
- Client activated object's lifetime controlled by
a lease on the object
- Leases have a lease time
- Remoting infrastructure drops reference to object
when lease expires
- Each method call renews the lease
- Use default renew on call time
- Clients can renew the lease using the proxy
- Sponsors can renew the lease
26Registering Client Activated Objs
- Server registers a client activated type using
RegisterActivatedServiceType
- Specifies the type being registered
using System.Runtime.Remoting
. . . ActivatedServiceTypeEntry ASTE new Acti
vatedServiceTypeEntry(typeof(WiseOwl.Calculator))
RemotingConfiguration.RegisterActivatedService
Type(ASTE)
27Client activation example
using System using System.Runtime.Remoting usi
ng System.Runtime.Remoting.Channels
using System.Runtime.Remoting.Channels.Http
using System.Runtime.Remoting.Channels.Tcp
class RemotingHost static void Main(string
args) RemotingConfiguration.ApplicationNa
me "WiseOwlMathService" ActivatedService
TypeEntry ASTE new ActivatedServiceTypeE
ntry(typeof(WiseOwl.Calculator))
RemotingConfiguration.RegisterActivatedService
Type(ASTE ChannelServices.RegisterChannel(n
ew HttpChannel(9000)) ChannelServices.Regist
erChannel(new TcpChannel(4242))
Console.ReadLine()
28Client activation URLs
- Client activated objects do not need a unique URL
for each object
- PossibleApplicationName becomes virtual directory
name when hosted in IIS
- A TcpChannel requires the port number
ProtocolScheme//ComputerNamePort/PossibleApplica
tionName
29Remoting config file 2
- As before, all hard-coded remoting information
can reside in external config file
Calculator,MathObjects" /
tp" /
30Revised Server Example
- Server uses config file doesn't change!
using System using System.Reflection using Sys
tem.Runtime.Remoting class RemotingHost s
tatic void Main(string args)
String s Assembly.GetExecutingAssembly().Loc
ation RemotingConfiguration.Configure (s "
.config") Console.ReadLine()
31Remoting clients
- A clients wants to use a remote object
- Well-known objects exist at a URL
- Client obtains proxy using Activator.GetObject
- Client can also obtain proxy using new
- Client activated object factory exists at a URL
- Client requests factory to instantiate object and
return a proxy to it using Activator.CreateInstanc
e
- Client can also make same request using new
32Activator.GetObject
- GetObject returns a proxy for the well-known type
served at the specified location
- No network traffic until first method call!
- Proxy built on client from metadata
- Server activates object on first method call
Dim o as Object Activator.GetObject (
GetType (WiseOwl.Calculator),
"http//localhost9000/WiseOwlMathServ
ice/SharedCalc") WiseOwl.Calculator c CType (
o, WiseOwl.Calculator) c.Add (42)
33RemotingServices.Connect
- GetObject is a thin wrapper over
System.Runtime.Remoting.RemotingServices.Connect
- Which calls System.Runtime.Remoting.RemotingServi
ces.Unmarshal
Using System.Runtime.Remoting
static object o RemotingServices.Connect (
Type classToProxy,
string url)
34RemotingServices.Unmarshal
- RemotingServices.Unmarshal does the real work
- Checks that type is MarshalByRef or Interface
- Find or creates identity based on URI
- Creates envoy and channel sink chains
- Gets or creates transparent and real proxy
- End results is client gets a proxy
35Activator.CreateInstance
- Client activation requires a network round trip
- Send activation message to server
- Can include constructor parameters
- Server creates object using specified
constructor
- Server builds ObjRef about new instance
- Server sends message with ObjRef to client
- Client creates proxy from ObjRef
36Activator.CreateInstance
- CreateInstance returns a proxy to a new instance
of the type served at the specified location
Dim activationAttributes() As Object New _
Activation.UrlAttribute("http//localhost9000/Wis
eOwlMathService") Dim o As Object
Activator.CreateInstance(GetType(WiseOwl.Cal
culator), Nothing, activationAttribute
s) WiseOwl.Calculator c CType (o, WiseOwl.Cal
culator) c.Add (42)
37Complete client application I
Imports System Imports System.Runtime.Remoting I
mports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Channels.Tcp
Class RemotingClient Shared Sub Main() '
Register channel(s) ChannelServices.Register
Channel(New HttpChannel()) ' Connect to a W
ell-known object Dim uriWKO As String "http
//localhost9000/WiseOwlMathService/SharedCalc"
Dim o As Object Activator.GetObject(GetType(
WiseOwl.Calculator), uriWKO) ' Use the Calc
ulator Dim c As WiseOwl.Calculator c C
Type(o, WiseOwl.Calculator) c.Add(21) En
d Sub End Class
38Complete client application II
Imports System Imports System.Runtime.Remoting I
mports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Channels.Tcp
Class RemotingClient Shared Sub Main() '
Register channel(s) ChannelServices.Register
Channel(New TcpChannel()) ' Create a client
activated object Dim url As String "tcp//
localhost4242/WiseOwlMathService"
Dim activationAttributes() As Object New
Activation.UrlAttribute(url)
Dim o As Object Activator.CreateInstance(Get
Type(WiseOwl.Calculator), _
Nothing,
activationAttributes) ' Use the Calculator
Dim c As WiseOwl.Calculator c CType(o, Wi
seOwl.Calculator) c.Add(21) End Sub End
Class
- Still need to information runtime of proper
channels to use
39Client's remoting config file
- As with a server, all client remoting information
can reside in external config file
- RemotingConfiguration.Configure (file)
MathService" displayName "W
iseOwlMathService"
seOwl.Calculator,MathObjects"/
athObjects" url"http//local
host9000/WiseOwlMathService/BadCalc"/
ntime.remoting
40Complete client application III
- All remoting information from config file
Class RemotingClient Shared Sub Main() ' U
se remoting configuration in file
Dim s As String System.Reflection.Assembly.G
etExecutingAssembly().Location
System.Runtime.Remoting.RemotingConfiguration.
Configure(s ".config") ' Use the Calc
ulator Dim c As WiseOwl.Calculator New Wise
Owl.Calculator() c.Add(21) ' Use the B
adCalculator Dim b As WiseOwl.BadCalculator
New WiseOwl.BadCalculator() b.Add(21) E
nd Sub End Class
41Default client channels
- Machine.config configuration file defines two
default, delay-loaded client channels
- HttpClientChannel and TcpClientChannel
- Must register explicit client channel to get
callbacks or use a non-default channel type
client (delay loaded)"
delayLoadAsClientCh
annel"true" / Name"tcp client (delay loaded)"
delayLoadAsClientChannel"true" /
. . . type"System.Runtime.Remoting.Channels.Http.Ht
tpClientChannel,System.Runtime.Remoting" /
emoting.Channels.Tcp.TcpClientChannel,
System.Runtime.Remoting" /
42How does it work?
- Client receives a proxy object when activating a
remote object
- Client "thinks" proxy is actual object
- Proxy is instance of TransparentProxy class
- Mimics inheritance hierarchy of remote object
- A call on the proxy
- passes control to actual object when in same
domain
- creates an IMessage to object in different
domain
- Passes message to a RealProxy instance
43RealProxy class
- RealProxy forwards msg to remote object
- Handles all method calls to remote object
- RealProxy class can be extended, customized
- For example, load balancing method calls, client
side validation/processing of certain calls
- TransparentProxy cannot be replaced
- Internal remoting implementation class
44IsTransparentProxy
- You can call IsTransparentProxy on any object
reference to see if it is a proxy
- Normally, you don't care
- How does TransparentProxy know about the remote
class?
using System.Runtime.Remoting
bool RemotingServices.IsTransparentProxy (Object
o)
45ObjRef
- Runtime creates an ObjRef when you register an
object with the remoting services
- An ObjRef contains all information required to
locate and access a remote object
- the strong name of the class
- the class's hierarchy (its parents)
- the names of all interfaces the class implements
- the object URI
- details of all available registered channels
46How to remote existing objects
- Server/client activation expose "new" objects
- What about existing objects?
- RemotingService.Marshal
- Accepts a MarshalByRefObject
- Registers it with remoting
- Returns an ObjRef
- RemotingService.Unmarshal
- Accepts an ObjRef
- Returns a proxy
47RemotingServices.Marshal
// // On the server, we find our hero about to go
public. . . WiseOwl.Calculator calc new WiseOw
l.Calculator () // Let the remoting layer (and
the world) know about it ObjRef or RemotingSer
vices.Marshal (calc, "ExplicitCalculator")
// Clients can now connect to the Calculator as
a Well-known object // prot//machineport/WiseOw
lMathServices/ExplicitCalculator
// Alternatively, we can serialize and send the
ObjRef to a client // System.Runtime.Serializati
on.Formatters.Soap.SoapFormatter sf
new System.Runtime.Serialization.Forma
tters.Soap.SoapFormatter () System.IO.FileStream
fs new System.IO.FileStream (_at_"C
\ObjRef.xml", System.IO.FileMode.Create)
sf.Serialize (fs, or) fs.Close ()
48RemotingServices.Unmarshal
' ' There's an ObjRef serialized to a file using
the SOAP formatter ' Get a formatter that can rea
d the format Dim sf As System.Runtime.Serializati
on.Formatters.Soap.SoapFormatter
sf New System.Runtime.Serialization.Formatte
rs.Soap.SoapFormatter() ' Open a stream on the
file Dim fs As System.IO.FileStream fs New
System.IO.FileStream("C\ObjRef.xml",
System.IO.FileMode.Open) ' Deserialize the obje
ct in the file Dim o As Object sf.Deserialize(f
s) fs.Close() ' Done with the file ' Object
is really an ObjRef but deserializes as the
referenced type Dim c As WiseOwl.Calculator CTy
pe(o, WiseOwl.Calculator) ' Use the Calculator
c.Add(21)
49Client-side remoting
- Client calls method on TransparentProxy
- TransparentProxy builds message
- Stack frame to message
- TransparentProxy passes msg to RealProxy
- RealProxy forwards msg to envoy sink(s)
- Last envoy sink forwards msg to context sink(s)
- Last context sink forwards msg to channel
sink(s)
- Channel sinks turn message into a byte stream
- Last channel sink sends byte stream to server
50Server-side remoting
- Channel receives a stream of bytes
- Channel forwards stream to channel sink(s)
- Last channel sink converts stream into message
- Last channel sink forwards msg to context
sink(s)
- Last context sink forwards msg to server sink(s)
- Last server sink is stack frame sink
- Stack frame sink builds and issues call to
requested method
51Remoting chain
Envoy Sink
TxSink
Envoy Sink
TxSink
Envoy Sinks
ContextSinks
Formatter Sink
010110
Channel
Transport Sink
Channel Sink
Channel Sink
Client-side
Server-side
Channel
010110
Transport Sink
Channel Sink
Channel Sink
TxSink
TxSink
TxSink
TxSink
Serverobject
Stack Builder Sink
FormatterSink
ContextSinks
ServerSinks
52Summary
- A class will either not marshal, marshal by value
or marshal by reference across an AppDomain
boundary
- Three forms of activation
- Server activated singleton
- Server activated singlecall
- Client activated