39
C# and Window s Programming Application Domains and Remoting

C# Application Domains and Remoting

Embed Size (px)

DESCRIPTION

C# is an object-oriented programming language used with XML-based Web services on the .NET platform and designed for improving productivity in the development of Web applications.

Citation preview

Page 1: C# Application Domains and Remoting

C# and Windows Programming

Application Domains and Remoting

Page 2: C# Application Domains and Remoting

2

Contents

Application Domains Remoting

Page 3: C# Application Domains and Remoting

3

Processes

A process is a separate program running under the control of the operating system

A processHas its own private memoryIs scheduled to run directly by the operating

systemCannot access memory not allocated to the

process

Page 4: C# Application Domains and Remoting

4

Threads

Threads are often referred to as lightweight processes

Threads exist within a process and can run concurrently

They differ from processes in that all threads in a process share the memory of the process

Page 5: C# Application Domains and Remoting

5

Application Domains

The application domain provides a new way to split a process into parts

Each application domain can run a separate application within the same process

An application domain cannot access the resources of another application context

One application domain can crash and the others will be unaffected

Each application domain has its own threads

Page 6: C# Application Domains and Remoting

6

Application Domains

Application Domain 1

ApplicationDomain 2

ApplicationDomain 3

Process 2098

Thread

Page 7: C# Application Domains and Remoting

7

Application Domains

When we start an application, we can create new application domains

We can then load an assembly into each application domain and run it

The default application domain can then stop and start the applications in the other domains

Each domain can also have its own security in effect

Page 8: C# Application Domains and Remoting

8

Remote Communication

Remote communication must be used to communicate betweentwo processes on the same machineTwo application domains within the same

processTwo processes on different computers

Page 9: C# Application Domains and Remoting

9

Interprocess Communication

When two processes are on the same machine, they have options as to how they can communicate Shared memory

Memory outside each process which can be accessed by each process

Pipes Data streams between processes which usually run through

buffers owned by the operating system Internet

Using the internet to communicate between processes

Page 10: C# Application Domains and Remoting

10

Remote Procedure Calls

Sending bytes between processes is a primitive ability and most applications need more than this

This forces applications to define protocols to define meaning for the byte streams

This effort can be saved if a useful protocol is defined in advance

Page 11: C# Application Domains and Remoting

11

Remote Procedure Calls

The remote procedure call provides a pre-defined protocol that exactly matches the method calls of a programming language

RPC Allows a method on one computer to invoke a method

on another computer Parameters can be transmitted and results returned The developer is barely aware that the procedure

being invoked is not in the local process memory

Page 12: C# Application Domains and Remoting

12

The RPC Landscape

RMI Java Remote Method Invocation Java only

CORBA Common Object Request Broker Language independent

SOAP Simple Object Access Protocol Language independent

.NET Remoting .NET native RPC .NET only at the moment

Page 13: C# Application Domains and Remoting

13

Proxies

A proxy is something which stands in place of something else

In RPC, proxies are used to represent remote objects

A proxy must Implement the same interface as the remote object Implement each method to package the method call,

send it across a network, and return the response It must do this transparently

Page 14: C# Application Domains and Remoting

14

Proxies

Local Class

Proxy

Remote Interface

Sink RemoteObject

implements

Network

Methodcall

Methodcall

Serialized Data

•A proxy has the same interface as the remote object•It turns a method call into serialized data and sends the data across the net to the remote object

Page 15: C# Application Domains and Remoting

15

Marshaling by Value

Objects cannot just be sent across a network

They must be turned into a data stream for transmission

The data stream consists ofThe class or type of the objectThe types of the fieldsThe data in the fields

Page 16: C# Application Domains and Remoting

16

Marshaling by Value

When method parameters are sent across a network, they are usually serialized

Serialization turns a primitive or object into a data stream which can be transmitted across a serial connection

Deserialization at the receiving end can turn the serialized stream back into an object with the same values as the original

Page 17: C# Application Domains and Remoting

17

Marshaling by Reference

Marshaling by value sends a copy of an object across a network

There is another way to access an object across a network – marshal by reference

Marshall by reference sends a proxy for the object across the net

This looks like the real object, but it makes network calls to the real object when you ask it to do anything

Page 18: C# Application Domains and Remoting

18

Transmission Protocols

At present, remoting supports two protocolsHTTP

The data is serialized into a textual format called the Simple Object Access Protocol and transmitted via the web

The advantage is that many companies close all ports except port 80 for security reasons and this might be the only way to transport requests

Page 19: C# Application Domains and Remoting

19

Transmission Protocols

TCP/IP This serializes into a binary stream transported

over TCP/IP sockets It requires the use of ports other than 80 Since the stream is binary, it is more compact and

efficient than the textual stream used by SOAP

Page 20: C# Application Domains and Remoting

20

Simple Object Access Protocol

SOAP is a textual form of RPC It represents all the information for a

remote procedure call as an XML document

Any objects which have to be passed are also represented as XML documents

SOAP is a surprisingly complex format

Page 21: C# Application Domains and Remoting

21

Clients and Servers

Remote objects listen for requests from clients in the form of method calls

In this way, a remote object acts as a server and the program calling the remote method is the client

In many cases, one remote object will call another and they will alternately become clients and servers as the exchange continues

Page 22: C# Application Domains and Remoting

22

Publishing

How do you find a remote object? When a remote object is created, it

publishes its location in a directoryIt selects a name and binds its internet

address and proxy to the name The client then asks the server for the

remote object and is given a proxy for the object

Page 23: C# Application Domains and Remoting

23* Daniel Meng

Page 24: C# Application Domains and Remoting

24

Remoting Components

Proxies Have the same interface as a remote object Marshal the calls and transmit them across a network Transparent proxy

This presents the remote interface and is what is called by the client

Real proxy This is called by the transparent proxy and marshals the data

Page 25: C# Application Domains and Remoting

25

Remoting Components

Formatters Translates an object into its serialized form

Sinks These are components which process the messages They are in a chain which can be extended They can

Enforce security Act as formatters Encrypt the messages

Page 26: C# Application Domains and Remoting

26

Remoting Components

ChannelsThe channel is the part that is responsible for

the transport of the messagesThis is either an HTTP channel or a TCP/IP

channel Custom Sinks

You can create custom sinks to do additional processing of the messages

Page 27: C# Application Domains and Remoting

27

Remote Object Lifetime

When you create a remote object you can select between Single Call

Every call to the object generates a new instance of the object which handles the request and then dies

This is suitable if each call should have its own data and should have no memory of any other calls

This makes the remote object stateless

Page 28: C# Application Domains and Remoting

28

Remote Object Lifetime

Singleton If you select this, then all calls to the remote object

are handled by the same object There is only a single copy of the object Data can be stored in the object and it will be

available to the next call to the object This makes the object stateful

Page 29: C# Application Domains and Remoting

29

Example: Remote Phone Directory

We will now look at a simple example of a remote phone directory consisting of PhoneDirectory

A remote object acting as a directory PhoneInfo

A serializable class used to return the phone information on a person

Client A command line application which looks up phone

information and displays the results

Page 30: C# Application Domains and Remoting

30

Step 1: Interfaces

First, create an interface for the remote objectpublic interface IPhoneDirectory

{

PhoneInfo GetPhoneInfo(string name);

}

Page 31: C# Application Domains and Remoting

31

Step 1: Interfaces

You might also want to make an interface for the serializable classespublic interface IPhoneInfo { string Name { get;} string Address { get;} string Phone { get;} string ToString(); }

Page 32: C# Application Domains and Remoting

32

Step 2: Remote Object

This must be accessed by reference Therefore, it must extend MarshalByRefObject

class PhoneDirectory : MarshalByRefObject, IPhoneDirectory

{

Hashtable phoneTable = new Hashtable();

}

Page 33: C# Application Domains and Remoting

33

Step 2: Remote Object

The constructor fills the phone dirpublic PhoneDirectory(){ phoneTable["Fred"] = new PhoneInfo("Fred Flintstone", "99 Granite Way", "416-

238-4387"); phoneTable["Wilma"] = new PhoneInfo("Wilma Flintstone", "99 Granite Way",

"416-238-4387"); phoneTable["Barney"] = new PhoneInfo("Barney Rubble", "97 Granite Way",

"416-238-4343"); phoneTable["Betty"] = new PhoneInfo("Betty Rubble", "97 Granite Way", "416-

238-4343"); phoneTable["Bam Bam"] = new PhoneInfo("Bam Bam Flintstone", "99 Granite

Way", "416-238-4387"); }

Page 34: C# Application Domains and Remoting

34

Step 2: Remote Object

Finally, we have the method to return a phone entrypublic PhoneInfo GetPhoneInfo(String name)

{

return (PhoneInfo)phoneTable[name];

}

Page 35: C# Application Domains and Remoting

35

Step 3: Serializable Data Now, we make the PhoneInfo class serializable

[Serializable] public class PhoneInfo { String _name; String _address; String _phoneNumber;

public PhoneInfo(string nm, string adr, string ph) { _name = nm; _address = adr; _phoneNumber = ph; }

…}

Page 36: C# Application Domains and Remoting

36

Step 4: Create a Server

This is a Main method to create and publish the remote objectstatic void Main(string[] args){ TcpServerChannel channel = new TcpServerChannel(9999); ChannelServices.RegisterChannel(channel, false); RemotingConfiguration.RegisterWellKnownServiceType(

typeof (PhoneDirectory), "PhoneDirectory", WellKnownObjectMode.Singleton);

… }

Page 37: C# Application Domains and Remoting

37

Step 4: Create a Server

Create channel on port 9999TcpServerChannel channel = new

TcpServerChannel(9999); Register channel without security

ChannelServices.RegisterChannel(channel, false); Create the remote object

RemotingConfiguration.RegisterWellKnownServiceType(typeof (PhoneDirectory), "PhoneDirectory",

WellKnownObjectMode.Singleton);

Page 38: C# Application Domains and Remoting

38

Step 5: Create a Client

Create a channelChannelServices.RegisterChannel(new TcpClientChannel(), false);

Locate remote objectIPhoneDirectory phoneDir =

(IPhoneDirectory)Activator.GetObject( typeof(IPhoneDirectory),

"tcp://localhost:9999/PhoneDirectory"); Invoke a method

PhoneInfo info = phoneDir.GetPhoneInfo(“Fred”);

* see remote_demo, remote_demo_client