9
1 University of Dublin Trinity College Microsoft .NET Remoting Framework 2 2 .NET Remoting Elements The .Net Remoting Framework is a layered and highly extensible system Replace tiers Chain new implementations into existing tiers Architecture contains 5 elements Proxies: masquerade as remote objects and forward calls Messages: contain data necessary to execute remote call Message Sinks: allow custom processing of messages Formatters: serialise messages to transfer formats (e.g. SOAP) Transport Channels: transfer serialised messages to the remote . . process 3 Simplified model of .Net Remoting Msg Proxy MessageSink MessageSink Msg Dispatcher Srv Obj 4 Proxies Masquerade as remote object Provide same interface as remote object Forwards each invocation to remoting framework as a message object Handles the return message from the .Net Remoting Framework 5 Proxy Creation Remote reference creation starts with a call of the new operator or Activator.GetObject() The .Net Remoting Framework generates 2 proxy objects An instance of System.Runtime.Remoting.Proxies.TransparentProxy Returned to client An instance of RemotingProxy Hidden from client Responsible for passing invocation request to remoting framework References to client-side messaging sink chains are obtained from sink providers Stored in Identity Object 6 Proxy architecture TransparentProxy RemotingProxy [RealProxy] Identity First sink in chain [IMessageSink] _rp _identity _channelSink

NET Remoting Elements Microsoft .NET Remotingebarrett/lectures/cs7051/pdfs/dotnet2.pdf · 1 University of Dublin Trinity College Microsoft .NET Remoting Framework 2 2.NET Remoting

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: NET Remoting Elements Microsoft .NET Remotingebarrett/lectures/cs7051/pdfs/dotnet2.pdf · 1 University of Dublin Trinity College Microsoft .NET Remoting Framework 2 2.NET Remoting

1

University of Dublin

Trinity College

Microsoft .NET Remoting

Framework 2

2

.NET Remoting Elements

The .Net Remoting Framework is a layered and highly

extensible system• Replace tiers

• Chain new implementations into existing tiers

Architecture contains 5 elements• Proxies: masquerade as remote objects and forward calls

• Messages: contain data necessary to execute remote call

• Message Sinks: allow custom processing of messages

• Formatters: serialise messages to transfer formats (e.g. SOAP)

• Transport Channels: transfer serialised messages to the remote .

. process

3

Simplified model of .Net Remoting

MsgProxy

MessageSinkMessageSink

Msg

DispatcherSrv

Obj

4

Proxies

Masquerade as remote object• Provide same interface as remote object

• Forwards each invocation to remoting framework as a message

object

• Handles the return message from the .Net Remoting Framework

5

Proxy Creation

Remote reference creation starts with a call of the newoperator or Activator.GetObject()

The .Net Remoting Framework generates 2 proxyobjects• An instance of System.Runtime.Remoting.Proxies.TransparentProxy

• Returned to client

• An instance of RemotingProxy• Hidden from client

• Responsible for passing invocation request to remoting framework

References to client-side messaging sink chains areobtained from sink providers• Stored in Identity Object

6

Proxy architecture

TransparentProxy

RemotingProxy

[RealProxy]

Identity

First sink in chain

[IMessageSink]

_rp

_identity

_channelSink

Page 2: NET Remoting Elements Microsoft .NET Remotingebarrett/lectures/cs7051/pdfs/dotnet2.pdf · 1 University of Dublin Trinity College Microsoft .NET Remoting Framework 2 2.NET Remoting

2

7

TransparentProxy

Contains a list of interface methods of the remote object

Registered with CLR on creation

All method invocations are intercepted by runtime• call is examined to determine if it is a valid method of the remote

object …

• … and if an instance of the remote object resides in the same

application domain as the proxy …

• In which case a simple method call is routed to the actual object

• … or if the object is in a different application domain

• the call parameters on the stack are packaged into an IMessage object and

forwarded to the RealProxy instance by calling its Invoke method.

8

RealProxy

Responsible for actual invocation

Is passed MessageData by TransparentProxy

• Creates the Message object

Does the invocation

• By calling SyncProcessMessage on first message sink

Handles return values

• Inits out paramaters and return value in Message object and return to TP

Is first extension point

• E.g. A custom RealProxy implementation can perform load balancing,

directing the invocation to one of a number of remote objects

9

Simplified Sequence Diagram

10

Messages

A dictionary object that implements the IMessage

interface

Several Types• E.g. ConstructionCall, MethodCall

Message object is passed through a chain of sinks andpasses at least two important points• Formatter

• Special kind of sink that encodes the internal dictionary into a wire protocol

format

• Transport Channel

• Transfers the serialised message from one format to another

11

Message Contents

Some properties of class

System.Runtime.Remoting.Messaging.MethodCall …

int ArgCount

object[] Args {23,45,”stephen”}

object[] InArgs

string MethodName DoWork

object MethodSignature null

LogicalCallContext LogicalCallContext null

string Uri /MyRemoteObject.soap

12

Message Sinks

Sinks act in chains• Recieves message object

• Processes the message, if it can

• Passes message to next sink in chain

• Recieves and returns response

4 interfaces• IMessageSink (and variant IDynamicMessageSink)

• IClientChannelSink

• IServerChannelSink

• IClientFormatterSink

A full client chain will have one or more objects implementing (inorder) IMessageSink, IClientFormatterSink, andIClientChannelSink (4-6 stages)

A full server chain will pass messages through up to 13 stages

Page 3: NET Remoting Elements Microsoft .NET Remotingebarrett/lectures/cs7051/pdfs/dotnet2.pdf · 1 University of Dublin Trinity College Microsoft .NET Remoting Framework 2 2.NET Remoting

3

13

IMessageSink

public interface IMessageSink

{

IMessageSink NextSink{get;}

IMessageCtrl AsyncProcessMessage(IMessage msg,

IMessageSink replySink);

IMessage SyncProcessMessage(IMessage msg);

}

Messages pass though each IMessageSink object untill

it reaches a formatter…

14

Serialisation through Formatters

A message object must be serialised into a streambefore it can be transported to the remote server

This is the job of the formatter

.Net provides two default formatter implementations• SoapFormatter

• BinaryFormatter

Note the separation of Formatter and Transport• Any formatter can be used with any transport

• Formatter produces transfer independent request – no http headersfor SOAP formatter for example

15

Formatters

Formatters implement IClientFormatterSink• This is a combination of IMessageSink and IClientChannelSink

Message processing will reach the formatter via itsSyncProcessMessage or AsyncProcessMessagemethods of IMessageSink

On the client side the formatter will serialise themessage onto a request stream it requests from thenext IClientChannelSink object in the sink chain• It also initialises a transfer header object, which will be used by the

formatter when creating transport protocol specific messagecontent

16

IClientChannelSink

public interface IClientChannelSink

{

IClientChannelSink NextChannelSink { get;}

void AsyncProcessRequest(…)

void AsyncProcessResponse(…)

Stream GetRequestStream(IMessage msg, …)

void ProcessMessage(IMessage msg,

ITransportHeaders reqHdrs,

Stream requestStream,

…, ref Stream response);

}

17

Sample SOAP Format

<SOAP-ENV:Envelope

xmlsn:xsi=http://www.w3.org/2001/XMLSchema-instance

etc. etc. >

<SOAP-ENV:Body>

<i2:doMethod id=“ref-1”>

<newval>23</newval>

</i2:doMethod>

</SOAP-ENV:Body>

</SOAP-ENV:Envelope>

18

The Transport Channels

Eventually, the message is passed to the final

IClientChannelSink implementation• This is the transport channel

The transport sink’s responsibility is to convert theheader information passed to it into a protocol

dependent format• E.g. HTTP headers

It then opens a connection to the remote server and

sends the request• formatted headers and the request stream

Page 4: NET Remoting Elements Microsoft .NET Remotingebarrett/lectures/cs7051/pdfs/dotnet2.pdf · 1 University of Dublin Trinity College Microsoft .NET Remoting Framework 2 2.NET Remoting

4

19

HTTP Headers for SOAP remoting

POST /MyRemoteObject.soap HTTP/1.1

User-Agent: Mozilla/4.0+(Compatible; MSIE 6.0 ….

Remoting;

MS .Net CLR 1.0.2914.16)

SOAPAction:

http://schemas.microsoft.com/clr/nsassem/......

Content-Type: text/xml; charset=“utf-8”

Content-Length: 467

Expect: 100-continue

Connection: Keep-Alive

Host: localhost

20

Client side messaging model

TransparentProxy

RealProxy

ClientContextTerminatorSink

[IMessageSink]

Optional dynamic sinks

[IDynamicMessageSink]

Optional preprocessing

[IMessageSink]

Formatter

[IClientFormatterSink]

Optional preprocessing

[IClientChannelSink]

Transfer channel

[IClientChannelSink]

Message emission

Notification of dynamic sinks

registered by

Context.RegisterDynamicProperty()

…of the message

Formatting

..of the serialised

message

Transfer

Customisable during

channel creation

21

ClientContextTerminatorSink

Automatically registered for all channels

First sink to be called• Notifies dynamic context sinks associated with current remoting

context

• These sinks don’t pass information along the chain

• When this processing is complete, the ClientContextTerminatorSink

passes the message to next IMessageSink

22

Server-side Messaging

HttpServerSocketHandler

HttpServerTransportSink

[IServerChannelSink]

SDLChannelSink

[IServerChannelSink]

Optional preprocessing

[IServerChannelSink]

Formatter

[IServerChannelSink]

Optional preprocessing

[IServerChannelSink]

DispatchChannelSink

[IServerChannelSink]

CrossContextChannel

[IMessageSink]

Reception of the serialised message

?WSDL requests

… of the serialised

message

Deserialisation

… of the message

Dispatching of the message

Notification of context sinks

Customisable during

channel creation

23

Server-side Messaging

Option dynamic sinks

[IDynamicMessageSink]

ServerContextTerminatorSink

[IMessageSink]

LeaseSink

[IMessageSink]

ServerObjectTerminatorSink

[IMessageSink]

StackbuilderSink

[IMessageSink]

CrossContextChannel

[IMessageSink]

Registered

by Context.RegisterDynamicProperty()

Last real sink

Increase lifetime

Forwards call

to stackbuilder

Executes call on

destination object

Notification of context sinks

From the Message’s

ServerIdentity

24

ServerChannelSinkStack

Each server side sink’s ProcessMessage takes an

additional paramater – a sink stack

Every sink that participates in the processing of the

message adds itself to this stack

This is so that call can be handled asynchronously…

Page 5: NET Remoting Elements Microsoft .NET Remotingebarrett/lectures/cs7051/pdfs/dotnet2.pdf · 1 University of Dublin Trinity College Microsoft .NET Remoting Framework 2 2.NET Remoting

5

25

IServerChannelSink

public interface IServerChannelSink

{

IServerChannelSink NextSinkChannel{get;}

ServerProcessing ProcessMessage(IServerChannelSinkStack s,

IMessage requestMsg,

ITransportHeaders reqHdrs,

Stream reqStream,

ref IMessage responseMsg,

…);

void AsyncProcessResponse(IServerResponseChannelSink sinkStack,

…);

Stream GetResponseStream(IServerResponseChannelSinkStack s,

…);

}

26

HttpServerTransportSink

Sets up ServerChannelSinkStack• Pushes itself onto the stack

Forwards call to next sink

After message processing is finished it generatesappropriate Http response heades• “200 OK” – synchronous

• “202 Accepted” – one way

27

SDLChannelSink

Looks for “?WSDL” or “?SDL” at end of HTTP Get

request

If found, does not forward request to destination object

Generates WSDL and returns this

Uses .Net FrameworkConvertTypesToSchemaToStream() from

System.Runtime.Remoting.MetadataServices.MetaD

ata

28

Formatters

Default Http channel uses both SoapServerFormatterSink andBinaryServerFormatterSink, in that order

If first formatter can deserialise the message stream• does so, and pushes itself onto the sink stack

• IMessage object passed to next sink

• Stream will be passed as null

If it can’t• A copy of the stream is passed to the next sink (the Binary formatter)

• Doesn’t push itself onto stack

Binary formatter behaves in same way• If it can’t process stream, it passes copy to next sink

• An exception is thrown if next sink is not a formatter

29

DispatchChannelSink

The dispatcher is responsible for insuring that the target

object is running

Takes the decoded IMessage and forwards it to

ChannelServices.DispatchMessage()• Checks for disconnected or timed-out objects and dynamically

instantiates SAOs if they do not exist at the server

Runs optional dynamic CrossContextChannel sinks

30

ServerContextTerminatorSink

Last “hardwired” sink

Uses the IMessage’s ServerIdentity object to build endof sink chain

Which sinks are added depend on Context data

It will include a lease sink, a TerminatorSink and astackbuilder sink.

Page 6: NET Remoting Elements Microsoft .NET Remotingebarrett/lectures/cs7051/pdfs/dotnet2.pdf · 1 University of Dublin Trinity College Microsoft .NET Remoting Framework 2 2.NET Remoting

6

31

Extending .Net Remoting Framework

Creating a compression sink

A sink typically operates on the IMessage object or theserialised message stream

A compression sink operates on the message stream• Compress the stream on the client side after formatter

• Decompress on the server side before the formatter

32

Compression Sink on Client

SoapClientFormatterSink

[IMessageSink]

CompressionClientSink

[IClientChannelSink]

HttpClientTransportSink

[IClientChannelSink]

33

Compression Sink on Server

HttpServerTransportSink

[IServerChannelSink]

CompressionServerSink

[IServerChannelSink]

SoapServerFormatterSink

[IServerChannelSink]

34

The necessary classes

CompressionClientSink

• Implements IClientChannelSink. Compresses the request stream and

decompresses the response stream.

CompressionClientSinkProvider

• Implements IClientChannelSinkProvider. Responsible for the creation of the

sink.

CompressionServerSink

• Implements IServerChannelSink. Decompresses the request stream and

compresses the response stream.

CompressionServerSinkProvider

• Implements IServerChannelSinkProvider. Responsible for creation of theserver side sink.

35

Implementing the client-side sink

public class CompressionClientSink:

BaseChannelSinkWithProperties,

IClientChannelSink

{

private IClientChannelSink _nextSink;

public CompressionClientSink(IClientChann …){}

public IClientChannelSink NextChannelSink

{

get{return _nextSink;}

}

36

Implementing the client-side sink

public void AsyncProcessRequest(IClientChannelSinkStack sinkStack,

IMessage msg, ITransportHeaders headers, Stream stream)

{

// call helper method to do compression

stream = CompressionHelper.GetCompressedStreamCopy(stream);

// push this sink onto the stack

sinkStack.Push(this, null);

_nextSink.AsyncProcessRequest(sinkStack, msg, headers, stream);

}

Page 7: NET Remoting Elements Microsoft .NET Remotingebarrett/lectures/cs7051/pdfs/dotnet2.pdf · 1 University of Dublin Trinity College Microsoft .NET Remoting Framework 2 2.NET Remoting

7

37

Implementing the client-side sink

public void

AsyncProcessResponse(IClientResponseChannelSinkStack

sinkStack,object state, ITransportHeaders headers, Stream

stream)

{

// call helper method to do decompression

stream =

CompressionHelper.GetUncompressedStreamCopy(stream);

// forward request

sinkStack.AsyncProcessResponse(headers, stream);

}

38

Implementing the client-side sink

public void ProcessMessage(IMessage msg, ITransportHeadersrequestHeaders, Stream requestStream, out ITransportHeadersresponseHeaders, out Stream responseStream)

{

// call helper method to do compression

requestStream =CompressionHelper.GetCompressedStreamCopy(requestStream);

// forward the call to the next sink

_nextSink.ProcessMessage(msg, requestHeaders, requestStream, outresponseHeaders, out responseStream);

// uncompress response

responseStream =CompressionHelper.GetUncompressedStreamCopy(responseStream);

}

39

The compression helper

// usinghttp://ww.icsharpecode.net/OpenSource/NZipLib/default.asp

public class CompressionHelper

{

public static Stream GetCompressedStreamCopy(StreaminStream)

{Stream oS = new MemoryStream();

DeflaterOutputStream cs = new DeflaterOutputStream(oS,new Deflater(Deflater.BEST_Compression));

byte[] buf = new Byte[256];

while(x = inStream.Read(buf,0,1000) > 0)cs.Write(buf,0,x);

cs.Finish(); cs.Flush(); return oS;

}

40

The compression helper

public static Stream

GetUncompressedStreamCopy(Stream inStream)

{

return new InflaterInputStream(inStream);

}

41

The Server-side Sink

public class CompressionServerSink:BaseChannelSinkWithProperties, IServerChannelSink

{

private IServerChannelSink _nextSink; …

public AsyncProcessResponse(IServerResponseChannelSinkStack sinkStack, object state,IMessage msg, ITransportHeaders headers, Stream stream)

{

// compress the response

stream =CompressionHelper.GetCompressedStreamCopy(stream);

// forward to stack for further processing

sinkStack.AsyncProcessResponse(msg, headers, stream);

}

42

The Server-side Sink

public ServerProcessing ProcessMessage(IServerChannelSinkStachsinkStack, IMessage requestMessage, ITransportHeadersrequestHeaders, Stream requestStream, out IMessage responseMsg,out ITransportHeaders responseHeaders, out Stream responseStream)

{

requestStream =CompressionHelper.GetUncompressedStreamCopy(requestStream);

sinkStack.Push(this, null); // put us on stack and forward call

ServerProcessing sp = _nextSync.ProcessMessage(…);

responseStream =CompressionHelper.GetCompressedStream(responseStream);

return sp;

}

Page 8: NET Remoting Elements Microsoft .NET Remotingebarrett/lectures/cs7051/pdfs/dotnet2.pdf · 1 University of Dublin Trinity College Microsoft .NET Remoting Framework 2 2.NET Remoting

8

43

The Server-side Sink

public void AsyncProcessResponse( IServerResponseChannelSink

sinkStack, object state, IMessage msg, ITransportHeaders

headers, Stream stream)

{

// compress the response

stream =

CompressionHelper.GetCompressedStreamCopy(stream);

// forward to the stack for further processing

sinkStack.AsyncProcessResponse(msg, headers, stream);

}

44

The Server-side Sink

public Stream

GetResponseStream(IServerResponseChannelSinksinkStack, object state, IMessage msg,

ITransportHeaders headers)

{

return null;

}

45

Sink Providers

public class CompressionClientSinkProvider :IClientChannelSinkProvider

{

public IClientChannelSink CreateSink(IChannelSender channel,string url, object remoteChannelData)

{

// create other sinks in the chain

IClientChanelSinknext = _nextProvider.CreateSink(channel, url,remoteChannelData);

// put our sink on top

return new CompressionClientSink(next);

}

46

Sink Providers

public class CompressionServerSinkProvider :

IServerChannelSinkProvider

{

public IServerChannelSink CreateSink(IChannelReceiver

channel)

{

// create other sinks in the chain

IClientChanelSinknext = _nextProvider.CreateSink(channel);

// put our sink on top

return new CompressionServerSink(next);

}

47

Using the sinks

ApplicationName.config

// client side configuration

<channels>

<channel ref=“http”>

<clientProviders>

<formatter ref =“soap”/>

<provider

type=“CompressionSink.CompressionClientSinkProvider, CompressionSink” />

</clientProviders>

48

Using the Sinks

ApplicationName.config

// server side configuration file

<channels>

<channel ref=“http” port=“1234”>

<serverProviders>

<provider

type=“CompressionSink.CompressionServerSinkProvider, CompressionSink” />

<formatter ref =“soap”/>

</serverProviders>

Page 9: NET Remoting Elements Microsoft .NET Remotingebarrett/lectures/cs7051/pdfs/dotnet2.pdf · 1 University of Dublin Trinity College Microsoft .NET Remoting Framework 2 2.NET Remoting

9

49

TransportHeaders

To handle compressed and uncompressed messages we can usetransport headers

Eg. ProcessMessage(…)

{

requestStream =CompressionHelper.GetCompressedStreamCopy(requestStream);

requestHeaders[“X-Compress”] = “yes”;

string xcompress = (string) responseHeaders[“X-Compress”];

if(xcompress != null && xcompress== “yes”)

{ responseStream = CompressionHelper.GetUncompressedStreamCopy(

responseStream);

}

}