NET Remoting with an easy example.docx
- 文档编号:26859520
- 上传时间:2023-06-23
- 格式:DOCX
- 页数:24
- 大小:34.14KB
NET Remoting with an easy example.docx
《NET Remoting with an easy example.docx》由会员分享,可在线阅读,更多相关《NET Remoting with an easy example.docx(24页珍藏版)》请在冰豆网上搜索。
NETRemotingwithaneasyexample
∙Downloadsourcefiles-16.6Kb
∙Downloaddemoproject-8.92Kb
Introduction
.NETRemotingisamechanismforcommunicatingbetweenobjectswhicharenotinthesameprocess.Itisagenericsystemfordifferentapplicationstocommunicatewithoneanother..NETobjectsareexposedtoremoteprocesses,thusallowinginterprocesscommunication.Theapplicationscanbelocatedonthesamecomputer,differentcomputersonthesamenetwork,oroncomputersacrossseparatenetworks.
Microsoft.NETRemotingprovidesaframeworkthatallowsobjectstointeractwitheachotheracrossapplicationdomains.Remotingwasdesignedinsuchawaythatithidesthemostdifficultaspectslikemanagingconnections,marshalingdata,andreadingandwritingXMLandSOAP.Theframeworkprovidesanumberofservices,includingobjectactivationandobjectlifetimesupport,aswellascommunicationchannelswhichareresponsiblefortransportingmessagestoandfromremoteapplications.
RemoteObjects
Whatareremoteobjects?
Anyobjectoutsidetheapplicationdomainofthecallerapplicationshouldbeconsideredremote,wheretheobjectwillbereconstructed.Localobjectsthatcannotbeserializedcannotbepassedtoadifferentapplicationdomain,andarethereforenonremotable.
AnyobjectcanbechangedintoaremoteobjectbyderivingitfromMarshalByRefObject,orbymakingitserializableeitherbyaddingthe[Serializable]tagorbyimplementingtheISerializableinterface.Whenaclientactivatesaremoteobject,itreceivesaproxytotheremoteobject.AlloperationsonthisproxyareappropriatelyindirectedtoenabletheRemotinginfrastructuretointerceptandforwardthecallsappropriately.Incaseswheretheproxyandremoteobjectsareindifferentapplicationdomains,allmethodcallparametersonthestackareconvertedintomessagesandtransportedtotheremoteapplicationdomain,wherethemessagesareturnedbackintoastackframeandthemethodcallisinvoked.Thesameprocedureisusedforreturningresultsfromthemethodcall.
Typesof.NETRemotableObjects
Therearethreetypesofobjectsthatcanbeconfiguredtoserveas.NETremoteobjects.Youcanchoosethetypeofobjectdependingontherequirementofyourapplication.
SingleCall
SingleCallobjectsserviceoneandonlyonerequestcomingin.SingleCallobjectsareusefulinscenarioswheretheobjectsarerequiredtodoafiniteamountofwork.SingleCallobjectsareusuallynotrequiredtostorestateinformation,andtheycannotholdstateinformationbetweenmethodcalls.
SingletonObjects
Singletonobjectsarethoseobjectsthatservicemultipleclients,andhencesharedatabystoringstateinformationbetweenclientinvocations.Theyareusefulincasesinwhichdataneedstobesharedexplicitlybetweenclients,andalsoinwhichtheoverheadofcreatingandmaintainingobjectsissubstantial.
Client-ActivatedObjects(CAO)
Client-activatedobjects(CAO)areserver-sideobjectsthatareactivateduponrequestfromtheclient.Whentheclientsubmitsarequestforaserverobjectusinga"new"operator,anactivationrequestmessageissenttotheremoteapplication.Theserverthencreatesaninstanceoftherequestedclass,andreturnsanObjRefbacktotheclientapplicationthatinvokedit.AproxyisthencreatedontheclientsideusingtheObjRef.Theclient'smethodcallswillbeexecutedontheproxy.Client-activatedobjectscanstorestateinformationbetweenmethodcallsforitsspecificclient,andnotacrossdifferentclientobjects.Eachinvocationof"new"returnsaproxytoanindependentinstanceoftheservertype.
Domains
In.NET,whenanapplicationisloadedinmemory,aprocessiscreated,andwithinthisprocess,anapplicationdomainiscreated.Theapplicationisactuallyloadedintheapplicationdomain.Ifthisapplicationcommunicateswithanotherapplication,ithastouseRemotingbecausetheotherapplicationwillhaveitsowndomain,andacrossdomains,objectcannotcommunicatedirectly.Differentapplicationdomainsmayexistinsameprocess,ortheymayexistindifferentprocesses.
Contexts
The.NETruntimefurtherdividestheapplicationdomainintocontexts.Acontextguaranteesthatacommonsetofconstraintsandusagesemanticsgovernallaccesstotheobjectswithinit.Allapplicationshaveadefaultcontextinwhichobjectsareconstructed,unlessotherwiseinstructed.Acontext,likeanapplicationdomain,formsa.NETRemotingboundary.Accessrequestsmustbemarshaledacrosscontexts.
Proxies
WhenacallismadebetweenobjectsinthesameApplicationDomain,onlyanormallocalcallisrequired;however,acallacrossApplicationDomainsrequiresaremotecall.Inordertofacilitatearemotecall,aproxyisintroducedbythe.NETframeworkattheclientside.ThisproxyisaninstanceoftheTransparentProxyclass,directlyavailabletotheclienttocommunicatewiththeremoteobject.Generally,aproxyobjectisanobjectthatactsinplaceofsomeotherobject.Theproxyobjectensuresthatallcallsmadeontheproxyareforwardedtothecorrectremoteobjectinstance.In.NETRemoting,theproxymanagesthemarshalingprocessandtheothertasksrequiredtomakecross-boundarycalls.The.NETRemotinginfrastructureautomaticallyhandlesthecreationandmanagementofproxies.
RealProxyandTransparentProxy
The.NETRemotingFrameworkusestwoproxyobjectstoaccomplishitsworkofmakingaremotecallfromaclientobjecttoaremoteserverobject:
aRealProxyobjectandaTransparentProxyobject.TheRealProxyobjectdoestheworkofactuallysendingmessagestotheremoteobjectandreceivingresponsemessagesfromtheremoteobject.TheTransparentProxyinteractswiththeclient,anddoestheworkofinterceptingtheremotemethodcallmadebytheclient.
Marshaling
ObjectMarshallingspecifieshowaremoteobjectisexposedtotheclientapplication.Itistheprocessofpackaginganobjectaccessrequestinoneapplicationdomainandpassingthatrequesttoanotherdomain.The.NETRemotinginfrastructuremanagestheentiremarshalingprocess.Therearetwomethodsbywhicharemoteobjectcanbemadeavailabletoalocalclientobject:
Marshalbyvalue,andMarshalbyreference.
Marshallingobjectsbyvalue
Marshalingbyvalueisanalogoustohavingacopyoftheserverobjectattheclient.Objectsthataremarshaledbyvaluearecreatedontheremoteserver,serializedintoastream,andtransmittedtotheclientwhereanexactcopyisreconstructed.Oncecopiedtothecaller'sapplicationdomain(bythemarshalingprocess),allmethodcallsandpropertyaccessesareexecutedentirelywithinthatdomain.
Marshallbyvaluehasseveralimplications;first,theentireremoteobjectistransmittedonthenetwork.Second,someortheentireremoteobjectmayhavenorelevanceoutsideofitslocalcontext.Forexample,theremoteobjectmayhaveaconnectiontoadatabase,orahandletoawindow,orafilehandleetc.Third,partsoftheremoteobjectmaynotbeserialiazable.Inaddition,whentheclientinvokesamethodonanMBVobject,thelocalmachinedoestheexecution,whichmeansthatthecompiledcode(remoteclass)hastobeavailabletotheclient.Becausetheobjectexistsentirelyinthecaller'sapplicationdomain,nostatechangestotheobjectarecommunicatedtotheoriginatingapplicationdomain,orfromtheoriginatorbacktothecaller.
MBVobjectsare,however,veryefficientiftheyaresmall,andprovidearepeatedfunctionthatdoesnotconsumebandwidth.Theentireobjectexistsinthecaller'sdomain,sothereisnoneedtomarshalaccessesacrossdomainboundaries.Usingmarshal-by-valueobjectscanincreaseperformanceandreducenetworktraffic,whenusedforsmallobjectsorobjectstowhichyouwillbemakingmanyaccesses.
Marshalbyvalueclassesmusteitherbemarkedwiththe[Serilaizable]attributeinordertousethedefaultserialization,ormustimplementtheISerializableinterface.
Marshallingobjectsbyreference
Marshalingbyreferenceisanalogoustohavingapointertotheobject.Marshalbyreferencepassesareferencetotheremoteobjectbacktotheclient.ThisreferenceisanObjRefclassthatcontainsalltheinformationrequiredtogeneratetheproxyobjectthatdoesthecommunicationwiththeactualremoteobject.Onthenetwork,onlyparametersandreturnvaluesarepassed.Aremotemethodinvocationrequirestheremoteobjecttocallitsmethodontheremotehost(server).
MarshalbyreferenceclassesmustinheritfromSystem.MarshalByRefObject.
Marshallingdonebythe.NETFramework
Marshallingisdonebythe.NETFrameworkitself.Wedon�thavetowriteanycodeforit.
Channels
The.NETRemotinginfrastructureprovidesamechanismbywhichastreamofbytesissentfromonepointtotheother(clienttoserveretc.).Thisisachievedviaachannel.Strictlyspeaking,itisaclassthatimplementstheIChannelinterface.Therearetwopre-defined.NETRemotingchannelsexistinginSystem.Runtime.Remoting.Channels,theTcpChannelandtheHttpChannel.TousetheTcpChannel,theservermustinstantiateandregistertheTcpServerChannelclass,andtheclient,theTcpClientChannelclass.
Channelselectionissubjecttothefollowingrules:
∙Atleastonechannelmustberegisteredwiththeremotingframeworkbeforearemoteobjectcanbecalled.Channelsmustberegisteredbeforeobjectsareregistered.
∙Channelsareregisteredperapplicationdomain.Therecanbemultipleapplicationdomainsinasingleprocess.Whenaprocessdies,allchannelsthatitregistersareautomaticallydestroyed.
∙Itisillegaltoregisterthesamechannelthatlistensonthesameportmorethanonce.Eventhoughchannelsareregisteredperapplicatio
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- NET Remoting with an easy example