Net Remoting
Net Remoting
NET REMOTING
Remoting
Communication of applications
Instantiating objects through remoting. Sharing data, making function calls Distributed Applications
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
Non-remotable objects
CLR objects cannot, by default, be used outside their AppDomain
Exception occurs when you try to pass an object reference to a different domain
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
Serializable objects
When runtime can make a copy of the object, an object can marshal-by-value to other AppDomains
MarshalByRef objects
When the object's class derives, directly or indirectly, from MarshalByRefObject, the runtime creates a proxy to the object
[Serializable] class Obj : System.MarshalByRefObject { . . . } // MBRO overrides // Serializable
Clients in foreign AppDomains receive proxy How does a client specify what proxy?
Remoting in general
Clients must connect to servers
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
Decide which channels/ports to support Select an activation model Decide how clients get type metadata Select/write appropriate host Configure remoting system activation mode Register channels
Implementation
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
Can write your own host Can use IIS as an already available HTTP host
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
Well-known objects
Server activated types are "well-known"
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 Connect to this server machine Get the (known) type at this end point (name)
Activation requests
Server also chooses how it wants to process activations requests for a type Two forms of server activation
SingleCall objects
Server's remoting layer creates one SingleCall object per method call
Singleton objects
Server's remoting layer creates one Singleton object
RegisterWellKnownService Type
using System.Runtime.Remoting; . . . WellKnownServiceTypeEntry WKSTE = new WellKnownServiceTypeEntry(typeof(SuperCalc.Calculator), "Calculator", WellKnownObjectMode.SingleCall);
RemotingConfiguration.RegisterWellKnownServiceType(WKSTE);
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 HTTP and TCP channels You can write custom channels
Registering Channels
System.Runtime.Remoting.Channels ns. Custom channels implement IChannel. ChannelServices class
ChannelServices.RegisterC hannel
using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; using System.Runtime.Remoting.Channels.Tcp; . . . ChannelServices.RegisterChannel (new HttpChannel()); ChannelServices.RegisterChannel (new TcpChannel(4242));
class RemotingHost { static void Main(string[] args) { RemotingConfiguration.ApplicationName = MathService"; WellKnownServiceTypeEntry WKSTE = new WellKnownServiceTypeEntry(typeof(SuperCalc.Calculator), "SharedCalc", WellKnownObjectMode.Singleton);
RemotingServices
Connect Disconnect Marshal Unmarshal GetLifeTimeService IsOneWay
Lifetime Management
The Ilease interface defines the current lifetime.
CurrentLeaseTime CurrentState InitialLeaseTime Register Renew UnRegister
MarshalByRefObject
Used to access objects across domains.
GetLifeTimeService InitializeLifeTimeService
Such types are called well-known types The URL is called the well-known object URL
ProtocolScheme://ComputerName:Port/PossibleApplicationName/ObjectUri
PossibleApplicationName becomes virtual dir name ObjectUri should end in ".rem" or ".soap"
Default filename is executable plus ".config" E.g. RuntimeHost.exe is RuntimeHost.exe.config RemotingConfiguration.Configure (file)
Remoting clients
A clients wants to use a remote object
Class RemotingClient Shared Sub Main() ' Register channel(s) ChannelServices.RegisterChannel(New HttpChannel()) ' Connect to a Well-known object Dim uriWKO As String = "http://localhost:9000/SuperCalcMathService/SharedCalc" Dim o As Object = Activator.GetObject(GetType(SuperCalc.Calculator), uriWKO) ' Use the Calculator Dim c As SuperCalc.Calculator c = CType(o, SuperCalc.Calculator) c.Add(21) End Sub End Class
Class RemotingClient Shared Sub Main() ' Register channel(s) ChannelServices.RegisterChannel(New TcpChannel())
' Create a client activated object Dim url As String = "tcp://localhost:4242/SuperCalcMathService" Dim activationAttributes() As Object = { New Activation.UrlAttribute(url) } Dim o As Object = Activator.CreateInstance(GetType(SuperCalc.Calculator), _ Nothing, activationAttributes) ' Use the Calculator Dim c As SuperCalc.Calculator c = CType(o, SuperCalc.Calculator) c.Add(21) End Sub End Class
RemotingConfiguration.Configure (file)
<configuration> <system.runtime.remoting> <application name="SuperCalcMathService"> <client url = "http://localhost:9000/SuperCalcMathService" displayName = "SuperCalcMathService"> <activated type = "SuperCalc.Calculator,MathObjects"/> <wellknown type = "SuperCalc.BadCalculator,MathObjects" url="http://localhost:9000/SuperCalcMathService/BadCalc"/> </client> </application> </system.runtime.remoting> </configuration>
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
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
WellKnownServiceEntry
Holds a remotable object registered on the service.
AssemblyName Mode ObjectUri
CallContext
...Messaging.CallContext Represents the context of a remoted object
SetData GetData GetHeaders SetHeaders
Others
OneWayAttribute
Automatic transactions Object pooling Just-in-time activation (JITA) Loosely-coupled events Etc.
Example:
Contexts
Contexts are the primary service providers
System.EnterpriseServices.ContextUtil class provides access to contexts as did CoGetObjectContext Rich set of static and member functions exposed by System.EnterpriseServices.ContextUtil
ContextUtil Class
Methods exposed by the ContextUtil class are:
Security
Access, authentication, and impersonation levels specified using attributes at the assembly level
[assembly:ApplicationAccessControl( AccessChecksLevel= AccessChecksLevelOption.ApplicationComponent, Authentication=AuthenticationOption.Default, ImpersonationLevel=ImpersonationLevelOption.Impersonate) ]
[ComponentAccessControl]
[InterfaceQueuing(Interface = IQueuedInterface"]
Object Pooling
Any .NET ServicedComponent could be pooled Pooling is made available through the ObjectPooling attribute [ObjectPooling(MinPoolSize=2, MaxPoolSize=5)] public class SVCCompClass : ServicedComponent
Expose end points and protocol using the Web.config file for the IIS VRoot.
<configuration> <system.runtime.remoting> <application> <service> <wellknown mode="SingleCall"type="SVCCompSoap.SVCCompSo apCls,SVCCompSoap objectUri="SVCCompSoap.soap"/> </service> </application> </system.runtime.remoting> </configuration>
[assembly:ApplicationActivation(ActivationOption .Server)]
[assembly: AssemblyKeyFile("..\\..\\SVCComp.snk")]