Sie sind auf Seite 1von 15

Remoting

What is remoting?
• .NET Remoting is a technique using which .NET
objects in another application domain (which
may be on another server) can be accessed.
• System.Runtime.Remoting
• Under the hood .NET Remoting uses CLR
Remoting.
• All language constructs (like constructor,
methods, properties, fields etc) can be used with
CLR Remoting.
Comparison with similar techniques
• Using .NET Remoting interoperability ( accessing different
types of application) is not possible. For this web services
should be used.
• Note that web services is also W3C standard and hence
platform independent way of accessing objects. However
web services can be accessed only via HTTP (SOAP) and
could be slow as compared to .NET Remoting
• For objects inside same application domain remoting is not
required.
• .NET Remoting is now superseded by Windows
Communication Foundation (WCF), which is part of the
.NET Framework 3.0.
• WCF allows both platform-independent and fast binary
communication between .NET application.
Remote objects
• An object that can be accessed remotely from a different
system.
• .NET requires that remote object derive from
MarshalByRefObject
• A proxy object is used to access remote object from
another application.
• Remote object has a distributed identity and proxy knows
about this identity.
• We will call the application that has Remote object as
server application and the application that access the
remote object as client application.
• The parameters that are passed and returns must be
serializable.
Channels
• Remote objects are accessed through
Channels.
• Channels physically transport the
messages to and from remote objects.
• There are two existing channels
• TcpChannel
• HttpChannel
Remoting Architecture
Components required for building
an application
• Remote object
• Server application
• Client application

• Steps to build
1. Write Remote class
2. Write Server class
3. Write Client class
Writing a remote class
• Create a C# console application.

• Create a remote object class called Hello.


using System;
using System.Collections.Generic;
using System.Text;

namespace RemoteServerApp
{
public class Hello: MarshalByRefObject
{
public string sayHello(string name)
{
return "hello " + name;
}

}
}
Server program
• A server program needs to be written to register
and expose remote object.
• The server program example uses TCP/IP
channel .
• The service can be registered as
WellKnownObjectMode.SingleCall, which
results in a new instance of the object for each
client, or as
WellKnownObjectMode.Singleton, which
results in one instance of the object used for all
clients.
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
Add a reference to
namespace RemoteServerApp{ System.Runtime.Remoting
diagram in the next slide
class RemoteServerClass {
static void Main() {
TcpChannel channel = new TcpChannel(8086);

ChannelServices.RegisterChannel(channel,
true);
Register as an available service
RemotingConfiguration.RegisterWellKnownService
Type(typeof(Hello), "RemoteHello",
WellKnownObjectMode.SingleCall);
Client will use this name to
refer to this object
Console.WriteLine("Press the enter key to
exit...");
System.Console.ReadLine();
}
}
}

Right click on
Solution Explorer and
Add reference

Build the application.


Creating Client
• Create another C# application
• Add a reference to
System.Runtime.Remoting in the project.
• Also add a reference to the project containing
the Remote object (otherwise the code will not
compile because it won't know how to find a
reference to Remote object).
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using
System.Runtime.Remoting.Channels.Tcp;

namespace RemoteHelloClient{
class RemoteClient {
static void Main(string[] args) {
TcpChannel chan = new TcpChannel();
ChannelServices.RegisterChannel(chan,
true);

RemoteServerApp.Hello obj =
(RemoteServerApp.Hello)Activator.
GetObject(typeof(RemoteServerApp.Hello),
"tcp://localhost:8086/RemoteHello");

if( obj.Equals(null) ) {
System.Console.WriteLine("Error: unable
to locate server");
}
else
{ Console.WriteLine(obj.sayHello("Ra
ma"));
}}} }
Execute server application. Execute Client application.
You could run both the application from the console.

Das könnte Ihnen auch gefallen