Vous êtes sur la page 1sur 39

C# and Windows

Programming
Application Domains and Remoting
2
Contents

Application Domains

Remoting
3
Processes

A process is a separate program running


under the control of the operating system

A process

Has its own private memory

s scheduled to run directly !y the operating


system

Cannot access memory not allocated to the


process
"
#hreads

#hreads are often referred to as


lightweight processes

#hreads e$ist within a process and can


run concurrently

#hey differ from processes in that all


threads in a process share the memory of
the process
%
Application Domains

#he application domain provides a new way to


split a process into parts

&ach application domain can run a separate


application within the same process

An application domain cannot access the


resources of another application conte$t

'ne application domain can crash and the


others will !e unaffected

&ach application domain has its own threads


(
Application Domains
Application
Domain )
Application
Domain 2
Application
Domain 3
Process 2*+,
#hread
-
Application Domains

When we start an application. we can create


new application domains

We can then load an assem!ly into each


application domain and run it

#he default application domain can then stop


and start the applications in the other domains

&ach domain can also have its own security in


effect
,
Remote Communication

Remote communication must !e used to


communicate !etween

two processes on the same machine

#wo application domains within the same


process

#wo processes on different computers


+
nterprocess Communication

When two processes are on the same machine.


they have options as to how they can
communicate

/hared memory

0emory outside each process which can !e accessed !y


each process

Pipes

Data streams !etween processes which usually run through


!uffers owned !y the operating system

nternet

1sing the internet to communicate !etween processes


)*
Remote Procedure Calls

/ending !ytes !etween processes is a


primitive a!ility and most applications
need more than this

#his forces applications to define protocols


to define meaning for the !yte streams

#his effort can !e saved if a useful


protocol is defined in advance
))
Remote Procedure Calls

#he remote procedure call provides a pre2


defined protocol that e$actly matches the
method calls of a programming language

RPC

Allows a method on one computer to invo3e a method


on another computer

Parameters can !e transmitted and results returned

#he developer is !arely aware that the procedure


!eing invo3ed is not in the local process memory
)2
#he RPC 4andscape

R0

5ava Remote 0ethod nvocation

5ava only

C'R6A

Common '!7ect Re8uest 6ro3er

4anguage independent

/'AP

/imple '!7ect Access Protocol

4anguage independent

9:&# Remoting

9:&# native RPC

9:&# only at the moment


)3
Pro$ies

A pro$y is something which stands in place of


something else

n RPC. pro$ies are used to represent remote


o!7ects

A pro$y must

mplement the same interface as the remote o!7ect

mplement each method to pac3age the method call.


send it across a networ3. and return the response

t must do this transparently


)"
Pro$ies
4ocal Class
Pro$y
Remote nterface
/in3
Remote
'!7ect
implements
:etwor3
0ethod
call
0ethod
call
/eriali;ed Data
<A pro$y has the same interface as the remote o!7ect
<t turns a method call into seriali;ed data and sends
the data across the net to the remote o!7ect
)%
0arshaling !y =alue

'!7ects cannot 7ust !e sent across a


networ3

#hey must !e turned into a data stream for


transmission

#he data stream consists of

#he class or type of the o!7ect

#he types of the fields

#he data in the fields


)(
0arshaling !y =alue

When method parameters are sent across


a networ3. they are usually seriali;ed

/eriali;ation turns a primitive or o!7ect into


a data stream which can !e transmitted
across a serial connection

Deseriali;ation at the receiving end can


turn the seriali;ed stream !ac3 into an
o!7ect with the same values as the original
)-
0arshaling !y Reference

0arshaling !y value sends a copy of an o!7ect


across a networ3

#here is another way to access an o!7ect across


a networ3 > marshal !y reference

0arshall !y reference sends a pro$y for the


o!7ect across the net

#his loo3s li3e the real o!7ect. !ut it ma3es


networ3 calls to the real o!7ect when you as3 it
to do anything
),
#ransmission Protocols

At present. remoting supports two


protocols

H##P

#he data is seriali;ed into a te$tual format called


the /imple '!7ect Access Protocol and transmitted
via the we!

#he advantage is that many companies close all


ports e$cept port ,* for security reasons and this
might !e the only way to transport re8uests
)+
#ransmission Protocols

#CP?P

#his seriali;es into a !inary stream transported


over #CP?P soc3ets

t re8uires the use of ports other than ,*

/ince the stream is !inary. it is more compact and


efficient than the te$tual stream used !y /'AP
2*
/imple '!7ect Access Protocol

/'AP is a te$tual form of RPC

t represents all the information for a


remote procedure call as an @04
document

Any o!7ects which have to !e passed are


also represented as @04 documents

/'AP is a surprisingly comple$ format


2)
Clients and /ervers

Remote o!7ects listen for re8uests from clients in


the form of method calls

n this way. a remote o!7ect acts as a server and


the program calling the remote method is the
client

n many cases. one remote o!7ect will call


another and they will alternately !ecome clients
and servers as the e$change continues
22
Pu!lishing

How do you find a remote o!7ectA

When a remote o!7ect is created. it


pu!lishes its location in a directory

t selects a name and !inds its internet


address and pro$y to the name

#he client then as3s the server for the


remote o!7ect and is given a pro$y for the
o!7ect
23
B Daniel 0eng
2"
Remoting Components

Pro$ies

Have the same interface as a remote o!7ect

0arshal the calls and transmit them across a networ3

#ransparent pro$y

#his presents the remote interface and is what is called !y


the client

Real pro$y

#his is called !y the transparent pro$y and marshals the data


2%
Remoting Components

Cormatters

#ranslates an o!7ect into its seriali;ed form

/in3s

#hese are components which process the messages

#hey are in a chain which can !e e$tended

#hey can

&nforce security

Act as formatters

&ncrypt the messages


2(
Remoting Components

Channels

#he channel is the part that is responsi!le for


the transport of the messages

#his is either an H##P channel or a #CP?P


channel

Custom /in3s

Dou can create custom sin3s to do additional


processing of the messages
2-
Remote '!7ect 4ifetime

When you create a remote o!7ect you can select


!etween

/ingle Call

&very call to the o!7ect generates a new instance of the


o!7ect which handles the re8uest and then dies

#his is suita!le if each call should have its own data and
should have no memory of any other calls

#his ma3es the remote o!7ect stateless


2,
Remote '!7ect 4ifetime

/ingleton

f you select this. then all calls to the remote o!7ect


are handled !y the same o!7ect

#here is only a single copy of the o!7ect

Data can !e stored in the o!7ect and it will !e


availa!le to the ne$t call to the o!7ect

#his ma3es the o!7ect stateful


2+
&$ampleE Remote Phone Directory

We will now loo3 at a simple e$ample of a


remote phone directory consisting of

PhoneDirectory

A remote o!7ect acting as a directory

Phonenfo

A seriali;a!le class used to return the phone information on a


person

Client

A command line application which loo3s up phone


information and displays the results
3*
/tep )E nterfaces

Cirst. create an interface for the remote


o!7ect
pu!lic interface PhoneDirectory
F
Phonenfo GetPhonenfoHstring nameIJ
K
3)
/tep )E nterfaces

Dou might also want to ma3e an interface for the


seriali;a!le classes
pu!lic interface Phonenfo
F
string :ame F getJK
string Address F getJK
string Phone F getJK
string #o/tringHIJ
K
32
/tep 2E Remote '!7ect

#his must !e accessed !y reference

#herefore. it must e$tend 0arshal6yRef'!7ect


class PhoneDirectory E 0arshal6yRef'!7ect.
PhoneDirectory
F
Hashta!le phone#a!le L new Hashta!leHIJ
M
K
33
/tep 2E Remote '!7ect

#he constructor fills the phone dir


pu!lic PhoneDirectoryHI
F
phone#a!leNOCredOP L new PhonenfoHOCred ClintstoneO. O++ Granite WayO. O")(2
23,2"3,-OIJ
phone#a!leNOWilmaOP L new PhonenfoHOWilma ClintstoneO. O++ Granite WayO.
O")(223,2"3,-OIJ
phone#a!leNO6arneyOP L new PhonenfoHO6arney Ru!!leO. O+- Granite WayO.
O")(223,2"3"3OIJ
phone#a!leNO6ettyOP L new PhonenfoHO6etty Ru!!leO. O+- Granite WayO. O")(2
23,2"3"3OIJ
phone#a!leNO6am 6amOP L new PhonenfoHO6am 6am ClintstoneO. O++ Granite
WayO. O")(223,2"3,-OIJ
K
3"
/tep 2E Remote '!7ect

Cinally. we have the method to return a


phone entry
pu!lic Phonenfo GetPhonenfoH/tring nameI
F
return HPhonenfoIphone#a!leNnamePJ
K
3%
/tep 3E /eriali;a!le Data

:ow. we ma3e the Phonenfo class seriali;a!le


N/eriali;a!leP
pu!lic class Phonenfo
F
/tring QnameJ
/tring QaddressJ
/tring Qphone:um!erJ
pu!lic PhonenfoHstring nm. string adr. string phI
F
Qname L nmJ
Qaddress L adrJ
Qphone:um!er L phJ
K
M
K
3(
/tep "E Create a /erver

#his is a 0ain method to create and pu!lish the remote


o!7ect
static void 0ainHstringNP argsI
F
#cp/erverChannel channel L new #cp/erverChannelH++++IJ
Channel/ervices9RegisterChannelHchannel. falseIJ
RemotingConfiguration9RegisterWellRnown/ervice#ypeH
typeof HPhoneDirectoryI. OPhoneDirectoryO.
WellRnown'!7ect0ode9/ingletonIJ
M
K
3-
/tep "E Create a /erver

Create channel on port ++++


#cp/erverChannel channel L new
#cp/erverChannelH++++IJ

Register channel without security


Channel/ervices9RegisterChannelHchannel. falseIJ

Create the remote o!7ect


RemotingConfiguration9RegisterWellRnown/ervice#ype
Htypeof HPhoneDirectoryI. OPhoneDirectoryO.
WellRnown'!7ect0ode9/ingletonIJ
3,
/tep %E Create a Client

Create a channel
Channel/ervices9RegisterChannelHnew #cpClientChannelHI. falseIJ

4ocate remote o!7ect


PhoneDirectory phoneDir L
HPhoneDirectoryIActivator9Get'!7ectH
typeofHPhoneDirectoryI.
OtcpE??localhostE++++?PhoneDirectoryOIJ

nvo3e a method
Phonenfo info L phoneDir9GetPhonenfoHSCredTIJ

B see remoteQdemo. remoteQdemoQclient

Vous aimerez peut-être aussi