Download Presentation

Introduction to Remoting

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

3. Motivation 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

4. Non-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

5. Remotable 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

6. Serializable 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

7. MarshalByRef 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?

8. Remoting 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

9. Writing 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

11. Channels 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

12. ChannelServices.RegisterChannel

13. Activation 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)

14. SingleCall 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

16. Well-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)

17. Well-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

18. RegisterWellKnownServiceType

19. Server activation example

20. Well-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

25. Lease 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

28. Client 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

31. Remoting 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.CreateInstance
Client can also make same request using new

32. Activator.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

34. RemotingServices.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

42. How 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

44. IsTransparentProxy 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?

45. ObjRef 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

46. How 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

52. Summary 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