15
.Net Remoting .Net Remoting

Net Remoting. 2 Distributed Computing under.Net In.Net, there are three levels of access to distributed computing machinery: In.Net, there are three

Embed Size (px)

Citation preview

Page 1: Net Remoting. 2 Distributed Computing under.Net In.Net, there are three levels of access to distributed computing machinery: In.Net, there are three

.Net Remoting.Net Remoting

Page 2: Net Remoting. 2 Distributed Computing under.Net In.Net, there are three levels of access to distributed computing machinery: In.Net, there are three

.Net Remoting.Net Remoting 22

Distributed Computing Distributed Computing under .Netunder .Net

In .Net, there are three levels of access to distributed In .Net, there are three levels of access to distributed computing machinery:computing machinery:Low Level:Low Level:

System.Net.SocketsSystem.Net.Sockets Intermediate LevelIntermediate Level

System.Runtime.InteropSerivesSystem.Runtime.InteropSerives Access COM objects and Win32 APIAccess COM objects and Win32 API

System.Runtime.RemotingSystem.Runtime.Remoting Access channels and CLR activationAccess channels and CLR activation Channels based on TCP or HTTP over TCPChannels based on TCP or HTTP over TCP

High LevelHigh LevelSystem.Web.ServicesSystem.Web.ServicesSystem.Web.UISystem.Web.UI

Page 3: Net Remoting. 2 Distributed Computing under.Net In.Net, there are three levels of access to distributed computing machinery: In.Net, there are three

.Net Remoting.Net Remoting 33

Distributed Computing Distributed Computing under .Netunder .Net

System.Net.SocketsSystem.Net.SocketsProvides low-level access to socket objectsProvides low-level access to socket objectsYou create listeners and send and receive just like we did inYou create listeners and send and receive just like we did in

CSE691 – Software Modeling and AnalysisCSE691 – Software Modeling and Analysis System.Runtime.RemotingSystem.Runtime.Remoting

Provides access at a medium level of abstraction.Provides access at a medium level of abstraction.You create channels and proxies and do RPCs on remote You create channels and proxies and do RPCs on remote

objectsobjectsData marshaling is much richer than under COM. You can Data marshaling is much richer than under COM. You can

send anything the CLR understands as long as it has a send anything the CLR understands as long as it has a [serializable] attribute or derives from MarshalByRef.[serializable] attribute or derives from MarshalByRef.Basically you just add those .Net identifiers and the CLR takes care of Basically you just add those .Net identifiers and the CLR takes care of

everything else.everything else.

Page 4: Net Remoting. 2 Distributed Computing under.Net In.Net, there are three levels of access to distributed computing machinery: In.Net, there are three

.Net Remoting.Net Remoting 44

Remoting .Net LibrariesRemoting .Net Libraries System.Runtime.Remoting.ActivationSystem.Runtime.Remoting.Activation

Activate remote objectsActivate remote objects System.Runtime.Remoting.ChannelsSystem.Runtime.Remoting.Channels

Sets up channel sinks and sources for remote objectsSets up channel sinks and sources for remote objects System.Runtime.Remoting.Channels.HTTPSystem.Runtime.Remoting.Channels.HTTP

Uses SOAP protocol to communicate with remote objectsUses SOAP protocol to communicate with remote objects System.Runtime.Remoting.Channels.TCPSystem.Runtime.Remoting.Channels.TCP

Uses binary transmission over socketsUses binary transmission over sockets System.Runtime.Remoting.ContextsSystem.Runtime.Remoting.Contexts

Set threading and security contexts for remotingSet threading and security contexts for remoting System.Runtime.Remoting.MessagingSystem.Runtime.Remoting.Messaging

Classes to handle message passing through message sinksClasses to handle message passing through message sinks System.Runtime.Remoting.MetadataSystem.Runtime.Remoting.Metadata

Customize HTTP SoapAction type output and XML Namespace URLCustomize HTTP SoapAction type output and XML Namespace URL System.Runtime.Remoting.ProxiesSystem.Runtime.Remoting.Proxies System.Runtime.Remoting.ServicesSystem.Runtime.Remoting.Services

Page 5: Net Remoting. 2 Distributed Computing under.Net In.Net, there are three levels of access to distributed computing machinery: In.Net, there are three

.Net Remoting.Net Remoting 55

Distributed Computing Distributed Computing under .Netunder .Net

System.Web.ServicesSystem.Web.ServicesServers are hosted under IISServers are hosted under IISUse HTTP-GET and HTTP-POST or higher level SOAPUse HTTP-GET and HTTP-POST or higher level SOAP

Simple Object Access Protocol (SOAP)Simple Object Access Protocol (SOAP)Wraps XML message in SOAP envelope (XML tags)Wraps XML message in SOAP envelope (XML tags)SOAP messages are interpreted by IIS and ASPSOAP messages are interpreted by IIS and ASPTypically use standard and/or custom COM components in Typically use standard and/or custom COM components in

ASP pages.ASP pages.Active Server Pages (ASP) are XHTML pages with Active Server Pages (ASP) are XHTML pages with

embedded server-side and client-side scripts that may embedded server-side and client-side scripts that may access COM and C# objects for a significant part of their access COM and C# objects for a significant part of their processing.processing.

Page 6: Net Remoting. 2 Distributed Computing under.Net In.Net, there are three levels of access to distributed computing machinery: In.Net, there are three

.Net Remoting.Net Remoting 66

Web Related .Net LibrariesWeb Related .Net Libraries

System.WebSystem.WebSystem.Web.HostingSystem.Web.Hosting

Communicate with IIS and ISAPI run-timeCommunicate with IIS and ISAPI run-timeSystem.Web.MailSystem.Web.MailSystem.Web.SecuritySystem.Web.Security

cookies, web authentication, Passportcookies, web authentication, PassportSystem.Web.Services – close ties to ASP.NETSystem.Web.Services – close ties to ASP.NET

System.Web.Services.DescriptionSystem.Web.Services.DescriptionSystem.Web.Services.DiscoverySystem.Web.Services.DiscoverySystem.Web.Services.Protocol – raw HTTP and SOAP requestsSystem.Web.Services.Protocol – raw HTTP and SOAP requestsSystem.Web.SessionState – maintain state between page requestsSystem.Web.SessionState – maintain state between page requests

System.Web.UI – access to WebFormsSystem.Web.UI – access to WebForms

Page 7: Net Remoting. 2 Distributed Computing under.Net In.Net, there are three levels of access to distributed computing machinery: In.Net, there are three

.Net Remoting.Net Remoting 77

Remoting ArchitectureRemoting Architecture

.Net Remoting uses System.Runtime.Remoting.Net Remoting uses System.Runtime.Remoting It is built on processing that supports:It is built on processing that supports:

ChannelsChannelsA channel is a socket-based communication mechanism A channel is a socket-based communication mechanism

that can use either basic TCP protocol or the higher HTTP that can use either basic TCP protocol or the higher HTTP layer.layer.

ActivationActivationActivation is basically the same process that the CLR uses Activation is basically the same process that the CLR uses

to create local objects.to create local objects. You just have to tell the CLR what object you want.You just have to tell the CLR what object you want. The CLR manages its lifetime and marshals data and references The CLR manages its lifetime and marshals data and references

back and forth, using a channel.back and forth, using a channel.

Page 8: Net Remoting. 2 Distributed Computing under.Net In.Net, there are three levels of access to distributed computing machinery: In.Net, there are three

.Net Remoting.Net Remoting 88

Remoting ArchitectureRemoting Architecture

Application Domain

CLR Thread

Application Domain

CLR Thread

client Object

server object

proxy forRemote-able

ObjectRemote-able

Object

channel

channel

Page 9: Net Remoting. 2 Distributed Computing under.Net In.Net, there are three levels of access to distributed computing machinery: In.Net, there are three

.Net Remoting.Net Remoting 99

Basic .Net Remoting – Remote Basic .Net Remoting – Remote ObjectObject

First create a remote-able object:First create a remote-able object:Design an object to be invoked remotely, derived from Design an object to be invoked remotely, derived from

MarshalByRefMarshalByRef Implement the class as a C# library – this creates a dll.Implement the class as a C# library – this creates a dll.Any objects that you need to pass to that object need Any objects that you need to pass to that object need

to be serializable.to be serializable.The basic types like ints and strings already are serializable.The basic types like ints and strings already are serializable.Your class objects need to have the [Serializable] attribute Your class objects need to have the [Serializable] attribute

and contain only serializable data members.and contain only serializable data members.

Or they can derive from MarshalByRef.Or they can derive from MarshalByRef.

Page 10: Net Remoting. 2 Distributed Computing under.Net In.Net, there are three levels of access to distributed computing machinery: In.Net, there are three

.Net Remoting.Net Remoting 1010

Remoting ArchitectureRemoting Architecture

Application Domain

CLR Thread

Application Domain

CLR Thread

client Object

server object

proxy forRemote-able

ObjectRemote-able

Object

channel

channel

Page 11: Net Remoting. 2 Distributed Computing under.Net In.Net, there are three levels of access to distributed computing machinery: In.Net, there are three

.Net Remoting.Net Remoting 1111

Basic .Net Remoting – the Basic .Net Remoting – the ServerServer

Create a server:Create a server:Design a C# class, using a C# Console Application, or Empty Project Design a C# class, using a C# Console Application, or Empty Project

in which you will create a WinForm.in which you will create a WinForm. In a member function – main will work:In a member function – main will work:

Create a TcpServerChannelCreate a TcpServerChannelRegister the Channel with ChannelServicesRegister the Channel with ChannelServicesRegister the type of object clients want to use remotely by calling Register the type of object clients want to use remotely by calling

RegisterWellKnownServiceTypeRegisterWellKnownServiceType

Then, block the main thread.Then, block the main thread.The object will be created by the CLR on its own thread and remote clients will The object will be created by the CLR on its own thread and remote clients will

access the object through the CLR.access the object through the CLR.You don’t have to write any server code to support this access – the CLR You don’t have to write any server code to support this access – the CLR

takes care of it for you.takes care of it for you.

Create a reference to the remote-able object’s assembly, build, and Create a reference to the remote-able object’s assembly, build, and run the server.run the server.

Page 12: Net Remoting. 2 Distributed Computing under.Net In.Net, there are three levels of access to distributed computing machinery: In.Net, there are three

.Net Remoting.Net Remoting 1212

Remoting ArchitectureRemoting Architecture

Application Domain

CLR Thread

Application Domain

CLR Thread

client Object

server object

proxy forRemote-able

ObjectRemote-able

Object

channel

channel

Page 13: Net Remoting. 2 Distributed Computing under.Net In.Net, there are three levels of access to distributed computing machinery: In.Net, there are three

.Net Remoting.Net Remoting 1313

Basic .Net Remoting – the Basic .Net Remoting – the ClientClient

Create a client:Create a client:Design a C# class, using a C# Console Application, or Empty Design a C# class, using a C# Console Application, or Empty

Project in which you will create a WinForm.Project in which you will create a WinForm. In a member function – main will work:In a member function – main will work:

Create a TcpServerChannelCreate a TcpServerChannelRegister the Channel with ChannelServicesRegister the Channel with ChannelServices

In a member function – main will work:In a member function – main will work:Create a proxy to access the remote component. You do this by calling Create a proxy to access the remote component. You do this by calling

Activator.GetObject(…) and casting the result to the type of the remote Activator.GetObject(…) and casting the result to the type of the remote object.object.

Note that both client and server need the assembly for the remote-able object.Note that both client and server need the assembly for the remote-able object.

Then, make calls on the remote object as needed.Then, make calls on the remote object as needed.You simply use the proxy as if it were the real object.You simply use the proxy as if it were the real object.

Create a reference to the remote-able object’s assembly, build, Create a reference to the remote-able object’s assembly, build, and run the client.and run the client.

Page 14: Net Remoting. 2 Distributed Computing under.Net In.Net, there are three levels of access to distributed computing machinery: In.Net, there are three

.Net Remoting.Net Remoting 1414

Remoting ArchitectureRemoting Architecture

Application Domain

CLR Thread

Application Domain

CLR Thread

client Object

server object

proxy forRemote-able

ObjectRemote-able

Object

channel

channel

Page 15: Net Remoting. 2 Distributed Computing under.Net In.Net, there are three levels of access to distributed computing machinery: In.Net, there are three

.Net Remoting.Net Remoting 1515

Architecture for Project #3Architecture for Project #3 The previous slides described a classic client/server The previous slides described a classic client/server

configuration.configuration.For Project #3 we need something a little more sophisticated.For Project #3 we need something a little more sophisticated.

In a classic client/server only the client initiates communication.In a classic client/server only the client initiates communication.In Project #3 you will have to have the “server” make notifications back on the In Project #3 you will have to have the “server” make notifications back on the

“client”.“client”. I would create a reply function that works just like the server, I would create a reply function that works just like the server,

described earlier, except that the thread does not block after setting up described earlier, except that the thread does not block after setting up the channel, but instead returns.the channel, but instead returns.

I would create a request function that works just like the client, I would create a request function that works just like the client, described earlier.described earlier.

I would have my Main function call the reply function then, as needed, I would have my Main function call the reply function then, as needed, call the request function. Everything else is just servicing the user call the request function. Everything else is just servicing the user interface.interface.

I would do this for both the “client” that subscribes to notifications, and I would do this for both the “client” that subscribes to notifications, and the “server” that sends out directory change notifications.the “server” that sends out directory change notifications.