3 Dotnet Remoting: Tutorial (Part 1)
3 Dotnet Remoting: Tutorial (Part 1)
Lecture: [Link]
Tutorial (Part 1): [Link]
3.1 Introduction
.NET remoting is a technology which allows objects to be placed remotely across a
network, where the object can be activated, and communicate with local objects us-
ing a communications channel. A formatter is then used to encode and decode the
messages as they pass between the remote object and the application. The format of
these message can either be:
A key element of .NET remoting is that objects can check the messages that are to be
sent before they are sent to the channel, and the remote objects are activated in dif-
ferent ways, these are:
Client-activated objects. These have a finite lease time, and once their lease has
expired, they are deleted (using the garbage collector).
Server-activated objects. These can either be defined with a single call or with a
singleton. A single call accepts one request from a client, then performs the ac-
tion, and is finally deleted (with the garbage collector). It is defined as stateless,
as it does not hold onto parameters from previous calls. Singletons are stateful
and can accept multiple calls, where they remember previous calls, and retain
their information. They can thus communicate with multiple clients. Also the
lifetime of singletons is controlled by lease-based lifetime.
Each application has its own code, data and configuration settings.
No other application can interfere with any other applications.
It is thought that application switching for processor time is more efficient than pro-
cessor switching. Along with this, it is easier to monitor the operation of an
application than it is to monitor a number of processes.
VB
VB.NET
.NET C#
C#
FCL
FCL
(Framework
(Framework CLS
CLS
Class (Common
ClassLibrary)
Library) Compiler
Compiler (Common
Language
Language
Specification)
Specification)
Web
Web
component
component
CTS
CTS
(Common
(Common MSIL (Microsoft
Type
Type Intermediate Language)
System)
System)
CLR
CLR(Common
(CommonLanguage
LanguageRuntime)
Runtime)
Allows
Allowsthe
theprograms
programsto
torun
run––similar
similarto
tothe
theJava
JavaVirtual
VirtualMachine
Machine(JVM)
(JVM)
System.Net. This includes classes relating to the networking elements of the dis-
tributed system.
System.Runtime.Remoting. This includes classes for the remote aspects of the
.NET framework, such as mechanisms for remote communication between ob-
jects.
System.Web.Services. This includes protocols relating to Web services, such as
those relating to HTTP and SOAP. This is defined as the ASP.NET Web services
framework.
Client object
Remoteable object
(object.dll)
ShowCapital()
Proxy
Serialisation
On the remote machine, the remote object is initially registered into an application
domain. The MarshalByRefObject is then used to encapsulate all the information
required to locate and access the remote object, such as its class name, its class hier-
archy, its interfaces and its communication channels. Activation occurs using the
URL, and identifies the URI (Unique Reference Identifier) of the remote object.
A MBR (Marshal-by-reference) always resides on the server, and the methods are
executed on the server, while the client communicates with the local proxy. The fol-
lowing shows an example of the ShowCapital class which derives from the
MashalByRefObject, and contains a show() method.
The MarshalByValue (MBV) involves serializing values on the server, and sending
them to the client. An MBV object is declared by a class with the Serializable attrib-
ute, such as:
[Serializable()]
public class NewMBVObject
{
}
The HTTPChannel can be used for a wide range of services which are hosted by a
Web server (such as IIS). It has security built into it, but has an overhead of extra
information (as it works at a higher level than TCP). The TCPChannel is more effi-
cient in its operation, but does not have any security built into it.
3.4.3 Activation
A key element of the .NET remoting framework is that it supports the activation of
remote objects as either a client or a server. Server activation is typically used when
remote objects do not required to maintain their state between method calls, or
where there are multiple clients who call methods on the same object instance
where the object maintains its state between function calls. In a client-activated ob-
ject, the client initiates the object and manages it for its lifetime.
Remote objects have to be initially registered with the remoting framework be-
fore the clients can use them. This is normally done when a hosting application
starts up and then registers one or more channels and one or more remote objects. It
then waits until it is terminated. When the hosting application is terminated, the ob-
jects and channels are deleted. For an object to be registered into the .NET remoting
framework the following need to be set:
Assembly name. This defines the assembly in which the class is contained in.
Type name. This defines the data type of the remote object.
Object URI. This is the indicator that clients use to locate the object.
RemotingConfiguration.RegisterWellKnownServiceType
(typeof(newclass.ShowCapital),
"ShowCapital1", WellKnownObjectMode.SingleCall);
which defines a SingleCall remote object, and the ShowCapital object, which is
within the newclass namespace. ShowCapital is thus the name of the object, where
the ShowCapital1 is the object URI.
It is also possible to store the parameters in a configuration file then using Con-
figure with the required configuration file, such as:
RemotingConfiguration.Configure("myconfig.config");
This method has the advantage that it does not need to be compiled with the appli-
cation, and can therefore be edited as required, and will be read in as required. Thus
a change of parameters, such as a change of object name does not require a recompi-
lation.
Once registered, the remote object will not instantiate itself, as this requires the
client to initiate it, or call a method. This is achieved by a client which knows the
URI of the remote object and by registering the channel it prefers using GetObject,
such as:
ChannelServices.RegisterChannel(channel);
where:
Another method is
RemotingConfiguration.RegisterWellKnownClientType(
typeof(ShowCapital),
"tcp://localhost:1234/ShowCapital");
None of these calls actually initiates the remote object, as only a proxy is created
which will be used to contact the object. The connection is only made when a meth-
od is called on the remote object. Once this happens, the remote framework extracts
the URI, and initiates the required object, and forwards the required method to the
object. If it is a SingleCall, the object is destroyed after the method has completed.
C# Code 3.1:
using System;
using System.Data;
namespace newclass
{
public class ShowCapital : MarshalByRefObject
{
public ShowCapital()
{
and the results are shown in Figure 3.5. This produces a class named ShowCapital,
which has a method of show(). This class, once built, produces a remotable class in
the form of a DLL file, which is placed in the bin\Debug folder. In this case it will
create a DLL named newclass.dll, as this is the name of the namespace.
Objects passed by
reference or value
for local objects. For
Client object remote it is not Server object
possible to pass by
reference – they
must be marshalled.
Proxy
Serialisation
1. Add a New Project, and select Console Application, and name it (for example
newclass2, as shown in Figure 3.6).
2. Next add the references to the first project (the DLL file), and to the Sys-
tem.Runtime.Remoting (Figure 3.7).
3. Next the following code can be added:
C# Code 3.2:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace newclass2
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
TcpServerChannel channel = new TcpServerChannel(1234);
ChannelServices.RegisterChannel(channel,false);
RemotingConfiguration.RegisterWellKnownServiceType
(typeof(newclass.ShowCapital), "ShowCapital",
WellKnownObjectMode.SingleCall);
Console.WriteLine("Starting...");
Console.ReadLine();
}
}
}
Client object
Server Remoteable object
innovation (object.dll)
ShowCapital()
Proxy
Channel=1234
Server-activation object
For this:
RemotingConfiguration.RegisterWellKnownServiceType
(typeof(newclass.ShowCapital), "ShowCapital", WellKnownObjectMode.SingleCall);
defines a SingleCall activation mode (which means it will run once and then be de-
leted), on the ShowCapital object, which is within the newclass namespace.
ShowCapital is thus the name of the object, where the ShowCapital1 is the object
URI. This refers to a DLL file named after the namespace (in this case newclass.dll).
The channel used is 1234. Once the server-activated component has been started it
will wait for the client to connect to it. As it is a SingleCall it will call the remote ob-
ject once, and then it will be deleted. Thus every time it is called, it will not
remember the previous state. If the Singleton option is used the remote object will
stay active and will thus store its state.
1. Add a New Project to the solution, using a Windows Application (Figure 3.9).
2. Next a reference is added for a the System.Runtime.Remoting assembly (Fig-
ure 3.10) and the remotable class (newclass), as shown in Figure 3.11.
Client object
Server Remoteable object
innovation (object.dll)
ShowCapital()
Proxy
Channel=1234
Server-activation object
C# Code 3.3:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using newclass;
namespace WindowsFormsApplication1
ShowCapital sh;
country = textBox1.Text;
cap = sh.show(country);
textBox2.Text = cap;
}
ChannelServices.RegisterChannel(channel,false);
RemotingConfiguration.RegisterWellKnownClientType(
typeof(ShowCapital),"tcp://localhost:1234/ShowCapital");
sh = new ShowCapital();
}
}
}
This then connects to server (in this case, with localhost, which has the IP address of
127.0.0.1), using TCP port of 1234.
Q3.1 Implement the code in C# Code 3.1, C# Code 3.2 and C# Code 3.3, and
prove that the application works. Next, complete the following:
(a) Add a debug message within C# Code 3.1 so that it displays the
country which is being searched for.
(b) Add a counter within C# Code 3.1 so that the remoteable object
returns a counter to show the number of times it has been called (as
illustrated in Figure 3.14). Show thus, for the SingleCall that the
counter will always show the same value.
(c) Next modify C# Code 3.2, so that is uses the Singleton method, and
show that the counter will increment for every access.
(d) Modify the application so that it uses HTTP rather than TCP.
Q3.2 Contact a neighbour in the lab, and ask them to place your server compo-
nent (dll and exe) from Tutorial Q3.1 onto their machine. Ask them to run
Q3.3 Modify the server C# Code 3.2 so that it listens on port 80 (or any other re-
served ports), and, if you have a WWW server running on your machine,
prove that the error message is:
Q3.3 From the Command Prompt, run netstat -a, and determine the TCP server
ports which are open. Next run netstat -a -v, and observe the output. What
does the 0.0.0.0 address identify?
Q3.4 Create a program which uses .NET remoting to implement the following:
(b) Created another method which returns the hostname for a given
IP address.
RemotingConfiguration.RegisterWellKnownServiceType
(typeof(newclass.ShowCapital), "ShowCapital",
WellKnownObjectMode.SingleCall);
RemotingConfiguration.RegisterActivatedServiceType
(typeof(newclass.ShowCapital));
RemotingConfiguration.RegisterWellKnownClientType(
typeof(ShowCapital), "tcp://localhost:1234/ShowCapital");
RemoteConfiguration.RegisterActivedClientType
(typeof(newclass.ShowCapital),”tcp://localhost:1234”);
3.6.2 Tutorial 2
Q3.5 Modify Tutorial Q3.1 so that it uses CAO instead of SAO.
<configuration>
<system.runtime.remoting>
<application>
<lifetime> </lifetime>
<service>
<wellknown/> <activated/>
</service>
<client>
<wellknown/> <activated/>
</client>
<channels> </channels>
</application>
</system.runtime.remoting>
</configuration>
It includes the full name of the application, with a .config extension. Thus an appli-
cation named myapp.exe will have an associated configuration file of
myapp.exe.config.
Console.WriteLine("Starting...");
Console.ReadLine();
RemotingConfiguration.RegisterWellKnownServiceType
(typeof(newclass.ShowCapital),
"ShowCapital1", WellKnownObjectMode.Singleton);
which defines a Singleton remote activation, and the ShowCapital object, which is
within the newclass namespace. ShowCapital is thus the name of the object, where
the ShowCapital1 is the object URI.
(a) Set one port to 1234, and the other to 9999. Prove that the program
will create an exception.
(b) Next, change both ports to 9999, and prove that the system works.
Q3.9 Run the program, and from the command prompt, run netstat, and deter-
mine that a TCP connection has been made. Outline its details:
Q3.10 Set up the client and server to communicate on port 5555 (using the config-
uration file). Next, contact a neighbour in the lab, and ask them to place
your server component (dll and exe) onto their machine. Ask them to run
the server component, and run the windows program to contact their com-
ponent, and prove that it works.
Note: Make sure you use the IP address of your neighbours PC.
Next, ask your neighbour to change the port to 8888 on the server side, and
change the port on your machine. Prove that the system can still communi-
cate.
C# Code 3.4:
using System;
namespace IDCapital
{
public interface IDCap
{
string show(string str);
}
}
This is the interface assembly. Next we can define the remotable object which im-
plements the interface assembly:
C# Code 3.5:
using System;
using System.Data;
using IDCapital;
namespace newclass
{
public class ShowCapital : MarshalByRefObject, IDCapital.IDCap
{
public string show(string country)
{
if (country.ToLower()=="england") return("London");
else if (country.ToLower()=="scotland") return("Edinburgh");
else return("Not known");
}
}
}
After which a remoting server can be created to register the remotable object that
implements an interface assembly:
C# Code 3.6:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using IDCapital;
namespace newclass2
{
class Class1
{
[STAThread]
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType
(typeof(newclass.ShowCapital), "ShowCapital",
WellKnownObjectMode.SingleCall);
Console.WriteLine("Starting...");
Console.ReadLine();
}
}
}
Finally a remote client is created which invokes the remotable object that imple-
ments the interface assembly:
C# Code 3.7:
. . .
IDCap sh;
country=textBox1.Text;
cap=sh.show(country);
textBox2.Text= cap;
}
private void Form1_Load(object sender, System.EventArgs e)
{
TcpClientChannel channel = new TcpClientChannel();
ChannelServices.RegisterChannel(channel);
which uses the required URL, and generates a DLL named newclass.dll. For exam-
ple:
The references are then added as Figure 3.18 and Figure 3.19.
3.8.2 Tutorial 4
Q3.11 Modify C# Code 3.1, C# Code 3.2 and C# Code 3.3, so it uses an interface
assembly.