Net Remo Ting

download Net Remo Ting

of 47

  • date post

    30-Apr-2017
  • Category

    Documents

  • view

    216
  • download

    0

Embed Size (px)

Transcript of Net Remo Ting

  • Microsoft.NETRemoting

    Morten Dahldahl@cs.aau.dk

  • ApplicationDomains

    Traditionallyprocessesareusedtoisolateapplications

    .NETtypesystemguaranteeswellbehavedcode applicationscannotinterferewitheachother

    AppDomains insteadofprocesses Lessperformancecost(crossprocesscalls,switching)

  • ApplicationDomains

    Process1

    AppDomain 1A

    AppDomain 1B

    Process2

    AppDomain 2A

    AppDomain 2B

  • CrossAppDomain Communication

    .NETRemoting Clientservermodel Abstractionofnetwork Objectorientedinterface Distributedgarbagecollection Extendablesubsystem Serverhosting:standalone,IIS,orWindowsService Moreaprotocolthanaservicelayer(notransactionsetc.)

    BestpracticerecommendsusingRemoting for CrossAppDomain andCrossProcess LANapplications

  • ByValue andRemoteObjects

    ByValue (passbyvalue)objects objectiscopiedacrossAppDomain boundaries norelationshiptooriginalobject methodcallsexecutedlocally mustbeserialisable:implementISerializable interfaceor

    markedwith[serializable]attribute

    Remote(passbyreference)objects objectlivesatorigin methodcallsexecutedatorigin referencedbynetworkedpointer (ObjRef) accessedthroughtransparentproxy mustextendMarshalByRefObject

  • Remoting Overview

    Clientcode

    Subsystem

    Remoteobject

    SubsystemNetwork

    Proxy

    Channel

    FormatterSinks

    pointer

    ObjRef (=URL)

    pointer

    Client

    Server

  • KindsofRemoteObjects

    Serveractivated(SA) notassignedtospecificclient Singleton:frameworkguaranteesthatexactlyonecopyofobjectis

    exposed usefulforsharingdatabetweenclients

    SingleCall:frameworkcreatesanewobjectforeachmethodcall(destroyedonreturn)

    scalablesincenostateisstored Published:serverprogramcreatesobjectandexposesit

    Clientactivated(CA) createdonrequestbyclient eachobjecthasitsownstate(needforobjectidinObjRef) similartolocalobjectsonlystoredatserver

  • NetworkedPointers:ObjRefs

    Objectpointersuitablefornetwork ifpassedfromoneclienttoanotheritpointsdirectlytoserverandnotthroughfirstclient

    serialisable Serveractivatedobject

    ServerURL:http://server:8000/RemoteObj.rem Clientactivatedobject

    ServerURL:http://server:8000/RemoteObj.rem ObjectID(GUID)

  • Binding

    FirstcontactbyURL http://server:8000/Gatekeeper fromthenonwecanuseObjRefs

    Activator Objo=(Obj)Activator.GetObject(typeof(Obj),url);

    newoperator Objo=(Obj)newObj; URLmustbeboundtotypebeforehand cannotbeusedifallwehaveisinterface!

  • StorageExample

    Illustrating binding exposingremoteobjects(server) accessingremoteobjects(client) differencebetweenkinds

  • StorageExample:shared

    Remotestorageobject Holdsanintegervalue Getterandsetterforvalue ImplementsMarshalByRefObject (Integertypeisserialisable)

    interfaceIStorage{

    voidSetValue(intValue);intGetValue();

    }

    classStorage:MarshalByRefObject,IStorage

    {privateintvalue;

    publicvoidSetValue(intValue){this.value=Value;}

    publicintGetValue(){returnthis.value;}

    }

  • StorageExample:server(SA)

    HttpChannelchannel=newHttpChannel(8000);

    ChannelServices.RegisterChannel(channel,false);

    RemotingConfiguration

    .RegisterWellKnownServiceType(

    typeof(Storage),

    Storage.rem,

    WellKnownObjectMode.Singleton);

  • StorageExample:server(SA)

    HttpChannelchannel=newHttpChannel(8000);

    ChannelServices.RegisterChannel(channel,false);

    RemotingConfiguration

    .RegisterWellKnownServiceType(

    typeof(Storage),

    Storage.rem,

    WellKnownObjectMode.SingleCall);

  • StorageExample:server(SA)

    HttpChannelchannel=newHttpChannel(8000);ChannelServices.RegisterChannel(channel,false);

    Storagestore=newStorage();store.SetValue(42);

    RemotingServices.Marshal(store,Storage.rem);

  • StorageExample:client(SA)

    HttpChannelchannel=newHttpChannel();

    ChannelServices.RegisterChannel(channel,false);

    IStoragestore=(IStorage)Activator.GetObject(

    typeof(IStorage),

    http://server:8000/Storage.rem);

    //usestore asthoughlocalobject

  • StorageExample:server(CA)

    HttpChannelchannel=newHttpChannel(8000);ChannelServices.RegisterChannel(channel,false);

    RemotingConfiguration.ApplicationName="Storage.rem";

    RemotingConfiguration.RegisterActivatedServiceType(

    typeof(Storage));

  • StorageExample:client(CA)

    HttpChannelchannel=newHttpChannel();ChannelServices.RegisterChannel(channel,false);

    RemotingConfiguration.RegisterActivatedClientType(

    typeof(Storage),http://server:8000/Storage.rem);

    Storage storeA=(Storage)Activator.CreateInstance(typeof(Storage));Storage storeB=newStorage();

    //usestoreA andstoreB asthoughlocalobjects

  • Break

  • MethodCalls

    ObjectClient

    pointer

    methodcallbypointer

    Client

    pointer

    methodcallbypointer

    RemoteObject

    Proxy

    ObjRef(=URL)

    methodcallbysubsystem

    Network

  • RemoteMethodCall

    Clientcallsmethodonproxywithsameinterface;proxytransferscalltoserver

    InputandoutputparametersmustbeByValueorremoteobjects clientcanpotentiallybecomeserver

    Synchronously,asynchronously,oronewayasynchronously(fireandforget;noreturnvaluenorexceptions)

  • RemoteMethodCall

    Client

    TransparentProxy

    RealProxy

    Sinks

    Channel

    Remoteobject

    Dispatcher

    Sinks

    Channel

    MethodcallMethodcall

    Network

    Client Server

  • ClientsideMessaging

    TransparentProxy

    RealProxy

    ContextTerminator

    Formatter

    Methodcall

    Channel

    IMessageSink

    IClientFormatterSink(IMessageSink+IClientChannelSink)

    IClientChannelSink

    DynamicSinks

    Sinks

    Sinks

    Networkoutput

    IDynamicMessageSink

  • ClientsideMessaging

    TransparentProxy:sameinterfaceasremoteobject;convertsbetweenmethodcallandmessage

    Message:representsmethodcall(andparameters)viadictionary RealProxy:takesmessageandforwardsittosinks;holdsObjRef ContextTerminator/DynamicSinks:ContextTerminatornotifies

    dynamicsinksassociatedwithcontextofmethodcall Sinks:optionalmessageprocessing

    e.g.threadpriority Formatter:serialisationofmessageintostream Sinks:optionalstreamprocessing

    e.g.compression Channel:implementsnetworkprotocol(TCP,HTTP,custom)

  • ServersideMessaging

    ContextTerminator

    DynamicSinks

    CrossContext

    Formatter

    Networkinput

    Channel

    IMessageSink

    IServerChannelSink

    Dispatcher

    Sinks

    Sinks

    IDynamicMessageSink

    Lease ObjectTerminator Stackbuilder

    Methodcall

  • ServersideMessaging

    Channel:implementsnetworkprotocol(TCP,HTTP,custom)

    Sinks:optionalstreamprocessing e.g.decompression

    Formatter:deserialisationofstreamintomessage Sinks:optionalmessageprocessing

    e.g.threadpriority Dispatcher:checksfordisconnectedortimedoutobjects;

    createsSAOobjects CrossContext/DynamicSinks:notifiesdynamicsinks ContextTerminator/Lease:callsRenewOnCall oftarget

    objectslease ObjectTerminator/Stackbuilder:constructsstackframe

  • BuiltinComponents

    Formatters BinaryFormatter (SoapFormatter)

    Channels TcpChannel(authenticationandencryption) HttpChannel(authenticationandencryptioninIIS) IpcChannel(authentication)

    Customformattersandchannels

  • SinkExample

    Illustrating messagingsubsystem

    SoapFormatter+HttpChannel compressionchannelsink threadprioritymessagesink

    configurationfiles

  • SinkExample:shared

    Cartoonremoteobject Nameproperty Behavemethod

    onewayaswesuspectithavingnoeffectwhatsoever

    Sleep forwhenbehavingbecomestiresome

    publicclassCartoon:MarshalByRefObject{

    privatestringname;publicstringName{get{returnname;}

    set{name=value;}}

    [OneWay]publicvoidBehave(){ }

    publicintSleep(){

    intnapDuration=4000;Thread.Sleep(napDuration);returnnapDuration;

    }}

  • SinkExample:subsystem

    TransparentProxy

    RealProxy

    SoapFormatter

    Methodcall

    HttpChannel

    IMessageSink

    IClientChannelSink

    PrioritySink

    CompressionSink

    SoapFormatter

    HttpChannel

    IServerChannelSink

    PrioritySink

    CompressionSink

    Dispatcher

    Methodcall

    ...

    Cartoon

    Network

    ...

  • SinkExample:basicconfig HttpChannelchannel=newHttpChannel();

    ChannelServices.RegisterChannel(channel,false); Cartoonloony=(Cartoon)Activator.GetObject(typeof(Cartoon),

    "http://localhost:8000/Cartoon.rem");

  • SinkExample:fullconfig

  • SinkExample:sinks Compressionchannelsink

    compressesstreambeforesendonchannel

    decompressesstreamafterreceivedonchannel

    publicvoidProcessMessage(IMessagemsg,ITransportHeadersreqHeaders,)

    {1. requestStream=

    GetCompressedStream(requestStream);

    2. nextSink.ProcessMessage(msg,requestHeaders,);

    3. responseStream=GetDecompressedStream(responseStream);

    }

    Prioritymessagesink storesclientthreadpriorityin

    message givesserverthreadpriorityfrom

    message

    publicIMessageSyncProcessMessage(IMessagemsg)

    {1. LogicalCallContextlcc=

    msg.Properties["__CallContext"];

    2. lcc.SetData("Priority",Thread.CurrentThread.Priority);

    3. returnnextSink.SyncProcessMessage(msg);}

  • Break

  • DistributedGarbageCollection

    Erroroccursifclientaccessescollectedobject;whentogarbagecollect?

    Referencecounting clientcancrash

    Alive!pings notsuitableinallscenarios:fir