You are on page 1of 108

06 0789728206 CH03 2/27/03 9:54 AM Page 167

OBJECTIVES

This chapter covers the following Microsoft-specified


objective for the “Creating a Managing Microsoft
Windows Services, Service Components, .NET
Remoting Objects, and XML Web Services” section of
the “Developing XML Web Services and Server
Components with Microsoft Visual Basic .NET and
the Microsoft .NET Framework” exam:
Create and consume a .NET Remoting object.
• Implement server-activated components.
• Implement client-activated components.
• Select a channel protocol and a formatter.
Channel protocols include TCP and HTTP.
Formatters include SOAP and binary.
• Create client configuration files and server
configuration files.
• Implement an asynchronous method.
• Create the listener service.
• Instantiate and invoke a .NET Remoting
object.
. This exam objective tests your skill in designing
distributed applications using .NET remoting,
which is part of the .NET Framework SDK. You
should know how to create a remote object, how to
make it available to users via a remoting server, and
how to write a client application that instantiates
remote objects and invokes their methods.

Remoting provides a very flexible and configurable


environment for distributed applications. By virtue
of this flexibility, .NET remoting applications can
C H A P T E R 3
be designed in several different ways. In this exam,
you are required to know how to make choices
between various remoting configuration parameters
(such as activation mode, channel, and formatter)
for a given scenario.

.NET Remoting
06 0789728206 CH03 2/27/03 9:54 AM Page 168

OBJECTIVES OUTLINE

Remoting is configurable. All the remoting settings Introduction 170


can be written in an XML-based configuration file.
Keeping these settings in separate configuration
files allows easier modification and maintenance of Application Boundaries 171
remoting applications. This objective requires you
to distinguish between several configuration files Process Boundary 171
such as the application configuration file, the Application Domain Boundary 172
web.config file, and the machine.config file.
Distributed Applications 173
In distributed applications, methods are invoked
across the network and several factors, such as net- Evolution of Distributed Applications 173
work bandwidth and server availability, might cause Developing Distributed Applications
your application to respond slowly as compared to Using the .NET Framework 174
a desktop application. This exam objective also tests
your skill on creating asynchronous or responsive
distributed applications. .NET Remoting Architecture 176
Object Marshalling 178
Marshal-by-Value Objects 178
Marshal-by-Reference Objects 179
Channels 179
HTTP Channels 180
TCP Channels 181
Choosing Between the HTTP and
the TCP Channels 182
Formatters 183
The SOAP Formatter 183
The Binary Formatter 184
Channels and Formatters 184
Remote Object Activation 185
Server-Activated Objects 186
Client-Activated Objects 187
Comparing the Object Activation
Techniques 188
Lifetime Leases 189

Applying .NET Remoting 191


Creating a Remotable Class 192
Creating a Server-Activated Object 194
Registering a Remotable Class As a
Server-Activated Object Using the
SingleCall Activation Mode 195
06 0789728206 CH03 2/27/03 9:54 AM Page 169

OUTLINE S T U DY S T R AT E G I E S

Instantiating and Invoking a . Write programs to create remotable objects,


Server-Activated Object 198 remoting hosts, and remoting clients.
Registering a Remotable Class As a Understand what role each of these plays in
Server-Activated Object Using the a distributed computing scenario.
Singleton Activation Mode 204
. Understand the difference between server-
Creating a Client-Activated Object 209 activated objects and client-activated objects,
Registering a Remotable Class As a HTTP channel and TCP channel, SOAP formatter
Client-Activated Object 209 and binary formatter. You should be ready to
Instantiating and Invoking a answer questions that ask you to choose
Client-Activated Object 211 between these remoting elements in a given
Using Configuration Files to Configure scenario.
the Remoting Framework 216
. Use both declarative and programmatic configu-
Server-Side Configuration 218
ration for distributed applications. Appreciate
Client-Side Configuration 221
the advantages and shortcomings of each
Using Interface Assemblies to Compile approach.
Remoting Clients 229
. Understand how to make a client program
Creating an Interface Assembly 229
responsive despite the delay in method calls
Creating a Remotable Object That
across the network by using asynchronous
Implements an Interface 230
programming techniques.
Creating a Remoting Client That
Uses an Interface Instead of the
Implementation 234
Using the Soapsuds Tool to
Automatically Generate an Interface
Assembly 236
Creating an Interface Assembly
That Works with the Client-Activated
Objects 240
Using IIS As an Activation Agent 247
Asynchronous Remoting 251
Understanding the Model of
Asynchronous Programming in
the .NET Framework 252
Applying Asynchronous Programming 254

Chapter Summary 260

Apply Your Knowledge 262


06 0789728206 CH03 2/27/03 9:54 AM Page 170

170 Par t I EXAM PREPARATION

INTRODUCTION
The .NET Framework provides a platform for building next-
generation distributed applications. I’ll start this chapter by intro-
ducing distributed applications and how they are different from
conventional applications. The .NET Framework allows you to
create distributed applications in various ways. Two popular
approaches are .NET remoting and ASP.NET Web services. I’ll
discuss .NET remoting in this chapter and cover ASP.NET Web
services in the next two chapters.
In this chapter, you’ll start learning about remoting by understand-
ing its architecture. You’ll learn about various remoting elements,
such as remotable classes, remoting hosts, remoting clients, channels,
formatters, and activation modes, and you’ll also learn how these ele-
ments fit together to create a distributed application. I’ll compare
the various choices available with each of these remoting elements
and explain how to decide between those choices in a given scenario.
The next part of the chapter is code intensive. You’ll write code to
practice creating small but fully functional distributed applications.
While working with the step-by-step exercises, you’ll develop various
skills instrumental for designing remoting applications (and of
course also for passing this exam).
I’ll first show you how to create a class that can be remoted across
network and application boundaries. I’ll then show how to create a
remoting host that hosts the class so that the client program can take
the services offered by the remotable class. I’ll also show how to
create a client program that can instantiate a remote object and
invoke methods on it.
I’ll discuss various types of applications that can work as remoting
host, such as a console application, a windows service, or IIS
(Internet Information Services). I’ll also discuss how you can use
configuration files to conveniently modify the behavior of both the
remoting host as well as the remoting client application.
Finally in this chapter, I’ll show how to program the client applica-
tion to invoke remote method calls asynchronously. Asynchronous
method invocations, as you’ll see, boost the responsiveness of the
client application and keep users happy.
06 0789728206 CH03 2/27/03 9:54 AM Page 171

Chapter 3 .NET REMOTING 171

APPLICATION BOUNDARIES
An application boundary defines the scope of an application. It
encompasses various resources critical to an application’s execution,
such as address space, executable code and the data used by the
application. A typical multiprogramming execution environment,
such as Windows, uses application boundaries to protect one appli-
cation from affecting the execution of another application.
In this section, you’ll learn about the application boundaries sup-
plied by Windows and the .NET Framework. You’ll understand how
application boundaries protect applications from poorly designed or
faulty code. You’ll also learn how application boundaries make it dif-
ficult to design applications that want to communicate beyond
application boundaries.

Process Boundary
A process is an application under execution. Windows isolates
processes from each other to ensure that code running in one process
cannot adversely affect other processes. Windows achieves this isola-
tion by creating a process boundary. A process boundary ensures that
. Each process has its own virtual address space, executable code,
and data.
. A Windows process cannot directly access the code or data of
another Windows process.
. A Windows process runs only one application, so if an applica-
tion crashes, it does not affect other applications.

Process boundaries are a good thing because they allow processes to


coexist. However, it takes lot of system resources to create, monitor,
and terminate a process. In addition, when the processor switches
between the processes, the processor must save and reset the execu-
tion context of the processes. Often an application would involve
several short-lived processes, which require the system to spend a lot
of resources just for process management.
06 0789728206 CH03 2/27/03 9:54 AM Page 172

172 Par t I EXAM PREPARATION

Application Domain Boundary


The Common Language Runtime (CLR) provides a managed execu-
tion environment for the .NET applications. The managed execu-
tion environment provides various services to the executing code;
including cross-language integration, code access security, object life-
time management, and debugging and profiling support. This is
why code executed by the CLR is also known as managed code.
Unlike Windows, the CLR can verify the type-safety of programs to
guarantee that a program does not request resources outside of its
own boundary. The characteristics of the CLR help provide isolation
between running programs at a lower cost than the process boundary.
Instead of a process, the basic unit of isolation for running applica-
tions in the CLR is an application domain. An application domain
(also known as an AppDomain) is the smallest execution unit for a
.NET application. The CLR allows several application domains to
run within a single Windows process and still provides the same level
of isolation between applications as provided by a Windows process.
The application domains achieve isolation through application
domain boundaries, which ensure that
. Each application domain contains its own set of code, data,
and configuration settings.
. An application domain cannot directly access the code or data
structures of another application domain.
. Code running in one application domain cannot affect other
application domains. The CLR can terminate an application
domain without stopping the entire process.

Creating, monitoring, and maintaining an application domain uses fewer


resources than performing the same operations with a process. In addi-
tion, the capability of an application domain to run multiple applications
within the same process reduces the overhead of process switching. Thus,
application domains increase the performance of the applications.
You can create an application domain in a program using the
AppDomain class of System namespace. However, in most cases the
application domains are created and managed by the runtime hosts
that execute your code. Runtime hosts provide the environment to
run managed code on behalf of the user. When you install the .NET
Framework, you get three runtime hosts already configured—the
Windows shell, ASP.NET, and Internet Explorer.
06 0789728206 CH03 2/27/03 9:54 AM Page 173

Chapter 3 .NET REMOTING 173

DISTRIBUTED APPLICATIONS
From the previous sections, you can see that both process and appli-
cation domains provide a closely protected environment. As a result,
objects in a process or an application domain cannot directly talk to
objects in another process or application domain.
However, in the increasingly connected world, enterprises and users
demand distributed applications. Distributed applications allow
objects to talk across process boundaries. Often, distributed applica-
tions also meet the following objectives:
. Establishes communication between objects that run in differ-
ent application domains and processes whether on the same
computer or across the Internet.
. Enables enterprise application integration by establishing
communication between objects that run on heterogeneous
architectures.
. Enables application availability by making sure that portions
of an application continue to run even if some components are
busy or have failed.
. Provides increased security and scalability by dividing the
application into several layers (or tiers).

Evolution of Distributed Applications


A well-designed distributed application has the potential to be more
connected, more available, more scalable, and more robust than an
application in which all components run on a single computer. This
is a desirable model for an enterprise application.
Several efforts have been made to design frameworks for developing
distributed applications. A few well-known frameworks are
Distributed Computing Environment/Remote Procedure Calls
(DEC/RPC), Microsoft Distributed Component Object Model
(DCOM), Common Object Request Broker Architecture (CORBA), and
Java Remote Method Invocation (RMI). Some of these implementa-
tions are widely deployed in enterprises.
06 0789728206 CH03 2/27/03 9:54 AM Page 174

174 Par t I EXAM PREPARATION

However, modern business requirements are different from earlier


days. Today, businesses seek solutions that can be developed rapidly,
that integrate well with their legacy applications, and that interoper-
ate well with their business partners. Each of the previously
mentioned technologies failed to satisfy one or more of these
requirements.
In 2000, Microsoft introduced the .NET Framework for designing
next-generation, distributed applications. As you’ll explore more in
this book, the .NET Framework is specifically targeted to meet the
needs of modern business whether the need is rapid development,
integration, or interoperability.

Developing Distributed Applications


Using the .NET Framework
The .NET Framework provides various mechanisms to support dis-
tributed application development. Most of this functionality is pre-
sent in the following three namespaces of the Framework Class
Library (FCL):
. The System.Net Namespace—This namespace includes classes
to create standalone listeners and custom protocol handlers to
start from scratch, as well as to create your own framework for
developing a distributed application. Working with the
System.Net namespace directly requires a good understanding
of network programming.
. The System.Runtime.Remoting Namespace—This namespace
includes the classes that constitutes the .NET remoting frame-
work. The .NET remoting framework enables communication
between objects living in different application domains
whether or not they are on the same computer. Remoting pro-
vides an abstraction over the complexities of network program-
ming and exposes a simple mechanism for interapplication
domain communication. The key objectives of .NET remoting
are flexibility and extensibility.
06 0789728206 CH03 2/27/03 9:54 AM Page 175

Chapter 3 .NET REMOTING 175

. The System.Web.Services Namespace—This namespace


includes the classes that constitute the ASP.NET Web services
framework. ASP.NET Web services enable objects living in
different application domains to exchange messages using stan-
dard protocols such as HTTP and SOAP. ASP.NET Web ser-
vices, when compared to remoting, provide a much higher
level of abstraction and simplicity. The key objectives of
ASP.NET Web services are ease of use and interoperability
with other systems.

Both .NET remoting and ASP.NET Web services provide a com-


plete framework for designing distributed applications. Most pro-
grammers will use either .NET remoting or ASP.NET Web services
instead of building a distributed programming framework from
scratch using the System.Net namespace classes.
The functionality offered by .NET Remoting and ASP.NET Web
services appears very similar to each other. In fact, ASP.NET Web
services are actually built on the .NET Remoting infrastructure. It is
also possible to design Web services using .NET remoting. Given
the amount of similarity, how do you choose one over another in
your project? Simply put, the decision depends on the type of appli-
cation you want to create. You’ll use
. .NET Remoting when both of the endpoints (client and server)
of a distributed application are in your control. This might be a
case when an application has been designed for use within a
corporate network.
. ASP.NET Web services when one of the endpoints of a distrib-
uted application is not in your control. This might be a case
when your application is interoperating with your business
partner’s application.

You will learn more about architectural differences and the specific
features of both technologies as you progress through this book. I
will discuss .NET remoting in this chapter and I will discuss
ASP.NET Web services in Chapter 4, “Basic Web Services,” and
Chapter 5, “Advanced Web Services.”
06 0789728206 CH03 2/27/03 9:54 AM Page 176

176 Par t I EXAM PREPARATION

R E V I E W B R E A K
. An Application domain, or AppDomain, is the smallest execu-
tion unit for a .NET application. The CLR allows several
application domains to run within a single Windows process.
. Distributed Applications enable communication between
objects that run in different application domains and
processes.
. The System.Net, System.Runtime.Remoting, and
System.Web.Services namespaces enable the .NET Framework
to support distributed application development.

.NET REMOTING ARCHITECTURE


.NET remoting enables objects in different application domains to
talk to each other. The real strength of remoting is in enabling com-
munication between objects when their application domains are
separated across the network. In this case, remoting transparently
handles details related to network communication.
Before getting into details, let’s first answer a basic question—How
can remoting establish cross-application domain communication
when an application domain does not allow direct calls across its
boundary?
Remoting takes an indirect approach to application domain commu-
nication by creating proxy objects as shown in Figure 3.1. Both
application domains communicate with each other using the follow-
ing steps:
1. When a client object wants to create an instance of the server
object, the remoting system at the client side instead creates a
proxy of the server object. The proxy object lives at the client
but behaves just like the remote object; this leaves the client
with the impression that the server object is in the client’s
process.
06 0789728206 CH03 2/27/03 9:54 AM Page 177

Chapter 3 .NET REMOTING 177

FIGURE 3.1
In this simplified view of .NET remoting, note
that the client and server communicate indi-
Client object Server Object rectly through a proxy object.

Proxy

Remoting Remoting
System Channel System

Client Application Server Application


Domain Domain

2. When the client object calls a method on the server object, the
proxy passes the call information to the remoting system on
the client. This remoting system in turn sends the call over the
channel to the remoting system on the server.
3. The remoting system on the server receives the call informa-
tion and, on the basis of it, invokes the method on the actual
object on the server (creating the object if necessary).
4. The remoting system on the server collects all the results of
the method invocation and passes them through the channel
to the remoting system on the client.
5. The remoting system at the client receives the response of the
server and returns the results to the client object through the
proxy.

The process of packaging and sending method calls among the


objects across the application boundaries via serialization and deseri-
alization, as shown in the preceding steps, is also known as mar-
shalling.
06 0789728206 CH03 2/27/03 9:54 AM Page 178

178 Par t I EXAM PREPARATION

Now that you have a basic idea of how .NET remoting works, its
time to get into details. In the next few sections, I’ll explain the vari-
ous key components and terminology of .NET remoting.

Object Marshalling
Remotable objects are objects that can be marshalled across the
application domains. In contrast, all other objects are known as non-
remotable objects. There are two types of remotable objects:
. Marshal-by-value (MBV) Objects—These objects are copied
and passed out of the server application domain to the client
application domain.
. Marshal-by-reference (MBR) Objects—These objects are
accessed on the client side using a proxy. The client just holds
a reference to these objects.

Marshal-by-Value Objects
MBV objects reside on the server. However, when the client invokes
a method on the MBV object, the MBV object is serialized, trans-
ferred over the network, and restored on the client as an exact copy
of the server-side object. The method is then invoked directly on the
client. When this happens, the MBV object is no longer a remote
object. Any method calls to the object do not require any proxy
object or marshalling because the object is locally available.
The MBV objects can provide faster performance by reducing the
number of network roundtrips, but in the case of large objects, the
time taken to transfer the serialized object from the server to the
client can be very significant. Further, MBV objects do not allow
you the flexibility to run the remote object in the server environ-
ment.
A MBV object can be created by declaring a class with the
Serializable attribute; for example

‘ Define a MBV remoting object


<Serializable()>
Public Class MyMBVObject
‘ Implementation details
End Class
06 0789728206 CH03 2/27/03 9:54 AM Page 179

Chapter 3 .NET REMOTING 179

If a class needs to control its own serialization, it can do so by


implementing the ISerializable interface as shown here:
Imports System.Runtime.Serialization

‘ Define a MBV remoting object


<Serializable()>
Public Class MyMBVObject
Implements ISerializable

‘ Class details ...

‘Implement custom serialization here


Public Sub GetObjectData( _
ByVal info As SerializationInfo, _
ByVal context As StreamingContext)
‘ Serialization details ...
End Sub

End Class

Marshal-by-Reference Objects
MBR objects are remote objects. They always reside on the server,
and all methods invoked on these objects are executed at the server
side. The client communicates with the MBR object on the server
using a local proxy object that holds the reference to the MBR
object.
Although the use of MBR objects increases the number of network
roundtrips, they are a good choice when the objects are prohibitively
large or when the functionality of the object is only available in the
server environment on which it is created.
An MBR object can be created by deriving from the
System.MarshalByRefObject class; for example,

‘ Define a MBR remoting object


Public Class MyMBRObject
Inherits MarshalByRefObject
‘ Class details ...
End Class

Channels
Create and consume a .NET Remoting object: Select a
channel protocol and a formatter. Channel protocols
include TCP and HTTP. Formatters include SOAP and
binary.
06 0789728206 CH03 2/27/03 9:54 AM Page 180

180 Par t I EXAM PREPARATION

Channels are the objects that transport messages across remoting


boundaries such as application domains, processes, and computers.
When a client calls a method on a remote object, the details of the
method call are transported to the remote object through a channel.
Any results returned from the remote object are communicated back
to the client again through the same channel.
The .NET remoting framework ensures that before a remote object
can be called, it has registered at least one channel with the remoting
system on the server. Similarly, the client object should specify a
channel before it can communicate with a remote object. If the
remote object offers more than one channel, the client can connect
using the channel that best suits its requirements.
A channel has two endpoints. The channel object at the receiving
Port Numbers Should Be Unique end of a channel (the server) listens to a particular protocol using the
TIP

on a Machine Each channel is specified port number, whereas the channel object at the sending
uniquely associated with a TCP/IP end of the channel (the client) sends information to the receiving
EXAM

port number. Ports are machine-


end using the protocol and port number specified by the channel
wide resources; therefore, you can-
object on the receiving end.
not register a channel that listens
on a port number already in use by To participate in the .NET remoting framework, the channel object
some other channel on the same at the receiving end must implement the IChannelReceiver interface
machine. while the channel object at the sending end must implement the
IChannelSender interface.

The .NET Framework provides implementations for HTTP


(Hypertext Transfer Protocol) and TCP (Transmission Control
Protocol) channels. If you want to use a different protocol, you can
define your own channel by implementing the IChannelReceiver
and IChannelSender interfaces.

HTTP Channels
HTTP channels use HTTP for establishing communication between
the two ends. These channels are implemented through the classes of
the System.Runtime.Remoting.Channels.Http namespace, as shown
in Table 3.1.
06 0789728206 CH03 2/27/03 9:54 AM Page 181

Chapter 3 .NET REMOTING 181

TABLE 3.1
T H E HTTP C H A N N E L C L A S S E S
Class Implements Purpose
HttpServerChannel IChannelReceiver An implementation for a server
channel that uses the HTTP protocol
to receive messages
HttpClientChannel IChannelSender An implementation for a client
channel that uses the HTTP protocol
to send messages

HttpChannel IChannelReceiver An implementation of a combined


and channel that provides the
IChannelSender functionality of both the
HttpServerChannel and the
HttpClientChannel classes

The following code example shows how to register a sender-receiver


HTTP channel on port 1234:
Imports System
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Http

‘ ...

Dim channel As HttpChannel = New HttpChannel(1234)


ChannelServices.RegisterChannel(channel)

‘ ...

The ChannelServices class used in the code provides various remoting-


related services. One of its static methods is RegisterChannel(),
which helps in registering a channel with the remoting framework.

TCP Channels
A TCP channel uses TCP for establishing communication between
the two ends. The TCP channel is implemented through various
classes of the System.Runtime.Remoting.Channels.Tcp namespace, as
shown in Table 3.2.
06 0789728206 CH03 2/27/03 9:54 AM Page 182

182 Par t I EXAM PREPARATION

TABLE 3.2
T H E TCP C H A N N E L C L A S S E S
Class Implements Purpose
TcpServerChannel IChannelReceiver An implementation for a server
channel that uses the TCP protocol
to receive messages
TcpClientChannel IChannelSender An implementation for a client
channel that uses the TCP protocol
to send messages

TcpChannel IChannelReceiver An implementation of a combined


and channel that provides the
IChannelSender functionality for both the
TcpServerChannel and
TcpClientChannel classes

The following code example shows how to register a sender-receiver


TCP channel on port 1234:
Imports System
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp

‘ ...

Dim channel As TcpChannel = New TcpChannel(1234)


ChannelServices.RegisterChannel(channel)

‘ ...
Support for Security .NET remot-
TIP

ing has no built-in support for secu-


rity. It instead depends on the Choosing Between the HTTP and the TCP
EXAM

remoting hosts to provide security. Channels


The only built-in remoting host that
provides security for remote objects Table 3.3 helps you make a decision about which channel to use in
is IIS. Therefore, any secured a given scenario. In summary, you’ll normally limit the use of TCP
objects must be hosted in IIS channel to within a low-risk intranet. For more wide-reaching
unless you write your own security applications, using HTTP channel makes more sense unless the
system. application’s efficiency requirements justifies the cost of creating a
customized security system.
06 0789728206 CH03 2/27/03 9:54 AM Page 183

Chapter 3 .NET REMOTING 183

TABLE 3.3
C H O O S I N G B E T W E E N HTTP C H A N N E L AND TCP C H A N N E L
Channel Scope Efficiency Security
HttpChannel Wide, HTTP channel allows you to Less, because HTTP is a More, because when remote objects
host objects in a robust HTTP server bulky protocol and has a lot of are hosted in IIS, the HttpChannel can
such as IIS. HTTP channels can be extra overhead. immediately take advantage of Secure
used over the Internet because Sockets Layer (SSL), Integrated
firewalls do not generally block Windows Authentication, or Kerberos.
HTTP communication.

TcpChannel Narrow, Using TCP channel over More, because TCP uses raw Less, until you implement a custom
the Internet would require opening sockets to transmit data across security system using the classes provided
certain ports in the firewall and the network. in the System.Security namespace.
could lead to security breaches.

Formatters
Formatters are the objects used to encode and serialize data into
messages before they are transmitted over a channel.
To participate in the .NET remoting framework, a formatter class
must implement the IFormatter interface. The .NET Framework
packages two formatter classes for common scenarios—the
BinaryFormatter class and the SoapFormatter class. If you want to
use a different formatter, you can define your own formatter class by
implementing the IFormatter interface.

The SOAP Formatter


SOAP (Simple Object Access Protocol) is a relatively straightfor-
ward, XML-based protocol for exchanging types and messages
between applications. SOAP is an extensible and modular protocol;
it is not bound to a particular transport mechanism such as HTTP
or TCP.
The SOAP formatter is implemented in the SoapFormatter class of
the System.Runtime.Serialization.Formatters.Soap namespace.
SOAP formatting is an ideal way of communicating between appli-
cations that use incompatible architectures. However, SOAP is very
verbose. SOAP messages require more bytes to represent data than
the equivalent binary messages.
06 0789728206 CH03 2/27/03 9:54 AM Page 184

184 Par t I EXAM PREPARATION

The Binary Formatter


Unlike SOAP, the binary format used by the .NET Framework is
proprietary and can be understood only within .NET applications.
However, as compared to SOAP, the binary format of representing
messages is very compact and efficient.
The binary formatter is implemented in the BinaryFormatter class of
the System.Runtime.Serialization.Formatters.Binary namespace.

Channels and Formatters


The HTTP channel uses the SOAP formatter as its default formatter
to transport messages to and from the remote objects. The
HTTP channel uses SoapClientFormatterSinkProvider and
SoapServerFormatterSinkProvider classes to serialize and deserialize
messages using SoapFormatter. You can create industry-standard XML
Web services by using the SOAP formatter with the HTTP channel.
The TCP channel uses the binary format by default to transport
messages to and from the remote object. The TCP channel uses
BinaryClientFormatterSinkProvider and
BinaryServerFormatterSinkProvider classes to serialize and deserial-
ize messages using BinaryFormatter.
However, channels are configurable. You can configure the HTTP
channel to use the binary formatter or a custom formatter instead of
the SOAP formatter. Similarly, the TCP channel can be configured
to use the SOAP formatter or a custom formatter instead of the
binary formatter.
Figure 3.2 compares the various combinations of channels and for-
matters on the scale of efficiency and compatibility. The protocols at
the top of the list are the most efficient, while those at the bottom of
the list are most interoperable. You can use this information to
decide which combination of channel and formatter you would
choose in a given scenario.

FIGURE 3.2
Channel Formatter Efficiency Interoperability
TCP channel with binary formatter provides
maximum efficiency whereas HTTP channel with TCP Binary
SOAP formatter provides maximum interoper-
ability. TCP SOAP

HTTP Binary

HTTP SOAP
06 0789728206 CH03 2/27/03 9:54 AM Page 185

Chapter 3 .NET REMOTING 185

R E V I E W B R E A K
. .NET remoting enables objects in different application
domains to talk to each other even when they are separated by
applications, computers, or the network.
. The process of packaging and sending method calls among
objects across the application boundaries via serialization and
deserialization is called marshalling.
. Marshal-by-value (MBV) and Marshal-by-reference (MBR) are
the two types of remotable objects. MBV objects are copied to
the client application domain from the server application
domain, whereas only a reference to the MBR objects is main-
tained in the client application domain. A proxy object is cre-
ated at the client side to interact with the MBR objects.
. A channel is an object that transports messages across remot-
ing boundaries such as application domains, processes, and
computers. The .NET Framework provides implementations
for HTTP and TCP channels to enable communication of
messages over the HTTP and TCP protocols, respectively.
. A channel has two endpoints. A channel at the receiving end,
the server, listens for messages at a specified port number from
a specific protocol; and a channel object at the sending end,
the client, sends messages through the specified protocol at the
specified port number.
. Formatters are the objects used to serialize and deserialize data
into messages before they are transmitted over a channel. You
can format messages in SOAP or the binary format with the
help of the SoapFormatter and BinaryFormatter classes in
the FCL.
. The default formatter to transport messages to and from the
remote objects for the HTTP channel is the SOAP formatter
and for the TCP channel is the binary formatter.

Remote Object Activation


Between the two types of objects that you have seen (MBV objects
and MBR objects), only MBR objects can be activated remotely.
06 0789728206 CH03 2/27/03 9:54 AM Page 186

186 Par t I EXAM PREPARATION

No remote activation is needed in the case of MBV objects because


Remotable Members An MBR
TIP

the MBV object itself is transferred to the client side.


object can remote the following
types of members: Based on the activation mode, an MBR object is classified into one
EXAM

of the following two categories:


• Non-static public methods
. Server-activated objects
• Non-static public properties

• Non-static public fields . Client-activated objects

Server-Activated Objects
Server-activated objects (SAO) are those remote objects whose life-
time is directly controlled by the server.
When a client requests an instance of a server-activated object, a
proxy to the remote object is created in the client’s application
domain. The remote object is only instantiated (or activated) on the
server when the client calls a method on the proxy object.
Server-activated objects provide limited flexibility because they can
only be instantiated using their default (parameter-less) constructors.
NOTE

Well-Known Objects Remote objects


activated in SingleCall or Singleton There are two possible activation modes for a server-activated object:
activation mode are also known as . SingleCall activation mode
well-known objects.
. Singleton activation mode

SingleCall Activation Mode


In the SingleCall activation mode, an object is instantiated for the
sole purpose of responding to just one client request. After the
request is fulfilled, the .NET remoting framework deletes the object
and reclaims its memory.
Objects activated in the SingleCall mode are also known as stateless
because the objects are created and destroyed with each client
request; therefore, they do not maintain state across requests. This
behavior of SingleCall mode allows for greater server scalability as an
object consumes server resources only for a small period, therefore
allowing the server to allocate resources to other objects.
The SingleCall activation mode is a desired solution when
. The overhead of creating an object is not significant.
. The object is not required to maintain its state.
06 0789728206 CH03 2/27/03 9:54 AM Page 187

Chapter 3 .NET REMOTING 187

. The server needs to support a large number of requests for the


Load-Balancing and SingleCall

TIP
object.
Activation Sometimes to improve
. The object needs to be supported in a load-balanced environment. the overall efficiency of an applica-

EXAM
tion, it might be hosted on multiple
Common scenarios of the SingleCall activation mode are those servers that share the incoming
applications in which the object is required by the client to do a requests to the application. In this
small amount of work and then the object is no longer required. case, a request can go to any of
Some common examples are retrieving the inventory level for an the available servers for process-
ing. This scenario is called a load-
item, displaying tracking information for a shipment, and so on.
balancing environment.

Singleton Activation Mode Because the SingleCall objects are


stateless, it does not matter which
In the Singleton activation mode, at most there will be one instance
server processes requests for such
of the remote object regardless of the number of clients accessing it.
objects. For this reason, SingleCall
A Singleton-mode object can maintain state information across activation is ideally suited for load-
method calls. For this reason, such objects are also sometimes balanced environments.
known as stateful objects. The state maintained by the Singleton-
mode object is globally shared by all of its clients. This generally
means that you should not store any state in Singleton-mode
objects. But there are circumstances (such as keeping track of usage
statistics) in which it can make sense to store shared state.
A Singleton object does not exist on the server forever. Its lifetime is
determined by the lifetime lease of the object. I’ll discuss lifetime
leases later in the chapter.
A Singleton object is a desired solution when
. The overhead of creating an object is substantial.
. The object is required to maintain its state over a prolonged
period.
. Several clients need to work on the shared state.

Singleton activation mode is useful in scenarios such as in a chat


server in which multiple clients talk to the same remote object and
share data between one another through this object.

Client-Activated Objects
Client-activated objects (CAO) are those remote objects whose life-
time is directly controlled by the client. This is in direct contrast
with SAOs, where the server, not the client, has the complete con-
trol over the lifetime of the objects.
06 0789728206 CH03 2/27/03 9:54 AM Page 188

188 Par t I EXAM PREPARATION

Client-activated objects are instantiated on the server as soon as the


client requests the object to be created. Unlike an SAO, a CAO does
not delay the object creation until the first method is called on the
object.
A CAO can be created using any of the available constructors for the
class. A typical CAO activation involves the following steps:
1. When the client attempts to create an instance of the server
object, an activation request message is sent to the remote
server.
2. The server then creates an instance of the requested class using
the specified constructor and returns an ObjRef object to the
client application that invoked it. The ObjRef object contains
all the required information to generate a proxy object that is
capable of communicating with a remote object.
3. The client uses the ObjRef object to create a proxy for the
server object on the client side.

An instance of a CAO serves only the client responsible for its cre-
ation, and the CAO doesn’t get discarded with each request. For this
reason, a CAO can maintain state with each client that it is serving,
but unlike the Singleton SAO, different CAOs cannot share a com-
mon state.
The lifetime of a CAO is determined using lifetime leases. I’ll talk
more about this topic shortly in a section titled “Lifetime Leases.”
A CAO is a desired solution when
. The clients want to maintain a private session with the remote
object.
. The clients want to have more control over how the objects are
created and how long they will live.

A CAO is useful in scenarios such as entering a complex purchase


order in which multiple roundtrips are involved and clients want to
maintain their own private state with the remote object.

Comparing the Object Activation Techniques


Based on the discussions in the previous section, the various object
activation techniques can be compared as shown in Figure 3.3.
06 0789728206 CH03 2/27/03 9:54 AM Page 189

Chapter 3 .NET REMOTING 189

The SingleCall server activation has maximum scalability because


such objects occupy server resources for the minimum amount of
the time. This enables the server to allocate its resources between a
large numbers of clients.
On the other hand, the client activation of remote objects offers
maximum flexibility because you have complete control over the
construction and lifetime of the remote object.

FIGURE 3.3
Activation Type Flexibility Scalability
The SingleCall server activation offers maxi-
SingleCall Server
mum scalability, whereas the client activation
Activation offers maximum flexibility.
Singleton Server
Activation

Client Activation

Lifetime Leases Leases and Activation Mode

TIP
A lifetime lease is the period of time that a particular object can be Leases apply only to Singleton and
active in memory before the .NET framework deletes it and reclaims Client Activated objects. In the

EXAM
its memory. Both Singleton SAO and CAO use lifetime leases to SingleCall activation mode, objects
are created and destroyed with
determine how long they should continue to exist.
each method call.
A lifetime lease is represented using an object that implements the
ILease interface defined in the System.Runtime.Remoting.Lifetime
namespace. Some of the important members of this interface are list-
ed in Table 3.4.

TABLE 3.4
I M P O R TA N T M E M B E R S OF THE ILease I N T E R FA C E
Member Name Type Description
CurrentLeaseTime Property Returns the amount of time remaining on the
lease as a TimeSpan object.
InitialLeaseTime Property Lets you supply a TimeSpan that dictates the
default lifetime of the object. If the object does
not receive any method calls, it will only live
for this period.
Register() Method Specifies an object to be notified when a lease
needs to be renewed.
continues
06 0789728206 CH03 2/27/03 9:54 AM Page 190

190 Par t I EXAM PREPARATION

TABLE 3.4 continued


Custom and Infinite Lifetimes
TIP

A remote object can choose to I M P O R TA N T M E M B E R S OF THE ILease I N T E R FA C E


have a custom defined lifetime Member Name Type Description
EXAM

by overriding the
Renew() Method Renews a lease for the specified TimeSpan.
InitializeLifetimeService()
method of the base class, RenewOnCallTime Property Each time the remote object is called, the lease
MarshalByRefObject. If the is renewed for this much time.
InitializeLifetimeService() SponsorshipTimeout Property Specifies the amount of time to wait for a
method returns a null value, the sponsor object to respond to a renewal request.
type tells the .NET Remoting sys-
tem that its instances are intended
to have an infinite lifetime. Simply speaking, the lease works as follows:
. When an object is created, its lifetime lease
(CurrentLeaseTime) is set using the value of the
InitialLeaseTime property (which is 5 minutes by default).

. Whenever the object receives a call, its CurrentLeaseTime is


reset to the time specified by the value of the RenewOnCallTime
property (which is 2 minutes by default).
. The client can also renew a lease for a remote object by direct-
ly calling the ILease.Renew() method:
Dim lease As ILease = _
CType(RemotingServices. _
GetLifetimeService(RemoteObject), ILease)
Dim expireTime As TimeSpan = _
lease.Renew(TimeSpan.FromSeconds(60))

. When the value of CurrentLeaseTime reaches 0, the .NET


Framework contacts any sponsors registered with the lease to
check if they are ready to sponsor renewing the object’s lease.
. If the sponsor does not renew the object or the server cannot
contact the sponsor within the duration specified by the
SponsorshipTimeout property, the object is marked for garbage
collection.

Sponsors are the objects responsible for dynamically renewing the


lease of an object in case of its lease expiry. For more information
about sponsors, refer to the “Renewing Leases” topic in the .NET
Framework Developer’s Guide.
06 0789728206 CH03 2/27/03 9:54 AM Page 191

Chapter 3 .NET REMOTING 191

R E V I E W B R E A K
. MBR remotable objects can be activated in two modes: Server-
activated mode and Client-activated mode.
. Server-activated objects (SAO) are those remote objects whose
lifetime is directly controlled by the server.
. You can activate SAOs in two ways: SingleCall (object is creat-
ed for each client request) and Singleton (object is created
once on the server and is shared by all clients).
. The SingleCall activation mode provides the maximum scala-
bility because it does not maintain any state and the object
lives for the shortest duration possible.
. CAO are created for each client when the client requests to
create a remote object. These objects maintain state for each
client with which they are associated.
. The leased-based lifetime process determines how long the
Singleton SAO and CAO should exist.

APPLYING .NET REMOTING


So far, I have discussed the architecture and various concepts related
to the .NET remoting. In this section, you will learn how to apply
these concepts to see remoting in action. In particular, you will learn
how to
. Create a remotable class.
. Create a Server-activated object.
. Create a Client-activated object.
. Use configuration files to configure the remoting framework.
. Use interface assemblies to compile remoting clients.
06 0789728206 CH03 2/27/03 9:54 AM Page 192

192 Par t I EXAM PREPARATION

Creating a Remotable Class


Creating a remotable class is simple. All you need to do is to inherit
a class from the MarshalByRefObject class. Step By Step 3.1 creates a
remotable class named DbConnect. This class connects to a specified
SQL Server database and allows you to execute a SELECT SQL state-
ment using its ExecuteQuery() method.

STEP BY STEP
3.1 Creating a Remotable Class
1. Launch Visual Studio .NET. Select File, New, Blank
Solution, and name the new solution 310C03. Click OK.
2. Add a new Visual Basic .NET Class library named
StepByStep3-1 to the solution.

3. In the Solution Explorer, rename the default Class1.vb to


DbConnect.vb.

4. Open the DbConnect.vb class and replace the code with


the following code:
Imports System
Imports System.Data
Imports System.Data.SqlClient

‘ Marshal-by-Reference Remotable Object


Public Class DbConnect
Inherits MarshalByRefObject
Private sqlconn As SqlConnection

‘ Default constructor connects to the Northwind


‘ database
Public Sub New()
sqlconn = New SqlConnection( _
“data source=(local);” & _
“initial catalog=Northwind;” & _
“integrated security=SSPI”)
Console.WriteLine( _
“Created a new connection “ & _
“to the Northwind database”)
End Sub
06 0789728206 CH03 2/27/03 9:54 AM Page 193

Chapter 3 .NET REMOTING 193

‘ Parameterized constructor connects to the


‘ specified database
Public Sub New(ByVal DbName As String)
sqlconn = New SqlConnection( _
“data source=(local);” & _
“initial catalog=” & DbName & “;” & _
“integrated security=SSPI”)
Console.WriteLine( _
“Created a new connection “ & _
“to the “ & DbName & “ database”)
End Sub

Public Function ExecuteQuery( _


ByVal strQuery As String) As DataSet
Console.Write(“Starting to execute “ & _
“the query...”)
‘ Create a SqlCommand to represent the query
Dim sqlcmd As SqlCommand = _
sqlconn.CreateCommand()
sqlcmd.CommandType = CommandType.Text
sqlcmd.CommandText = strQuery

‘ Create a SqlDataAdapter object


‘ to talk to the database
Dim sqlda As SqlDataAdapter = _
New SqlDataAdapter()
sqlda.SelectCommand = sqlcmd
‘ Create a DataSet to hold the results
Dim ds As DataSet = New DataSet()
Try
‘ Fill the DataSet
sqlda.Fill(ds, “Results”)
Catch ex As Exception
Console.WriteLine(ex.Message, _
“Error executing query”)
End Try
Console.WriteLine(“Done.”)
ExecuteQuery = ds
End Function
End Class

5. Select Build, Build StepByStep3-1. This step packages the


remotable class into the file StepByStep3-1.dll, which is
located in the bin or directory of your project. You can
navigate to it through the Solution Explorer: Just select
the project and click the Show All Files button in the
Solution Explorer toolbar.
06 0789728206 CH03 2/27/03 9:54 AM Page 194

194 Par t I EXAM PREPARATION

You’ve now created a remotable class, but it cannot yet be directly


called from client application domains. For a remotable class to be
activated, you need to connect the class to the remoting framework.
You’ll learn how to do that in the next section.

Creating a Server-Activated Object


A remotable class is usually connected with the remoting framework
through a separate server program. The server program listens to the
client request on a specified channel and instantiates the remote
object or invokes calls on it as required.
It is a good idea to keep the remotable class and server program sep-
arate; this enables the design to be modular and the code to be
reusable.
In this section, I’ll show you how to create a remoting server. Here’s
an overview of the steps that the remoting server must take.
1. Create a server channel that listens on a particular port to the
incoming object activation requests from other application
domains. The following code segment shows how to create a
TCP server channel and an HTTP server channel:
‘ Register a TCP server channel on port 1234
Dim channel As TcpServerChannel = _
New TcpServerChannel(1234)

‘ Register a HTTP server channel on port 1234


Dim channel As HttpServerChannel = _
New HttpServerChannel(1234)

2. Register the channel with the remoting framework. This tells


the framework which requests should be directed to this par-
ticular server. This registration is performed through the
RegisterChannel() method of the ChannelServices class:

‘ Register the channel with remoting framework


ChannelServices.RegisterChannel(channel)

3. Register the remotable class with the remoting framework.


This tells the framework which classes this particular server
can create for remote clients. For a server-activated object, this
registration is performed using the
RegisterWellKnownServiceType() method of the
RemotingConfiguration class, as shown here:
06 0789728206 CH03 2/27/03 9:54 AM Page 195

Chapter 3 .NET REMOTING 195

‘ Register a remote object with the remoting framework


RemotingConfiguration.RegisterWellKnownServiceType( _
GetType(DbConnect), “DbConnect”, _
WellKnownObjectMode.SingleCall)

Here, the first parameter is the type of the remotable class.


Accessing an Object Through

TIP
The second parameter specifies the uniform resource identifier
(URI) through which the server publishes the location of the Multiple Channels From steps 2
and 3, you can note that the chan-
remote object. The last parameter specifies the activation

EXAM
nel registration and the remote
mode. The activation mode can be one of the two possible val-
object registration are not related.
ues of the WellKnownObjectMode enumeration—SingleCall or In fact, a remote object can be
Singleton. accessed through all registered
4. With the channel and class both registered, the remoting serv- channels.
er is ready to go. The remoting framework will direct all
requests for that class via that channel to the registered server.

As I discussed, earlier, an SAO can be activated in two different


modes—SingleCall and Singleton. In the next few sections, I’ll cover
how to create a remoting server for activating objects in each of
these modes. I’ll also tell the story on the other side of the channel;
that is, how to connect the client program to the remoting frame-
work so that it can instantiate the SAO and call methods on it.

Registering a Remotable Class As a Server-


Activated Object Using the SingleCall
Activation Mode
In this section, I’ll demonstrate how to create a server that exposes
the remotable class through the remoting framework. The server
process here will be a long running user interface-less process that
will continue to listen for incoming client requests on a channel.
Ideally, you should write this type of server program as a Windows
service or use an existing Windows service such as Internet
Information Services (IIS) to work as a remoting server. But I have
chosen to write the server program as a console application mainly
because I’ll use the console Window to display various messages that
will help you understand the workings of the remoting server.
Later in this chapter, in the section “Using IIS As an Activation
Agent,” I’ll cover how to use IIS as a remoting server. I’ll talk about
Windows services in Chapter 6, “Windows Services.”
06 0789728206 CH03 2/27/03 9:54 AM Page 196

196 Par t I EXAM PREPARATION

STEP BY STEP
3.2 Registering a Server-Activated Object Using the
SingleCall Activation Mode
1. Add a new Visual Basic .NET Console application named
StepByStep3-2 to the solution.

2. In the Solution Explorer, right-click the project


StepByStep3-2 and select Add Reference from the context
menu. In the Add Reference dialog box (see Figure 3.4),
select the .NET tab, select the System.Runtime.Remoting
component from the list view, and click the Select button.
Now select the Projects tab, select the Project named
StepByStep3-1 (which contains the remotable object) from
the list view, and click the Select button. Both the selected
projects then appear in the Selected Components list, as
shown in Figure 3.4. Click OK.

FIGURE 3.4
The Add Reference dialog box allows you to
add references to components.

3. In the Solution Explorer, rename the default Module1.vb


module to DbConnectSingleCallServer.vb. Open the file
and change the name of the module to
NOTE

Namespace Naming Note that


although the project is named DbConnectSingleCallServer in the module declaration.

StepByStep3-1, the corresponding 4. Add the following Imports directives above the module
namespace is named StepByStep3_1.
declaration:
That’s because the .NET Framework
Imports StepByStep3_1
considers a dash to be an illegal char-
Imports System.Runtime.Remoting
acter in a namespace name. Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
06 0789728206 CH03 2/27/03 9:54 AM Page 197

Chapter 3 .NET REMOTING 197

5. Add the following code in the Main() procedure:


Public Sub Main()
‘ Create and Register a TCP server channel
‘ that listens on port 1234
Dim channel As TcpServerChannel = _
New TcpServerChannel(1234)
ChannelServices.RegisterChannel(channel)

‘ Register the service that publishes


‘ DbConnect for remote access in SingleCall mode
RemotingConfiguration. _
RegisterWellKnownServiceType( _
GetType(StepByStep3_1.DbConnect), “DbConnect”, _
WellKnownObjectMode.SingleCall)

Console.WriteLine(“Started server in the “ & _


“SingleCall mode”)
Console.WriteLine(“Press <ENTER> to terminate “ & _
“server...”)
Console.ReadLine()
End Sub

6. Right-click on the StepByStep3-2 project in the Solution


Explorer and select Properties. Change the Startup object
to DbConnectSingleCallServer.
7. Build the project. This step creates a remoting server that is
capable of registering the StepByStep3_1.DbConnect class for
remote invocation using the SingleCall activation mode.

Step by Step 3.2 uses a receiver TCP channel (TcpServerChannel) to


register a remotable class with the remoting framework. However,
converting this program to use HTTP channel is not difficult—you
just need to change all instances of Tcp to Http.
Step By Step 3.2 creates a remoting host that listens on port 1234.
This is an arbitrary port number that might or might not work on
your computer. A good idea is to check whether a port is already in
use by some other application before running this program. You can
do this from the command line on a particular computer by using
the Windows netstat command.
This suggestion works only in a test scenario. It is not reasonable to
instruct a customer to check whether the port is available before he
starts the application. If the application will run entirely on your
company’s network, you can safely use a port in the private port
range of 49152 through 65535—provided, of course, that the port
number you choose is not used by any other internal application.
06 0789728206 CH03 2/27/03 9:54 AM Page 198

198 Par t I EXAM PREPARATION

In case you are distributing the application, you should get a port
number registered with the IANA (Internet Assigned Numbers
Authority). You can see a list of already assigned port numbers at
www.iana.org/assignments/port-numbers.

Instantiating and Invoking a Server-


Activated Object
At this stage, you have a remotable object as well as a remoting serv-
er ready. In this section, I’ll show you how to create a remoting
client and use it to send messages to the remoting server to activate
the remote object. In order to achieve this, the remoting client needs
to take the following steps:
1. Create and register a client channel that is used by the remot-
ing framework to send messages to the remoting server. The
type of the channel used by the client should be compatible
with the channel used by server. The following examples show
how to create a TCP client channel and an HTTP client
channel:
‘ Create and register a TCP client channel
Client Channel Registration You Dim channel As TcpClientChannel = _
TIP

New TcpClientChannel()
do not specify a port number when
ChannelServices.RegisterChannel(channel)
you register the client channel. The
EXAM

port number is instead specified at ‘ Create and register a HTTP client channel
the time you register the remote Dim channel As HttpClientChannel = _
New HttpClientChannel()
class in the client’s domain.
ChannelServices.RegisterChannel(channel)

2. Register the remotable class as a valid type in the client’s appli-


cation domain. This registration is performed using the
RegisterWellKnownClientType() method of the
RemotingConfiguration class as shown here:

‘ Register the remote class as a valid


‘ type in the client’s application domain
RemotingConfiguration.RegisterWellKnownClientType( _
GetType(DbConnect), “tcp://localhost:1234/DbConnect”)

Here, the first parameter is the type of the remotable class. The
second parameter specifies the uniform resource identifier (URI)
through which the server publishes the location of the remote
object. Localhost maps to your local development machine.
06 0789728206 CH03 2/27/03 9:54 AM Page 199

Chapter 3 .NET REMOTING 199

If the remote object is on some other computer, you’ll replace


Instantiating a Server-Activated

TIP
localhost with the name of the computer.
Object You can only instantiate an
3. Instantiate the SAO on the server. You can only use the default SAO at client side using its default

EXAM
constructor. constructor.
‘ Instantiate the remote object
DbConnect dbc = new DbConnect()

I’ll demonstrate the preceding steps in Step by Step 3.3. In this


example, I’ll create the client program as a Windows application that
accepts a SQL statement from the user and passes it to the
remotable object. The rows returned by the remotable object are dis-
played in a DataGrid control.

STEP BY STEP
3.3 Instantiating and Invoking a Server-Activated
Object
1. Add a new Visual Basic .NET Windows Application
named StepByStep3-3 to the solution.
2. Add references to the .NET assembly
System.Runtime.Remoting and the project StepByStep3-1
(the remotable class assembly).
3. In the Solution Explorer, delete the default Form1.vb. Add
a new form named DbConnectClient.vb. Set the new form
as the Startup object for the project.
4. Add the following directives to the form’s module:
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports StepByStep3_1

5. Place two GroupBox controls, a TextBox control (txtQuery),


a Button control (btnExecute) and a DataGrid control
(dgResults) on the form. Set the Multiline property of
txtQuery to True. Arrange the controls as shown in
Figure 3.5.
continues
06 0789728206 CH03 2/27/03 9:54 AM Page 200

200 Par t I EXAM PREPARATION

continued

FIGURE 3.5
This form invokes a method of a remote object
to execute the given query.

6. Add the following code to the form directly after the


designer-generated code:
‘ Declare a Remote object
Dim dbc As DbConnect

7. Double-click the form and add the following code in the


Load event handler:

Private Sub DbConnectClient_Load( _


ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
‘ Register a TCP client channel
Dim channel As TcpClientChannel = _
New TcpClientChannel()
ChannelServices.RegisterChannel(channel)

‘ Register the remote class as a valid


‘ type in the client’s application domain
RemotingConfiguration. _
RegisterWellKnownClientType( _
GetType(DbConnect), _
“tcp://localhost:1234/DbConnect”)

‘ Instantiate the remote class


dbc = New DbConnect()
End Sub

8. Double-click the Button control and add the following


code in the Click event handler:
Private Sub btnExecute_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExecute.Click
Try
‘ Invoke a method on the remote object
Me.dgResults.DataSource = _
dbc.ExecuteQuery(Me.txtQuery.Text)
dgResults.DataMember = “Results”
06 0789728206 CH03 2/27/03 9:54 AM Page 201

Chapter 3 .NET REMOTING 201

Catch ex As Exception
MessageBox.Show(ex.Message, _
“Query Execution Error”)
End Try
End Sub

9. Right-click on the name of the solution in the Solution


Explorer window and select Properties. This opens the
Solution Property Pages dialog box. In the dialog box,
select the Multiple Startup Projects check box, select the
action Start for StepByStep3-2 and StepByStep3-3, and set
the action to None for other projects as shown in Figure
3.6. Make sure that StepByStep3-2 is placed above
StepByStep3-3. If it isn’t already there, click the Move Up
and Move Down buttons to get the right order.

FIGURE 3.6
Use the Solution Property Pages dialog box to
control which projects are started and in what
order.

10. Build the project. Select Debug, Start to run the project.
You should see a command window displaying a message
that the server is started in the SingleCall mode.
11. Shortly afterward, you’ll see a Windows form for the client
program. Enter a query in the text box and click the
Execute Query button. The client invokes a method on
the remote object and binds the results from the remote
method to the DataGrid control as shown in Figure 3.7.
continues
06 0789728206 CH03 2/27/03 9:54 AM Page 202

202 Par t I EXAM PREPARATION

continued
FIGURE 3.7
The remoting client populates the data grid with
the result returned from the method invocation
on a remote object.
NOTE

Starting Multiple Instances of a


Program When you run a project
from the Visual Studio .NET IDE, the
default behavior is to run the program
in debug mode. However, debug mode
does not allow you to start another
program from the IDE until you finish
the current execution. This can be
inconvenient if you want to start multi- In the preceding steps, I have chosen to start the client and server
ple client programs from within Visual
programs within the Visual Studio .NET IDE. You can also start
Studio. A solution is that you can set
these programs by clicking on StepByStep3-2.exe and StepByStep3-
the project you want to run first as
3.exe, respectively, from the Windows explorer. Note that the server
the startup object and select Debug,
Start Without Debugging to run the program should be running before you click on the Execute Query
project. This won’t lock Visual Studio button on the client program.
.NET in the debug mode, and you’ll be At the end of Step By Step 3.3 when you look at the console win-
able to run more programs from with-
dow of the server program, you’ll note the output as shown in Figure
in IDE using the same technique.
3.8. You’ll note that the remote object is created every time you click
on the Execute Query button. That’s normal behavior based on what
you’ve learned so far in the chapter; however, what’s peculiar is that
for the first call, the constructor is called twice.

FIGURE 3.8
The SingleCall remoting server creates a new
instance of the remote object with each
request.

In fact, calling the constructor twice for the first request on a server
is also a regular behavior of SingleCall object activation. The follow-
ing two points explain why this happens:
06 0789728206 CH03 2/27/03 9:54 AM Page 203

Chapter 3 .NET REMOTING 203

. The first call to constructor is made by the remoting frame-


work at the server side to check whether it is okay to call this
object remotely and to check the activation mode of the object.
. The second constructor is called because of the client’s call on
the remote object. In the case of SingleCall activation, the
server does not preserve the state of the constructor that was
called earlier; therefore, the object has to be re-created with
each client request.

You might wonder why I included a reference to StepByStep3-1.dll


in this project. You might support your argument by saying that the
DbConnect class contained in the StepByStep3-1.dll is a remotable
class and its right place is on the server and not on the client.
Well said, but I have included StepByStep3-1.dll because of the fol-
lowing reasons:
. The client project StepByStep3-3 won’t compile without
it—The reason is that I am referring to the DbConnect class in
the project StepByStep3-3, and the project StepByStep3-3 by
itself has no definition of DbConnect. When I include a refer-
ence to StepByStep3-1.dll, the project StepByStep3-3 can
resolve the definition for DbConnect from there and allow me
to compile the project.
. The client program StepByStep3-3.exe won’t execute with-
out it—I can’t remove StepByStep3-1.dll from the project
directory after the compilation is successfully completed. The
StepByStep3-1.dll is required again at the time of running the
client. This is because to create the proxy object for the
DbConnect class, the CLR must have the metadata that
describes DbConnect. This metadata is read from the assembly
stored in StepByStep3-1.dll.

In some cases, this isn’t a good real-life solution because the


StepByStep3-1.dll might contain important business logic that you
do not want your customers to decompile.
You’re right! And, I have a solution for that in the form of interface
assemblies. With interface assemblies, you just share the interface of
an assembly with your customers, not the actual business logic. I’ll
show you how to create interface assemblies later in this chapter.
06 0789728206 CH03 2/27/03 9:54 AM Page 204

204 Par t I EXAM PREPARATION

Registering a Remotable Class As a Server-


Activated Object Using the Singleton
Activation Mode
The same remotable object can be activated in different modes with-
out making any changes to the remotable object itself. In case of
SAO, the choice of activation mode is totally with the server. In this
section, I’ll show you how to create a remoting server that publishes
the DbConnect class as an SAO using the Singleton activation mode.
I’ll use the same client program that was created in Step By Step 3.3
to test this Singleton server.

STEP BY STEP
3.4 Registering a Server-Activated Object Using the
Singleton Activation Mode
1. Add a new Visual Basic .NET Console application named
StepByStep3-4 to the solution.

2. Add references to the .NET assembly


System.Runtime.Remoting and the project StepByStep3-1
(the remotable class assembly).
3. In the Solution Explorer, rename the default Module1.vb
to DbConnectSingletonServer.vb. Open the file and
change the name of the Module to
DbConnectSingletonServer in the module declaration.

4. Add the following directives:


Imports StepByStep3_1
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp

5. Add the following code in the Main() method:


Public Sub Main()
‘ Create and Register a TCP server channel
‘ that listens on port 1234
Dim channel As TcpServerChannel = _
New TcpServerChannel(1234)
ChannelServices.RegisterChannel(channel)

‘ Register the service that publishes


‘ DbConnect for remote access in SingleCall mode
RemotingConfiguration. _
RegisterWellKnownServiceType( _
GetType(StepByStep3_1.DbConnect), “DbConnect”, _
WellKnownObjectMode.Singleton)
06 0789728206 CH03 2/27/03 9:54 AM Page 205

Chapter 3 .NET REMOTING 205

Console.WriteLine(“Started server in the “ & _


“Singleton mode”)
Console.WriteLine(“Press <ENTER> to terminate “ & _
“server...”)
Console.ReadLine()
End Sub

6. Right-click on the StepByStep3-4 project in the Solution


Explorer and select Properties. Change the Startup object
to DbConnectSingletonServer.
7. Build the project. This step creates a remoting server capa-
ble of registering the StepByStep3_1.DbConnect class for
remote invocation using the Singleton activation mode.
8. Set the StepByStep3-4, the remoting server, as the startup
project. Select Debug, Start Without Debugging to run
the project. You should see a command window displaying
a message that the server is started in the Singleton mode.
9. Now, set StepByStep3-3, the remoting client, as the startup
project. Select Debug, Start Without Debugging to run
the project. Enter a query in the text box and click the
button. The code invokes a method on the remote object,
which is created when the form loads. The code binds the
results from the remote method to the DataGrid control.

Although there has been no change in the output for the client, if
you note the messages generated by server (see Figure 3.9), you’ll see
that just one instance of the connection is created and is shared by
all the clients that connect to this server.

FIGURE 3.9
The Singleton remoting server uses the same
instance of remote object with subsequent
requests.
06 0789728206 CH03 2/27/03 9:54 AM Page 206

206 Par t I EXAM PREPARATION

GUIDED PRACTICE EXERCISE 3.1


The objective of this exercise is to create a remoting server that
exposes the DbConnect class of StepByStep3-1 as a Singleton SAO.
However, the server and client should communicate via the HTTP
channels and SOAP formatter. Other than this, the server and the
client programs are similar to those created in Step by Step 3.4 and
Step by Step 3.3, respectively.
How would you establish communication between a remoting server
and client using HTTP and SOAP?
This exercise helps you practice creating a remoting server and
clients using the HTTP channels and SOAP formatter.
You should try working through this problem on your own first. If
you get stuck, or if you’d like to see one possible solution, follow
these steps:
1. Add a new Visual Basic .NET Console application named
GuidedPracticeExercise3-1_Server to the solution.

2. Add references to the .NET assembly


System.Runtime.Remoting and the project StepByStep3-1 (the
remotable class assembly).
3. In the Solution Explorer, rename the default Module1.vb to
DbConnectSingletonServer.vb. Open the file and change the
name of the class to DbConnectSingletonServer in the class
declaration.
4. Add the following directives:
Imports StepByStep3_1
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Http

5. Add the following code in the Main() method:


Sub Main()
‘ Create and Register a HTTP server channel
‘ that listens on port 1234
Dim channel As HttpServerChannel = _
New HttpServerChannel(1234)
ChannelServices.RegisterChannel(channel)
06 0789728206 CH03 2/27/03 9:54 AM Page 207

Chapter 3 .NET REMOTING 207

‘ Register the service that publishes


‘ DbConnect for remote access in Singleton mode
RemotingConfiguration. _
RegisterWellKnownServiceType( _
GetType(StepByStep3_1.DbConnect), “DbConnect”, _
WellKnownObjectMode.Singleton)
Console.WriteLine(“Started server in the “ & _
“Singleton mode”)
Console.WriteLine(“Press <ENTER> to terminate “ & _
“server...”)
Console.ReadLine()
End Sub

6. Right-click on the GuidedPracticeExercise3-1 Server project in


the Solution Explorer and select Properties. Change the start-
up object to DbConnectSingletonServer.
7. Build the project. This step creates a remoting server that is
capable of registering the StepByStep3_1.DbConnect class (the
remotable object) for remote invocation using the Singleton
activation mode via the HTTP channel.
8. Add a new Visual Basic .NET Windows Application named
GuidedPracticeExercise3-1_Client to the solution.

9. Add references to the .NET assembly


System.Runtime.Remoting and the project StepByStep3-1 (the
remotable class assembly).
10. In the Solution Explorer, delete the default Form1.vb. Add a
new form named DbConnectClient.vb. Set the new form as
the startup object for the project.
11. Add the following directives to the form’s module:
Imports StepByStep3_1
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Http

12. Place two GroupBox controls, a TextBox control (txtQuery), a


Button control (btnExecute) and a DataGrid control (dgResults)
on the form. Set the Multiline property of txtQuery to True.
Refer to Figure 3.5 for the design of the form.
13. Add the following code to the form directly after the designer-
generated code:
‘ Declare a Remote object
Dim dbc As DbConnect

continues
06 0789728206 CH03 2/27/03 9:54 AM Page 208

208 Par t I EXAM PREPARATION

continued
14. Double-click the form and add the following code in the Load
event handler:
Private Sub DbConnectClient_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
‘ Register a TCP client channel
Dim channel As HttpClientChannel = _
New HttpClientChannel()
ChannelServices.RegisterChannel(channel)

‘ Register the remote class as a valid


‘ type in the client’s application domain
RemotingConfiguration. _
RegisterWellKnownClientType( _
GetType(DbConnect), _
“http://localhost:1234/DbConnect”)

‘ Instantiate the remote class


dbc = New DbConnect()
End Sub

15. Double-click the Button control and add the following code in
the Click event handler:
Private Sub btnExecute_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExecute.Click
Try
‘ Invoke a method on the remote object
Me.dgResults.DataSource = _
dbc.ExecuteQuery(Me.txtQuery.Text)
dgResults.DataMember = “Results”
Catch ex As Exception
MessageBox.Show(ex.Message, _
“Query Execution Error”)
End Try
End Sub

16. Build the solution. Set the GuidedPracticeExercise3-1_Server,


the remoting server, as the startup project. Select Debug, Start
Without Debugging to run the project. You should see a com-
mand window displaying a message that the server is started in
the Singleton mode. The remoting server is now ready to
receive SOAP messages via HTTP.
17. Now, set GuidedPracticeExercise3-1_Client, the remoting
client, as the startup project. Select Debug, Start Without
Debugging to run the project. Enter a query in the text box and
click the button. The code invokes a method on the remote
object and sends the messages in SOAP format via HTTP.
06 0789728206 CH03 2/27/03 9:54 AM Page 209

Chapter 3 .NET REMOTING 209

If you have difficulty following this exercise, review the sections


“Channels,” “Formatters,” “Creating a Remotable class,” and
“Creating a Server-Activated Object” earlier in this chapter. Make
sure that you also perform Step By Step 3.1 through Step By Step
3.4. After doing that review, try this exercise again.

Creating a Client-Activated Object


When exposing a remotable class as a CAO, no changes are required
to be made on the remotable class. Instead, only the server and
client differ on how the remotable class is registered with the remot-
ing system.
In this section, I’ll show you how to register a remotable class as a
CAO and how to instantiate and invoke a CAO from a remoting
client.

Registering a Remotable Class As a Client-


Activated Object
You’ll have to take the following steps to register a remotable class as
a CAO on the server:
1. Create a server channel that listens on a particular port to the
incoming object activation requests from other application
domains. The following examples show how to create a TCP
server channel and an HTTP server channel:
‘ Register a TCP server channel on port 1234
Dim channel As TcpServerChannel = _
new TcpServerChannel(1234)

‘ Register a HTTP server channel on port 1234


Dim channel As HttpServerChannel = _
new HttpServerChannel(1234)

2. Register the channel with the remoting framework. This regis-


tration is performed through the RegisterChannel() method
of the ChannelServices class:
‘ Register the channel with remoting framework
ChannelServices.RegisterChannel(channel);
06 0789728206 CH03 2/27/03 9:54 AM Page 210

210 Par t I EXAM PREPARATION

3. Register the remotable class with the remoting framework. For


a client-activated object, this registration is performed using
the RegisterActivatedServiceType() method of the
RemotingConfiguration class as shown here:

‘ Register a remote object as CAO


‘ with the remoting framework
RemotingConfiguration.RegisterActivatedServiceType( _
GetType(DbConnect), “DbConnect”))

Here, the first parameter is the type of the remotable class. The
second parameter specifies the URI through which the server
publishes the location of the remote object.

Step By Step 3.5 shows how to expose the now-familiar DbConnect


class from Step By Step 3.1 as a CAO.

STEP BY STEP
3.5 Registering a Remotable Class As a Client-
Activated Object
1. Add a new Visual Basic .NET Console application named
StepByStep3-5 to the solution.

2. Add references to the .NET assembly


System.Runtime.Remoting and the project StepByStep3-1
(the remotable class assembly).
3. In the Solution Explorer, rename the default Module1.vb
to DbConnectCAOServer.vb. Open the file and change the
name of the module to DbConnectCAOServer in the module
declaration.
4. Add the following directives:
Imports StepByStep3_1
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp

5. Add the following code in the Main() method:


Sub Main()
‘ Create and Register a TCP Channel on port 1234
Dim channel As TcpServerChannel = _
New TcpServerChannel(1234)
ChannelServices.RegisterChannel(channel)
06 0789728206 CH03 2/27/03 9:54 AM Page 211

Chapter 3 .NET REMOTING 211

‘ Register the client activated object


RemotingConfiguration. _
RegisterActivatedServiceType( _
GetType(DbConnect))
Console.WriteLine( _
“Started server in the Client Activation mode”)
Console.WriteLine( _
“Press <ENTER> to terminate server...”)
Console.ReadLine()
End Sub

6. Right-click on the StepByStep3-5 project in the Solution


Explorer and select Properties. Change the Startup object
to DbConnectCAOServer.
7. Build the project. This step creates a remoting server that
is capable of registering the StepByStep3_1.DbConnect (the
remotable object) class for remote invocation using the
client activation mode.

Instantiating and Invoking a Client-


Activated Object
To instantiate and invoke a client-activated object, the remoting
client needs to take the following steps:
1. Create and register a client channel used by the remoting
framework to send messages to the remoting server. The type
of channel used by the client should be compatible with the
channel used by the server. The following examples show how
to create a TCP client channel and an HTTP client channel:
‘ Create and register a TCP client channel
Dim channel As TcpClientChannel = _
New TcpClientChannel()
ChannelServices.RegisterChannel(channel)

‘ Create and register a HTTP client channel


Dim channel As HttpClientChannel = _
New HttpClientChannel()
ChannelServices.RegisterChannel(channel)

2. Register the remotable class as a valid type in the client’s appli-


cation domain. This registration is performed using the
RegisterActivatedClientType() method of the
RemotingConfiguration class as shown here:

‘ Register DbConnect as a type on client,


‘ which can be activated on the server
06 0789728206 CH03 2/27/03 9:54 AM Page 212

212 Par t I EXAM PREPARATION

RemotingConfiguration.RegisterActivatedClientType( _
Instantiating a Client-Activated GetType(DbConnect), “tcp://localhost:1234”)
TIP

Object You can instantiate a CAO


Here, the first parameter is the type of the remotable class. The
at the client side using any of its
second parameter specifies the uniform resource identifier
EXAM

available constructors.
(URI) through which the server publishes the location of the
remote object.
3. Instantiate the CAO on the server using the desired
constructor.
‘ Instantiate the remote object
Dim dbc As DbConnect = new DbConnect(“Pubs”)

I’ll demonstrate the preceding steps in Step By Step 3.6. This exam-
ple is similar to the client program created in Step By Step 3.3, but
this time the client allows users to choose between the databases on
the server.

STEP BY STEP
3.6 Instantiating and Invoking a Client-Activated
Object
1. Add a new Visual Basic .NET Windows Application
named StepByStep3-6 to the solution.
2. Add references to the .NET assembly
System.Runtime.Remoting and the project StepByStep3-1
(the remotable class assembly).
3. In the Solution Explorer, delete the default Form1.vb. Add
a new form named DbConnectClient.vb. Set the new form
as the startup object for the project.
4. Add the following directives:
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports StepByStep3_1

5. Place three GroupBox controls (grpDatabases, grpQuery and


grpResults), a ComboBox control (cboDatabases), a TextBox
control (txtQuery), two Button controls (btnSelect and
btnExecute), and a DataGrid control (dgResults) on the
form. Refer to Figure 3.10 for the design of this form.
06 0789728206 CH03 2/27/03 9:54 AM Page 213

Chapter 3 .NET REMOTING 213

6. Select the Items property of the cboDatabases control in


the Properties window and click on the (…) button. This
opens the String Collection Editor dialog box. Enter the
following names of databases in the editor:
Northwind
Pubs

Click OK to add the databases to the Items collection of


the cboDatabases control.
7. Add the following code just after the Windows form
designer generated code:
‘ Declare a Remote object
Dim dbc As DbConnect

8. Double-click the form and add the following code in the


Load event handler:

Private Sub DbConnectClient_Load( _


ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
cboDatabases.SelectedIndex = 0
grpQuery.Enabled = False
End Sub

9. Double-click the btnSelect control and add the following


code in the Click event handler:
Private Sub btnSelect_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSelect.Click
‘ Disable the Databases group box and
‘ Enable the Query group box
grpDatabases.Enabled = False
grpQuery.Enabled = True

‘ Register a TCP client channel


Dim channel As TcpClientChannel _
= New TcpClientChannel()
ChannelServices.RegisterChannel(channel)

‘ Register the remote class as a valid


‘ type in the client’s application domain
‘ by passing the Remote class and its URL
RemotingConfiguration. _
RegisterActivatedClientType( _
GetType(DbConnect), “tcp://localhost:1234”)

‘ Instantiate the remote class


dbc = New DbConnect( _
cboDatabases.SelectedItem.ToString())
End Sub

continues
06 0789728206 CH03 2/27/03 9:54 AM Page 214

214 Par t I EXAM PREPARATION

continued
10. Double-click the btnExecute control and add the follow-
ing code in the Click event handler:
Private Sub btnExecute_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExecute.Click
Try
‘ Invoke a method on the remote object
Me.dgResults.DataSource = _
dbc.ExecuteQuery(Me.txtQuery.Text)
dgResults.DataMember = “Results”
Catch ex As Exception
MessageBox.Show(ex.Message, _
“Query Execution Error”)
End Try
End Sub

11. Build the project. You now have a remoting client ready
to use.
12. Set StepByStep3-5, the CAO remoting server, as the start-
up project. Select Debug, Start Without Debugging to
run the project. You should see a command window dis-
playing a message that the server is started in the Client
Activation mode.
13. Now, set StepByStep3-6, the remoting client, as the start-
up project. Select Debug, Start Without Debugging to
run the project. Select a database from the combo box and
click the Select button. An instance of the remotable
object, DbConnect, is created with the selected database.
Now, enter a query in the text box and click the button.
The code invokes a method on the remote object. The
code binds the results from the remote method to the
DataGrid control, as shown in Figure 3.10.

FIGURE 3.10
A CAO client allows database selection by tak-
ing advantage of the capability to call various
constructors.
06 0789728206 CH03 2/27/03 9:54 AM Page 215

Chapter 3 .NET REMOTING 215

14. Now, again select Debug, Start Without Debugging to


run one more instance of the remoting client. Select a dif-
ferent database from the combo box and click the Select
button. An instance of the remotable object, DbConnect, is
created with the selected database. Now, enter a query in
the text box and click the button. You should see that the
second instance of the client fetches the data from the
selected database. Now switch to the Server command
window. You see that the remote object is instantiated
twice with different databases, as shown in Figure 3.11.
This shows that the client activation creates an instance of
remotable object for each client.

FIGURE 3.11
The client-activated remoting server creates a
new instance of the remote object for each
client.

R E V I E W B R E A K
. To create a remotable class, inherit the remotable class from
the MarshalByRefObject class.
. A remotable class is usually connected with the remoting
framework through a separate server program. The server pro-
gram listens to the client request on a specified channel and
instantiates the remote object or invokes calls on it as required.
. You should create a server channel that listens on a given port
number and register the channel with the remoting framework
before you register the remotable class.
continues
06 0789728206 CH03 2/27/03 9:54 AM Page 216

216 Par t I EXAM PREPARATION

continued

. The type of channel registered by the client must be compati-


ble with the type of channel used by the server for receiving
messages.
. You do not specify a port number when you register the client
channel. The port number is instead specified at the time of
registering the remote class in the client’s domain.
. To register SAO objects on the server side, you call the
RemotingConfiguration.RegisterWellKnownServiceType()
method; and to register SAO objects on the client side, you
call the RemotingConfiguration.
RegisterWellKnownClientType() method.

. To register CAO objects on the server side, you call the


RemotingConfiguration.RegisterActivatedServiceType()
method; and to register CAO objects on the client side, you
call the RemotingConfiguration.
RegisterActivatedClientType() method.

. You can only instantiate SAO objects at the client side using
their default constructors, whereas you can instantiate CAO
using any of the object’s constructors.

Using Configuration Files to Configure


the Remoting Framework
In all the examples so far, I have written code to register the channel
and remote object with the remoting framework. This approach to
specifying settings is also known as programmatic configuration.
Although this approach works fine, there is a drawback—every time
you decide to make any change in how the channel or the remote
objects are registered, you’ll have to recompile the code to see the
effect of the changes.
Alternatively, you can store the remoting settings in an XML-based
configuration file instead of in the code file. Any changes made to
the configuration file can be automatically picked up by the pro-
gram when it executes the next time. You need not recompile the
sources. This approach of specifying configuration settings is also
known as declarative configuration.
06 0789728206 CH03 2/27/03 9:54 AM Page 217

Chapter 3 .NET REMOTING 217

Declarative configuration can be specified at two levels:

NOTE
Use Naming Convention for the
. Machine-Level—The configuration settings at the machine- Application-level Configuration Files
level can be specified through the machine.config file. The Although it’s not strictly required, you
machine.config file is present in the CONFIG subdirectory of the should prefer using the .NET
.NET Framework installation (typically, Microsoft.NET\ Framework naming convention for
Framework\v1.0.3705\CONFIG in the Windows directory of naming the application-level configura-
your computer, if you’re running version 1.0 of the .NET tion file. This ensures that configura-
Framework). Any settings specified in this file apply to all the tion settings for an application,
.NET applications running on the machine. whether remoting or security related,
are all at one place.
. Application-Level—The configuration settings for a specific
application can be specified through the application configura-
tion file. In a Windows application, the name of the
application-level configuration file includes the full application Application-Level Configuration File

TIP
name and the extension, with .config appended to that exten- Takes Precedence over Machine-
sion. For example, the configuration file name for Level Configuration When you

EXAM
StepByStep3-1.exe is StepByStep3-1.exe.config. In an specify both application-level and
ASP.NET application, the name of the configuration file is machine-level configurations for an
web.config. application, the application-level
configuration takes priority over the
The general format of a configuration file is a follows: machine-level configuration.

<configuration>
<system.runtime.remoting>
<application>
<lifetime>
<!-- Use this section to specify the -->
<!-- lifetime information for all -->
<!-- the objects in the application. -->
</lifetime>
<service>
<!-- Use this section to specify how a remote-->
<!-- object is exposed by the remoting server-->
<!-- Use the <wellknown> tag to configure a -->
<!-- a SAO while use the <activated> tag -->
<!-- to configure a CAO -->
<wellknown />
<activated />
</service>
<client>
<!-- Use this section to specify how a remote-->
<!-- object is consumed by the client -->
<!-- Use the <wellknown> tag to configure a -->
<!-- a call to SAO while use the <activated> -->
<!-- tag to configure a call to CAO -->
<wellknown />
<activated />
</client>
06 0789728206 CH03 2/27/03 9:54 AM Page 218

218 Par t I EXAM PREPARATION

<channels>
Configuration Files Configuration <!-- Use this section to configure the -->
TIP

<!-- channels that the application uses -->


files are case sensitive.
<!-- to communicate with the remote objects -->
</channels>
EXAM

</application>
</system.runtime.remoting>
</configuration>

Server-Side Configuration
In Step By Step 3.7, I’ll create a remoting server with Singleton acti-
vation mode similar to the one created in Step By Step 3.4.
However, you’ll note that the code itself is reduced because most of
the configuration-related code is now moved to a separate configura-
tion file.

STEP BY STEP
3.7 Registering a Server-Activated Object in the
Singleton Activation Mode Using Configuration
Files
1. Add a new Visual Basic .NET Console application named
StepByStep3-7 to the solution.

2. Add references to the .NET assembly


System.Runtime.Remoting and the project StepByStep3-1
(the remotable class assembly).
3. In the Solution Explorer, right-click project StepByStep3-7
and select Add, Add New Item from the context menu.
Add an Item named StepByStep3-7.exe.config based on
the XML File template.
4. Open the StepByStep3-7.exe.config file and modify it to
contain the following code:
<configuration>
<system.runtime.remoting>
<application>
<service>
<!-- Set the activation mode,
remotable object and its URL -->
<wellknown mode=”Singleton”
type=
06 0789728206 CH03 2/27/03 9:54 AM Page 219

Chapter 3 .NET REMOTING 219

“StepByStep3_1.DbConnect, StepByStep3-1”
objectUri=”DbConnect” />
</service>
<channels>
<!-- Set the channel and port -->
<channel ref=”tcp server”
port=”1234” />
</channels>
</application>
</system.runtime.remoting>
</configuration>

5. In the Solution Explorer, select the project and click the


Show All Files button in the toolbar. Move the
StepByStep3-7.exe.config file from the project folder to
the bin folder under the project, where the StepByStep3-
7.exe file will be created when the project is compiled.

6. In the Solution Explorer, rename the default Module1.vb


to DbConnectSingletonServer.vb. Open the file and
change the name of the module to
DbConnectSingletonServer in the module declaration. Set
the renamed module as the startup object for the project.
7. Add the following directive:
Imports System.Runtime.Remoting

8. Add the following code in the Main() method:


Sub Main()
‘ Load remoting configuration
RemotingConfiguration.Configure( _
“StepByStep3-7.exe.config”)
Console.WriteLine(“Started server in the “ & _
“Singleton mode”)
Console.WriteLine(“Press <ENTER> to terminate “ & _
“server...”)
Console.ReadLine()
End Sub

9. Build the project. This step creates a remoting server capa-


ble of registering the StepByStep3-1.DbConnect class for
remote invocation using the Singleton activation mode via
its settings in the configuration file.
10. Set StepByStep3-7, the remoting server, as the startup pro-
ject. Select Debug, Start Without Debugging to run the
project. You should see a command window displaying a
message that the server is started in the Singleton mode by
configuring from its settings in the configuration file.
continues
06 0789728206 CH03 2/27/03 9:54 AM Page 220

220 Par t I EXAM PREPARATION

continued
11. Now, set StepByStep3-3, the remoting client, as the start-
up project. Select Debug, Start Without Debugging to
run the project. Enter a query in the text box and click
the button. The code invokes a method on the remote
object, which is created when the form loads. The code
binds the results from the remote method to the DataGrid
control.

The most important thing to note in Step By Step 3.7 is the way I
have written the <service> and the <channels> elements in the con-
figuration file.
The <service> element is written as
<service>
<!-- Set the activation mode,
remotable object and its URL -->
<wellknown mode=”Singleton”
type=
“StepByStep3_1.DbConnect, StepByStep3-1”
objectUri=”DbConnect” />
</service>

For an SAO, you need to use the <wellknown> element in which you
specify the activation mode, type, and the object URI. The type
attribute is specified as a pair of qualified class names
(StepByStep3_1.DbConnect) and the name of the assembly
(StepByStep3-1). The objectUri attribute specifies the endpoint of
the URI where the client program will attempt to connect.
The <channels> element can be used to specify the channels used by
the server to expose the remotable class. The ref attribute specifies
the ID of the channel you want to use. The value ref=”tcp server”
specifies that the channel is a TCP server channel. If I instead write
ref=”tcp”, the channel becomes a receiver-sender channel.

<channels>
<!-- Set the channel and port -->
<channel ref=”tcp server” port=”1234” />
</channels>

With the use of configuration files, the remoting code inside the
Main() method of the server is now just one statement:

‘ Load remoting configuration


RemotingConfiguration.Configure( _
“StepByStep3-7.exe.config”)
06 0789728206 CH03 2/27/03 9:54 AM Page 221

Chapter 3 .NET REMOTING 221

The Configure() method of the RemotingConfiguration class loads


the configuration file into memory, parses its contents to locate the
<system.runtime.remoting> section, and—based on the settings—
calls the relevant methods to register the channels and the remoting
objects.

Client-Side Configuration
The configuration of a remoting client is quite similar to that of a
remoting server. However, you’ll configure the <client> element of
the configuration file instead of the <service> element.
Step By Step 3.8 demonstrates how to use client-side configuration
files.

STEP BY STEP
3.8 Instantiating and Invoking a Server-Activated
Object
1. Add a new Visual Basic .NET Windows Application
named StepByStep3-8 to the solution.
2. Add references to the .NET assembly
System.Runtime.Remoting and the project StepByStep3-1
(the remotable class assembly).
3. In the Solution Explorer, right-click the project
StepByStep3-8 and select Add, Add New Item from the
context menu. Add an Item named
StepByStep3-8.exe.config based on the XML File
template.
4. Open the StepByStep3-8.exe.config file and modify it to
contain the following code:
<configuration>
<system.runtime.remoting>
<application>
<client>
<!-- Set the remotable object
and its URL -->
<wellknown
type=

continues
06 0789728206 CH03 2/27/03 9:54 AM Page 222

222 Par t I EXAM PREPARATION

continued
“StepByStep3_1.DbConnect, StepByStep3-1”
url=”tcp://localhost:1234/DbConnect”
/>
</client>
</application>
</system.runtime.remoting>
</configuration>

5. In the Solution Explorer, select the project and click the


Show All Files button in the toolbar. Move the
StepByStep3-8.exe.config file from the project folder to
the bin folder under the project, where the StepByStep3-
8.exe file will be created when the project is compiled.

6. In the Solution Explorer, delete the default Form1.vb. Add


a new form named DbConnectClient.vb. Set the new form
as the startup object for the project.
7. Place two GroupBox controls (grpQuery and grpResults), a
TextBox control (txtQuery), a Button control (btnExecute)
and a DataGrid control (dgResults) on the form. Refer to
Figure 3.5 for the design of this form.
8. Add the following directives:
Imports System.Runtime.Remoting
Imports StepByStep3_1

9. Add the following code just after the Windows form


designer generated code:
‘ Declare a Remote object
Dim dbc As DbConnect

10. Double-click the form and add the following code in the
Load event handler:

Private Sub DbConnectClient_Load( _


ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
‘ Load remoting configuration
RemotingConfiguration.Configure( _
“StepByStep3-8.exe.config”)

‘ Instantiate the remote class


dbc = New DbConnect()
End Sub
06 0789728206 CH03 2/27/03 9:54 AM Page 223

Chapter 3 .NET REMOTING 223

11. Double-click the Button control and add the following


code in the Click event handler:
Private Sub btnExecute_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExecute.Click
Try
‘ Invoke a method on the remote object
Me.dgResults.DataSource = _
dbc.ExecuteQuery(Me.txtQuery.Text)
dgResults.DataMember = “Results”
Catch ex As Exception
MessageBox.Show(ex.Message, _
“Query Execution Error”)
End Try
End Sub

12. Build the project. Set StepByStep3-7, the remoting server,


as the startup project. Select Debug, Start Without
Debugging to run the project. You should see a command
window displaying a message that the server is started in
the Singleton mode by configuring from its settings in the
configuration file.
13. Now, set StepByStep3-8, the remoting client as the startup
project. Select Debug, Start Without Debugging to run
the project. Enter a query in the text box and click the
button. The code invokes a method on the remote object,
which is created from the settings stored in the configura-
tion file. The code binds the results from the remote
method to the DataGrid control.

The most important thing to note from this example is the use of
the <client> element in the configuration file. The mode attribute is
not part of the <wellknown> element of the client, and the objectUri
attribute is replaced with the url attribute that specifies the address
to connect to.
<client>
<!-- Set the remotable object
and its URL -->
<wellknown
type=
“StepByStep3_1.DbConnect, StepByStep3-1”
url=”tcp://localhost:1234/DbConnect”
/>
</client>
06 0789728206 CH03 2/27/03 9:54 AM Page 224

224 Par t I EXAM PREPARATION

You can note that unlike the server configuration file, the <channel>
element is not required for the client as it determines the protocol
and the port number using the specified URL.

GUIDED PRACTICE EXERCISE 3.2


In this exercise, you are required to expose the DbConnect class from
Step By Step 3.1 as a CAO through a remoting server using an
HTTP channel. You also want to invoke methods on this client-
activated object by creating a form similar to the one created in Step
By Step 3.6.
However, you should be able to change various parameters, such as
the channel protocol, port number, URL, name, and type of the
remote object, without the need to recompile the server.
How would you design such a remoting client and server?
This exercise helps you practice creating configuration files for both
server and client that remote a CAO. You should try working
through this problem on your own first. If you get stuck, or if you’d
like to see one possible solution, follow these steps:
1. Add a new Visual Basic .NET Console application named
GuidedPracticeExercise3-2_Server to the solution.

2. Add references to the .NET assembly


System.Runtime.Remoting and the project StepByStep3_1 (the
remotable class assembly).
3. In the Solution Explorer, right-click the project
GuidedPracticeExercise3-2_Server and select Add, Add New
Item from the context menu. Add an Item named
GuidedPracticeExercise3-2_Server.exe.config based on the
XML File template.
4. Open the GuidedPracticeExercise3-2_Server.exe.config file
and modify it to contain the following code:
<configuration>
<system.runtime.remoting>
<application>
<service>
<!-- Set the remotable object -->
<activated
type=
06 0789728206 CH03 2/27/03 9:54 AM Page 225

Chapter 3 .NET REMOTING 225

“StepByStep3_1.DbConnect, StepByStep3-1”
/>
</service>
<channels>
<channel ref=”http” port=”1234” />
</channels>
</application>
</system.runtime.remoting>
</configuration>

5. In the Solution Explorer, select the project and click the Show
All Files button in the toolbar. Move the
GuidedPracticeExercise3-2_Server.exe.config file from the
project folder to the bin folder under the project, where the
GuidedPracticeExercise3-2_Server.exe file will be created
when the project is compiled.
6. In the Solution Explorer, rename the default Module1.vb to
DbConnectCAOServer.vb. Open the file and change the name of
the module to DbConnectCAOServer in the module declaration.
Set the module as the Startup object for the project.
7. Add the following directive:
Imports System.Runtime.Remoting

8. Add the following code in the Main() method:


Sub Main()
‘ Load remoting configuration
RemotingConfiguration.Configure( _
“GuidedPracticeExercise3-2_Server.exe.config”)

Console.WriteLine( _
“Started server in the Client Activation mode”)
Console.WriteLine( _
“Press <ENTER> to terminate server...”)
Console.ReadLine()
End Sub

9. Build the project. This step creates a remoting server capable


of registering the StepByStep3_1.DbConnect class for remote
invocation using the client activation mode via its settings in
the configuration file.
10. Add a new Visual Basic .NET Windows Application named
GuidedPracticeExercise3-2_Client to the solution.

11. Add references to the .NET assembly


System.Runtime.Remoting and the project StepByStep3-1 (the
remotable class assembly).
continues
06 0789728206 CH03 2/27/03 9:54 AM Page 226

226 Par t I EXAM PREPARATION

continued
12. In the Solution Explorer, right-click the project
GuidedPracticeExercise3-2_Client and select Add, Add New
Item from the context menu. Add an Item named
GuidedPracticeExercise3-2_Client.exe.config based on the
XML File template.
13. Open the GuidedPracticeExercise3-2_Client.exe.config file
and modify it to contain the following code:
<configuration>
<system.runtime.remoting>
<application>
<!-- Set the url for the
client activation -->
<client url=”http://localhost:1234”>
<!-- Set the remote object
type and its assembly -->
<activated
type=
“StepByStep3_1.DbConnect, StepByStep3-1”
/>
</client>
</application>
</system.runtime.remoting>
</configuration>

14. In the Solution Explorer, select the project and click the Show
All Files button in the toolbar. Move the
GuidedPracticeExercise3-2_Client.exe.config file from the
project folder to the bin\Debug folder under the project,
where the GuidedPracticeExercise3-2_Client.exe file will be
created when the project is compiled.
15. In the Solution Explorer, remove the default Form1.vb. Add a
new form named DbConnectClient.vb. Set the new form as the
startup object for the project.
16. Place three GroupBox controls (grpDatabases, grpQuery and
grpResults), a ComboBox control (cboDatabases), a TextBox
control (txtQuery), two Button controls (btnSelect and
btnExecute), and a DataGrid control (dgResults) on the form.
Set the Multiline property of the txtQuery control to True.
Refer to Figure 3.10 for the design of this form.
06 0789728206 CH03 2/27/03 9:54 AM Page 227

Chapter 3 .NET REMOTING 227

17. Select the Items property of the cboDatabases control in the


Properties window and click on the (…) button. This opens
the String Collection Editor dialog box. Enter the following
names of databases in the editor:
Northwind
Pubs

Click OK to add the databases to the Items collection of the


cboDatabases control.

18. Add the following directives to the form’s module:


Imports System.Runtime.Remoting
Imports StepByStep3_1

19. Add the following code directly after the Windows Forms
designer generated code:
‘ Declare a Remote object
Dim dbc As DbConnect

20. Double-click the form and add the following code in the Load
event handler:
Private Sub DbConnectClient_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
cboDatabases.SelectedIndex = 0
grpQuery.Enabled = False
End Sub

21. Double-click the btnSelect control and add the following


code in the Click event handler:
Private Sub btnSelect_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSelect.Click
grpDatabases.Enabled = False
grpQuery.Enabled = True

‘ Load remoting configuration


RemotingConfiguration.Configure( _
“GuidedPracticeExercise3-2_Client.exe.config”)

‘ Instantiate the remote class


dbc = New DbConnect( _
cboDatabases.SelectedItem.ToString())
End Sub

continues
06 0789728206 CH03 2/27/03 9:54 AM Page 228

228 Par t I EXAM PREPARATION

continued

22. Double-click the btnExecute control and add the following


code in the Click event handler:
Private Sub btnExecute_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExecute.Click
Try
‘ Invoke a method on the remote object
Me.dgResults.DataSource = _
dbc.ExecuteQuery(Me.txtQuery.Text)
dgResults.DataMember = “Results”
Catch ex As Exception
MessageBox.Show(ex.Message, _
“Query Execution Error”)
End Try
End Sub

23. Build the project. Set the GuidedPracticeExercise3-2_Server,


the remoting server, as the startup project. Select Debug, Start
Without Debugging to run the project. You should see a com-
mand window displaying a message that the server is started in
the Client Activation mode by configuring from its settings in
the configuration file.
24. Now, set GuidedPracticeExercise3-2_Client, the remoting
client as the startup project. Select Debug, Start Without
Debugging to run the project. Select a database from the
combo box. Enter a query in the text box and click the but-
ton. The code invokes a method on the remote object, which
is created from the settings stored in the configuration file.
The code binds the results from the remote method to the
DataGrid control.

If you have difficulty following this exercise, review the sections


“Creating a Client-Activated Object” and “Using Configuration
Files to Configure the Remoting Framework” earlier in this chapter.
After doing that review, try this exercise again.
06 0789728206 CH03 2/27/03 9:54 AM Page 229

Chapter 3 .NET REMOTING 229

Using Interface Assemblies to Compile


Remoting Clients
So far, in all the examples, you had to copy the assembly containing
the remotable class to the client project (which happens automatical-
ly when you set a reference to the assembly) in order for the client to
work. However, this is not a desirable solution in many cases
because you might not want to share the implementation of the
remotable object with your customers or business partners who are
writing the client application.
An advisable solution in that case is to only share the interface of the
remotable class with the client application. An interface does not
contain any implementation; instead, it just contains the members
that are supplied by the class.
In the following sections, I’ll demonstrate how you can create inter-
faces that will allow you to create a remote SAO and CAO without
sharing the implementation. I’ll also introduce the Soapsuds tool,
which can help you in automatically creating the interfaces for a
remotable class.

Creating an Interface Assembly


In this section, I’ll create an assembly that implements an interface
named IDbConnect class. The interface defines a contract. All the
classes or structs that implement the interface must adhere to this
contract.

STEP BY STEP
3.9 Creating an Interface Assembly
1. Add a new Visual Basic .NET Class library named
StepByStep3-9 to the solution.

2. In the Solution Explorer, rename the default Class1.vbs


to IDbConnect.vb.
continues
06 0789728206 CH03 2/27/03 9:54 AM Page 230

230 Par t I EXAM PREPARATION

continued

3. Open the IDbConnect.vb and replace the code with the


following code:
Imports System
Imports System.Data

Public Interface IDbConnect


Function ExecuteQuery(ByVal strQuery As String) _
As DataSet
End Interface

4. Build the project. This step creates an assembly that con-


tains the definition of the IDbConnect interface.

Creating a Remotable Object That


Implements an Interface
Now that you created the interface IDbConnect, you’ll implement
this interface in a class named DbConnect. You must ensure that the
class DbConnect adheres to the contract defined by the IDbConnect
interface. This means that you must implement the method
ExecuteQuery() and that the data type of parameters and the return
value must match exactly with that defined in the interface.
Step By Step 3.10 creates the class DbConnect that adheres to the
IDbConnect interface.

STEP BY STEP
3.10 Creating a Remotable Object That Implements
an Interface Assembly
1. Add a new Visual Basic .NET Class library named
StepByStep3-10 to the solution.

2. Add references to the .NET assembly


System.Runtime.Remoting and the project StepByStep3-9
(the interface assembly).
3. In the Solution Explorer, rename the default Class1.vbs
to DbConnect.vb.
06 0789728206 CH03 2/27/03 9:54 AM Page 231

Chapter 3 .NET REMOTING 231

4. Open the DbConnect.vb and replace the code with the fol-
lowing code:
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports StepByStep3_9

‘ Marshal-by-Reference Remotable Object


Public Class DbConnect
Inherits MarshalByRefObject
‘ Implement the IDbConnect interface
Implements IDbConnect

Private sqlconn As SqlConnection

‘ Default constructor connects to the Northwind


‘ database
Public Sub New()
sqlconn = New SqlConnection( _
“data source=(local);” & _
“initial catalog=Northwind;” & _
“integrated security=SSPI”)
Console.WriteLine( _
“Created a new connection “ & _
“to the Northwind database”)
End Sub

‘ Parameterized constructor connects to the


‘ specified database
Public Sub New(ByVal DbName As String)
sqlconn = New SqlConnection( _
“data source=(local);” & _
“initial catalog=” & DbName & “;” & _
“integrated security=SSPI”)
Console.WriteLine( _
“Created a new connection “ & _
“to the “ & DbName & “ database”)
End Sub

Public Function ExecuteQuery( _


ByVal strQuery As String) As DataSet _
Implements IDbConnect.ExecuteQuery
Console.Write(“Starting to execute “ & _
“the query...”)
‘ Create a SqlCommand to represent the query
Dim sqlcmd As SqlCommand = _
sqlconn.CreateCommand()
sqlcmd.CommandType = CommandType.Text
sqlcmd.CommandText = strQuery

‘ Create a SqlDataAdapter object


‘ to talk to the database
Dim sqlda As SqlDataAdapter = _
New SqlDataAdapter()
sqlda.SelectCommand = sqlcmd

continues
06 0789728206 CH03 2/27/03 9:54 AM Page 232

232 Par t I EXAM PREPARATION

continued
‘ Create a DataSet to hold the results
Dim ds As DataSet = New DataSet()
Try
‘ Fill the DataSet
sqlda.Fill(ds, “Results”)
Catch ex As Exception
Console.WriteLine(ex.Message, _
“Error executing query”)
End Try
Console.WriteLine(“Done.”)
ExecuteQuery = ds
End Function
End CLass

5. Build the project. This step creates


StepByStep3_10.DbConnect, a remotable class that imple-
ments the IDbConnect interface.

This program is similar to the one created in Step By Step 3.4, with
the difference being that the DbConnect class is also implementing
the IDbConnect interface in addition to deriving from the
MarshalByRefObject class:

‘ Marshal-by-Reference Remotable Object


Public Class DbConnect
Inherits MarshalByRefObject
‘ Implement the IDbConnect interface
Implements IDbConnect

You can expose this remotable object to the clients via the remoting
framework using the techniques you already know. Step By Step
3.11 creates a remoting server that registers the DbConnect class creat-
ed in Step By Step 3.10 as an SAO in Singleton mode.

STEP BY STEP
3.11 Creating a Remoting Server to Register the
Remotable Object that Implements an Interface
Assembly
1. Add a new Visual Basic .NET Console application named
StepByStep3-11 to the solution.
06 0789728206 CH03 2/27/03 9:54 AM Page 233

Chapter 3 .NET REMOTING 233

2. Add references to the .NET assembly


System.Runtime.Remoting, the project StepByStep3-9 (the
interface assembly), and StepByStep3-10 (the remotable
class assembly).
3. In the Solution Explorer, rename the default Module1.vb
to DbConnectSingletonServer.vb. Open the file and
change the name of the module to
DbConnectSingletonServer in the module declaration. Set
the module to be the Startup object of the project.
4. Add the following directives:
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp

5. Add the following code in the Main() method:


Sub Main()
‘ Register a TCP server channel that
‘ listens on port 1234
Dim channel As TcpServerChannel = _
New TcpServerChannel(1234)
ChannelServices.RegisterChannel(channel)

‘ Register the service that publishes


‘ DbConnect for remote access in Singleton mode
RemotingConfiguration. _
RegisterWellKnownServiceType( _
GetType(StepByStep3_10.DbConnect), “DbConnect”, _
WellKnownObjectMode.Singleton)
Console.WriteLine(“Started server in the “ & _
“Singleton mode”)
Console.WriteLine(“Press <ENTER> to terminate “ & _
“server...”)
Console.ReadLine()
End Sub

6. Build the project. This step creates a remoting server capa-


ble of registering StepByStep3_10.DbConnect, the
remotable object that implements the
StepByStep3_9.IDbConnect interface for remote invocation
using the Singleton activation mode.
06 0789728206 CH03 2/27/03 9:54 AM Page 234

234 Par t I EXAM PREPARATION

Creating a Remoting Client That Uses an


Interface Instead of the Implementation
When the remotable class is implementing an interface, you can just
include the reference to the interface assembly instead of the imple-
mentation assembly at the client side. The client will then extract
the necessary type information and metadata for compiling and run-
ning the program from the interface. Step By Step 3.12 shows you
how to create such a client.

STEP BY STEP
3.12 Creating a Remoting Client to Invoke the
Remotable Object That Implements an Interface
Assembly
1. Add a new Visual Basic .NET Windows Application
named StepByStep3-12 to the solution.
2. Add references to the .NET assembly
System.Runtime.Remoting and the project StepByStep3-9
(the interface assembly).
3. In the Solution Explorer, delete the default Form1.vb. Add
a new form named DbConnectClient.vb, and set it as the
startup object for the project.
4. Add the following directives:
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports StepByStep3_9

5. Place two GroupBox controls (grpQuery and grpResults), a


TextBox control (txtQuery), a Button control (btnExecute),
and a DataGrid control (dgResults) on the form. Set the
Multiline property of txtQuery to True. Refer to Figure
3.5 for the design of this form.
6. Add the following code directly after the Windows Form
designer generated code:
‘ Declare a Remote object
Dim dbc As IDbConnect
06 0789728206 CH03 2/27/03 9:54 AM Page 235

Chapter 3 .NET REMOTING 235

7. Double-click the form and add the following code in the


Load event handler:

Private Sub DbConnectClient_Load( _


ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
‘ Register a TCP client channel
Dim channel As TcpClientChannel = _
New TcpClientChannel()
ChannelServices.RegisterChannel(channel)

‘ Instantiate the remote class


dbc = CType( _
Activator.GetObject(GetType(IDbConnect), _
“tcp://localhost:1234/DbConnect”), IDbConnect)
End Sub

8. Double-click the Button control and add the following


code in the Click event handler:
Private Sub btnExecute_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExecute.Click
Try
‘ Invoke a method on the remote object
Me.dgResults.DataSource = _
dbc.ExecuteQuery(Me.txtQuery.Text)
dgResults.DataMember = “Results”
Catch ex As Exception
MessageBox.Show(ex.Message, _
“Query Execution Error”)
End Try
End Sub

9. Build the project. Set the StepByStep3-11, the remoting


server, as the startup project. Select Debug, Start Without
Debugging to run the project. You should see a command
window displaying a message that the server is started in
the Singleton mode.
10. Now, set StepByStep3-12, the remoting client, as the start-
up project. Select Debug, Start Without Debugging to
run the project. Enter a query in the text box and click
the button. The code invokes a method on the remote
object, which is created when the form loads. The code
binds the results from the remote method to the DataGrid
control.
06 0789728206 CH03 2/27/03 9:54 AM Page 236

236 Par t I EXAM PREPARATION

In the preceding program, you add the reference to StepByStep3-


9.dll. This assembly contains the interface IDbConnect. However, an
interface variable cannot be instantiated, and, therefore, you cannot
create the instance of the remote object by using the new operator.
But there is an alternative way of creating instances of remote
objects, which is especially helpful in the case of interfaces. This is
by using the methods of Activator class as described here:
. The Activator.GetObject() Method—Calls the proxy to send
messages to the remote object. No messages are sent over the
network until a method is called on the proxy. This method is
useful for activating server-activated objects.
. The Activator.CreateInstance() Method—Creates an
instance of the specified type using the constructor that best
matches the specified parameters. This method is useful for
activating client-activated objects.

In Step By Step 3.12, I use the Activator.GetObject() method to


create a proxy for the server-activated object indicated by the type
IDbConnect and the specified server URL.

Using the Soapsuds Tool to Automatically


Generate an Interface Assembly
In the previous section, you learned how you can distribute interface
assemblies to the client instead of the implementation assembly.
Clients will still be able to instantiate the remote objects in this case.
However, in case of large numbers of clients, distribution of inter-
face files to each of them can become another big issue. However,
the .NET Framework SDK provides you the soapsuds tool
(soapsuds.exe) to overcome this issue.
Given the URL for a remotable object, the soapsuds tool can auto-
matically generate an interface assembly for the remote object. So all
the client needs to know is the URL of the remotable object, and
they can generate the interface assembly on their own by using the
soapsuds tool. A typical usage of the soapsuds tool is

soapsuds -NoWrappedProxy
➥ -url:http://MyServer.com/DbConnect?wsdl
➥ -outputAssemblyFile:DbConnectInterface.dll
06 0789728206 CH03 2/27/03 9:54 AM Page 237

Chapter 3 .NET REMOTING 237

In this command line, the url switch specifies the URL of the
remote object. You normally have to append ?wsdl to the URL to

NOTE
Wrapped Proxies Wrapped proxies
allow soapsuds to generate the metadata from the URL. The are useful when you need to quickly
outputAssemblyFile switch specifies the name of the file in which you test a Web service because with the
want the output assembly to be created. The NoWrappedProxy (or nowp) use of wrapped proxies, you need not
switch instructs the soapsuds tool to generate an unwrapped proxy. have to write code for channel config-
uration and remote object registration.
By default, the soapsuds tool generates wrapped proxies—proxies However, for more flexibility you
that store various connection details, such as the channel formatting should use unwrapped proxies.
and the URL within the proxy itself. Although this means that you
need not write the code for these things yourself, it’s not recom-
mended because it reduces the flexibility to change the configura-
Soapsuds Tool and Channels The

TIP
tion. If you want to avoid specifying these details in the code, a
soapsuds tool can only be used
better way is to use the configuration files.
with the HTTP channel. If you are

EXAM
Step By Step 3.13 creates a client that uses the soapsuds generated using the TCP channel, you still
unwrapped proxy to connect with the HTTP server created in the need to depend on manually creat-
Guided Practice Exercise 3.1. ing and distributing the interfaces
for remotable classes.

STEP BY STEP
3.13 Using the Soapsuds Tool to Automatically
Generate an Interface Assembly
1. Add a new Visual Basic .NET Windows Application to
your solution. Name it StepByStep3-13.
2. Set the GuidedPracticeExercise3-1_Server as the startup
project. Select Debug, Start Without Debugging to run
the project.
3. Select Start, Programs, Microsoft Visual Studio .NET,
Visual Studio .NET Tools, Visual Studio .NET
Command Prompt to launch a .NET command prompt.
4. Make sure that the GuidedPracticeExercise3-1_Server is
running. Navigate to the bin folder of the StepByStep3-13
project and run the following command to automatically
generate an interface assembly for the remotable object
DbConnect, registered by the remoting server,
GuidedPracticeExercise3-1_Server:

soapsuds -url:http://localhost:1234/DbConnect?wsdl
➥ -oa:DbConnectInterface.dll -nowp

continues
06 0789728206 CH03 2/27/03 9:54 AM Page 238

238 Par t I EXAM PREPARATION

continued
5. In Visual Studio .NET, add references to the .NET assem-
bly System.Runtime.Remoting and DbConnectInterface.dll
(the interface assembly auto generated by the soapsuds.exe
in step 4).
6. In the Solution Explorer, delete the default Form1.vb. Add
a new form named DbConnectClient.vb and set it as the
Startup object for the project.
7. Add the following directives:
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Http
Imports StepByStep3_1

8. Place two GroupBox controls (grpQuery and grpResults), a


TextBox control (txtQuery), a Button control (btnExecute),
and a DataGrid control (dgResults) on the form. Set the
Multiline property of txtQuery to True. Refer to Figure
3.5 for the design of this form.
9. Add the following code directly after the Windows Form
designer generated code:
‘ Declare a Remote object
Dim dbc As DbConnect

10. Double-click the form and add the following code in the
Load event handler:

Private Sub DbConnectClient_Load( _


ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
‘ This step is not required if you use
‘ a wrapped proxy
‘ Register a HTTP client channel
Dim channel As HttpClientChannel = _
New HttpClientChannel()
ChannelServices.RegisterChannel(channel)

‘ This step is not required if you use


‘ a wrapped proxy.
‘ Register the remote class as a valid
‘ type in the client’s application domain
RemotingConfiguration. _
RegisterWellKnownClientType( _
GetType(DbConnect), _
“http://localhost:1234/DbConnect”)

‘ Instantiate the remote class


dbc = New DbConnect()
End Sub
06 0789728206 CH03 2/27/03 9:54 AM Page 239

Chapter 3 .NET REMOTING 239

11. Double-click the Button control and add the following


code in the Click event handler:
Private Sub btnExecute_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExecute.Click
Try
‘ Invoke a method on the remote object
Me.dgResults.DataSource = _
dbc.ExecuteQuery(Me.txtQuery.Text)
dgResults.DataMember = “Results”
Catch ex As Exception
MessageBox.Show(ex.Message, _
“Query Execution Error”)
End Try
End Sub

12. Build the project. If the GuidedPracticeExercise3-


1_Server project is not running, set the
GuidedPracticeExercise3-1_Server as the startup project
and select Debug, Start Without Debugging to run the
project. You should see a command window displaying a
message that the server is started in the Singleton mode.
13. Now, set StepByStep3_13, the remoting client, as the start-
up project. Select Debug, Start Without Debugging to
run the project. Enter a query in the text box and click
the button. The code invokes a method on the remote
object, which is created when the form loads. The code
binds the results from the remote method to the DataGrid
control.

In Step By Step 3.13, you learned how to use the soapsuds tool to
automatically generate the interface class for the remotable object.
Soapsuds and Client-Activated
TIP

Also, the interface class generated by the soapsuds tool allows you to
Objects You cannot use soapsuds
directly use the name of remotable class. This means that you can
with client-activated objects
access the remotable class directly in the client as if you have a direct
EXAM

because the metadata generated


reference to the remotable class at the client side. For this reason, I by the soapsuds tool can only cre-
can use the RegisterWellKnownClientType() method of the ate remote objects using the
RemotingConfiguration class to register the remote class on the client default constructor.
side instead of using the Activator.GetObject() method.
06 0789728206 CH03 2/27/03 9:54 AM Page 240

240 Par t I EXAM PREPARATION

Creating an Interface Assembly That Works


with the Client-Activated Objects
Neither of the techniques for generating interface assemblies dis-
cussed so far (in the sections, “Creating an Interface Assembly” and
“Using the Soapsuds Tool to Automatically Generate an Interface
Assembly”) is useful for CAO.
The problem lies with the constructors. CAO has capabilities of
invoking even the non-default constructors of the remotable class.
However, an interface cannot contain the declaration for a construc-
tor as it does with a method or a property.
This common problem is generally solved using the following steps:
1. Create an interface and a remotable class as shown in the pre-
vious examples. For easy reference, I’ll call them IDbConnect
and DbConnect, respectively. IDbConnect contains definitions of
all the methods that the DbConnect class wants to be exposed
to the clients.
2. Create an interface that declares as many different methods as
there are constructors in the remotable class. Each of these
methods is responsible for creating an object in a way defined
by its corresponding constructor. This technique is also called
an abstract factory pattern. The name contains the word “fac-
tory” because it allows you to create objects in different ways.
Let’s call this interface IDbConnectFactory.
3. Create a class that derives from the MarshalByRefObject class
and implements the interface created in step 2
(IDbConnectFactory). Implement all methods to actually create
the remotable object (DbConnect) based on the given parame-
ters and return the created instance. Let’s call this class
DbConnectFactory.

4. Create a remoting server that registers the class created in


step 3 (DbConnectFactory) as the remotable class.
5. Create a client that connects to the server and create an
instance of the remotable class created in step 3
(DbConnectFactory).
6. Invoke the methods corresponding to a constructor on the
remotable object created in step 5. The return value of the
constructor will be a remotable object of type defined in step 1
(DbConnect).
06 0789728206 CH03 2/27/03 9:54 AM Page 241

Chapter 3 .NET REMOTING 241

7. You now have an object of a type that you originally wanted to


remote (DbConnect). You can use this object to invoke
methods.

In this section, I’ll show you how to implement the preceding tech-
nique to create a client-activated object that enables the creation of
objects using any of its available constructors.
I’ll use the same remotable class that I defined in Step By Step 3.10
(DbConnect) and its interface (IDbConnect) that I defined in Step By
Step 3.9. So I’ll directly start with step 2 of the preceding list by
creating an IDbConnectFactory interface in Step By Step 3.14.

STEP BY STEP
3.14 Creating an Assembly That Works As an
Abstract Factory for the IDbConnect Object
1. Add a new Visual Basic .NET Class library named
StepByStep3-14 to the solution.

2. Add a reference to the project StepByStep3-9 (the inter-


face assembly).
3. In the Solution Explorer, rename the default Class1.vb to
IDbConnectFactory.vb.

4. Open the IDbConnectFactory.vb and replace the code


with the following code:
Imports System
Imports System.Data
Imports StepByStep3_9

Public Interface IDbConnectFactory


Function CreateDbConnectInstance() As IDbConnect
Function CreateDbConnectInstance( _
ByVal dbName As String) As IDbConnect
End Interface

5. Build the project. The class library now contains an inter-


face to a class that can act as a factory of objects imple-
menting the IDbConnect interface.
06 0789728206 CH03 2/27/03 9:54 AM Page 242

242 Par t I EXAM PREPARATION

The next step is to create a class that implements the


IDbConnectFactory interface and then exposes that class as a
remotable class through the remoting framework. Step By Step 3.15
shows how to do so.

STEP BY STEP
3.15 Creating a Remoting Server That Exposes
DbConnectFactory As a Remotable Class
1. Add a new Visual Basic .NET Console application named
StepByStep3-15 to the solution.

2. Add references to the .NET assembly


System.Runtime.Remoting, the projects StepByStep3-9 (the
interface assembly containing IDbConnect), StepByStep3-
14 (the new interface assembly containing the
IDbConnectFactory), and StepByStep3-10 (the remotable
class assembly).
3. Add a new class to the project that will create another
remotable object that inherits from MarshalByRefObject
class and implements the IDbConnectFactory interface,
created in Step By Step 3.14. Name it
DbConnectFactory.vb. Open the DbConnectFactory.vb
class and replace the code with the following code:
Imports System
Imports StepByStep3_9
Imports StepByStep3_10
Imports StepByStep3_14

Public Class DbConnectFactory


Inherits MarshalByRefObject
Implements IDbConnectFactory

Public Function CreateDbConnectInstance() _


As IDbConnect Implements _
IDbConnectFactory.CreateDbConnectInstance
Return New DbConnect()
End Function

Public Function CreateDbConnectInstance( _


ByVal dbName As String) As IDbConnect _
Implements IDbConnectFactory. _
CreateDbConnectInstance
Return New DbConnect(dbName)
End Function
End Class
06 0789728206 CH03 2/27/03 9:54 AM Page 243

Chapter 3 .NET REMOTING 243

4. In the Solution Explorer, rename the default Module1.vb


as DbConnectFactoryServer.vb. Open the file and change
the name of the module to DbConnectFactoryServer in the
module declaration. Set this module to be the Startup
object of the project.
5. Add the following directives:
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp

6. Add the following code in the Main() method:


Sub Main()
‘ Register a TCP server channel that
‘ listens on port 1234
Dim channel As TcpServerChannel = _
New TcpServerChannel(1234)
ChannelServices.RegisterChannel(channel)

RemotingConfiguration. _
RegisterWellKnownServiceType( _
GetType(DbConnectFactory), _
“DbConnectFactory”, _
WellKnownObjectMode.Singleton)
Console.WriteLine(“Started server in the “ & _
“client activated mode”)
Console.WriteLine(“Press <ENTER> to terminate “ & _
“server...”)
Console.ReadLine()
End Sub

7. Build the project. This step creates a remoting server capa-


ble of registering StepByStep3_10.DbConnect, the
remotable object that implements the
StepByStep3_9.IDbConnect interface, for remote invoca-
tion using the Singleton activation mode.

Step By Step 3.15 exposes the DbConnectFactory as an SAO in


Singleton mode. Although the DbConnectFactory object is registered
as an SAO, the objects returned by various methods of
DbConnectFactory are CAO because they are created only on the
explicit request from the client.
Step By Step 3.16 is the final step for creating CAO using the
abstract factory pattern.
06 0789728206 CH03 2/27/03 9:54 AM Page 244

244 Par t I EXAM PREPARATION

STEP BY STEP
3.16 Instantiating and Invoking a Client-Activated
Object Using the Abstract Factory Pattern
1. Add a new Visual Basic .NET Windows Application
named StepByStep3-16 to the solution.
2. Add references to the .NET assembly
System.Runtime.Remoting and the projects StepByStep3-9
(the IDbConnect interface assembly) and StepByStep3-14
(the IDbConnectFactory interface assembly).
3. In the Solution Explorer, delete the default Form1.vb. Add
a new form and name it DbConnectClient.vb. Set the new
form as the startup object for the project.
4. Add the following directives:
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports StepByStep3_9 ‘ contains IDbConnect
Imports StepByStep3_14 ‘ contains IDbConnectFactory

5. Place three GroupBox controls (grpDatabases, grpQuery and


grpResults), a ComboBox control (cboDatabases), a TextBox
control (txtQuery), two Button controls (btnSelect and
btnExecute), and a DataGrid control (dgResults) on the
form. Set the Multiline property of txtQuery to True.
Refer to Figure 3.10 for the design of this form.
6. Select the Items property of the cboDatabases control in
the Properties window and click on the (…) button. This
opens String Collection Editor dialog box. Enter the fol-
lowing names of databases in the editor:
Northwind
Pubs

Click OK to add the databases to the Items collection of


the cboDatabases control.
7. Add the following code directly after the Windows Form
designer generated code:
‘ Declare a Remote object
Dim dbc As IDbConnect
06 0789728206 CH03 2/27/03 9:54 AM Page 245

Chapter 3 .NET REMOTING 245

8. Double-click the form and add the following code in the


Load event handler:

Private Sub DbConnectClient_Load( _


ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
cboDatabases.SelectedIndex = 0
grpQuery.Enabled = False
End Sub

9. Double-click the btnSelect control and add the following


code in the Click event handler:
Private Sub btnSelect_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSelect.Click
‘ Disable the Databases group box and
‘ Enable the Query group box
grpDatabases.Enabled = False
grpQuery.Enabled = True

‘ Register a TCP client channel


Dim channel As TcpClientChannel = _
New TcpClientChannel()
ChannelServices.RegisterChannel(channel)

‘ Register the remote class as a valid


‘ type in the client’s application domain
‘ by passing the Remote class and its URL
Dim dbcf As IDbConnectFactory = _
CType(Activator.GetObject(GetType(IDbConnect), _
“tcp://localhost:1234/DbConnectFactory”), _
IDbConnectFactory)

dbc = dbcf.CreateDbConnectInstance _
(cboDatabases.SelectedItem.ToString())
End Sub

10. Double-click the btnExecute control and add the follow-


ing code in the Click event handler:
Private Sub btnExecute_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExecute.Click
Try
‘ Invoke a method on the remote object
Me.dgResults.DataSource = _
dbc.ExecuteQuery(Me.txtQuery.Text)
dgResults.DataMember = “Results”
Catch ex As Exception
MessageBox.Show(ex.Message, _
“Query Execution Error”)
End Try
End Sub

continues
06 0789728206 CH03 2/27/03 9:54 AM Page 246

246 Par t I EXAM PREPARATION

continued

11. Build the project. This step creates a remoting client that
is capable of activating DbConnect as a CAO.
12. Set the StepByStep3-15, the remoting server, as the startup
project. Select Debug, Start Without Debugging to run
the project. You should see a command window displaying
a message that the server is started in the Client Activation
mode.
13. Now, set StepByStep3-16, the remoting client, as the start-
up project. Select Debug, Start Without Debugging to
run the project. Select a database from the combo box and
click the Select button. An instance of the remotable
object, DbConnect, is created with the selected database.
Now, enter a query in the text box and click the button.
The code invokes a method on the remote object. The
code binds the results from the remote method to the
DataGrid control.

14. Now, again select Debug, Start Without Debugging to


run the remoting client. Select a different database from
the combo box and click the Select button. An instance of
the remotable object, DbConnect, is created with the select-
ed database. Now, enter a query in the text box and click
the button. You should see that the second instance of the
client fetches the data from the selected database. Now
switch to the Server command window. You see that the
remote object is instantiated twice with different databas-
es. This shows that the client activation creates an instance
of remotable object per client.

In Step By Step 3.16, it is important to note that although the


object for DbConnectFactory is created as a server-activated object,
the DbConnect object is always created as the client-activated object.
For the DbConnectFactory object, there will be only one instance for
all the clients; however, there will be one DbConnect object for each
client.
06 0789728206 CH03 2/27/03 9:54 AM Page 247

Chapter 3 .NET REMOTING 247

R E V I E W B R E A K
. The .NET Framework allows you to store remoting configura-
tion details in an XML-based configuration file instead of the
code file. This causes any changes in the configuration file to
be automatically picked up, rather than recompiling the code
files.
. To configure remoting configuration details from configura-
tion files, you should call the
RemotingConfiguration.Configure() method and pass the
name of the configuration file.
. You can distribute the interface assembly to the client instead
of the implementation assembly by creating an interface that
defines the contract and exposes the member definitions to the
client. The remotable class should implement this interface.
. You can use the soapsuds tool to automatically generate the
interface class for the remotable object instead of manually
defining the interface. However, the soapsuds tool only works
when the HTTP channel is used for communication.
. To create an interface that allows creating client-activated
remote objects, you should create an additional interface that
declares as many different methods as there are constructors in
the remotable class. Each of these methods should be able to
create the remote object in a way defined by its corresponding
constructor.

Using IIS As an Activation Agent


So far, in this chapter, you are hosting the remotable class by creat-
ing your own server that is a console application. A major disadvan-
tage with this approach is that you have to manually start the server
if it is not already running.
As discussed before, remoting provides two alternatives to overcome
this disadvantage. You can either run the server process as a
Windows service instead of a console application, or use IIS (which
is a built-in Windows service) as an activation agent for the server
process. I’ll talk about the former alternative in Chapter 6 and the
latter in this section.
06 0789728206 CH03 2/27/03 9:54 AM Page 248

248 Par t I EXAM PREPARATION

Using IIS as an activation agent offers the following advantages:


. You need not write a separate server program to register the
remotable classes.
. You need not worry about finding an available port for your
server application. You can just host the remotable object, and
IIS automatically uses the port 80.
. IIS can provide other functionality such as authentication and
secure sockets layer (SSL).

The following list specifies what you need to do in order to host a


IIS Does Not Support CAO When
TIP

remotable class in IIS:


creating IIS hosted remote objects,
you cannot specify constructor . Place the assembly containing the remotable objects into the
EXAM

parameters. Therefore, activating \bin directory of an IIS Web application or place the assembly
CAO is not possible using IIS. in the GAC (Global Assembly Cache) on the IIS computer.
. Configure the remoting settings by placing the <system.
runtime.remoting> configuration section into the web.Config
file for the Web application. Alternatively, you can write the
configuration code in the Application_Start() method of the
NOTE

Channels and IIS Activation IIS


Activation only supports the HTTP global.asax file in the same way you would register a remote
channel. The default formatting is object in an .exe host.
SOAP, but IIS also supports binary and
. You should not specify a channel. IIS already listens on port
other custom formatting.
80. Specifying a port for a channel causes exceptions to be
thrown when new IIS worker processes are started.
. The well-known object URIs must end with “.rem” or “.soap”
because these are the two extensions, which are registered with
both IIS (via the aspnet_isapi.dll) as well as the remoting
system (in machine.config).

Step By Step 3.17 demonstrates how to use IIS for activating the
DbConnect remotable class from Step By Step 3.10.

STEP BY STEP
3.17 Using IIS As an Activation Agent
1. Add a new Empty Web Project named StepByStep3-17 to
the solution.
06 0789728206 CH03 2/27/03 9:54 AM Page 249

Chapter 3 .NET REMOTING 249

2. Add references to the project StepByStep3-9 (the interface


assembly) and StepByStep3-10 (the remotable object) by
selecting Add Reference from the context menu.
3. Add a Web Configuration File to the project. Open the
web.config file and add the following
<system.runtime.remoting> element inside the
<configuration> element:

<configuration>
<system.runtime.remoting>
<application>
<service>
<!-- Set the activation mode,
remotable object and its URL -->
<wellknown mode=”Singleton”
type=
“StepByStep3_10.DbConnect, StepByStep3-10”
objectUri=”DbConnect.rem”
/>
</service>
</application>
</system.runtime.remoting>
...
</configuration>

4. IIS is now hosting StepByStep3_10.DbConnect, the


remotable class, as a server activated object using the
Singleton activation mode.

If you note the objectUri of the SAO in the preceding example, it


ends with the extension .rem.
Step By Step 3.18 demonstrates how to invoke a remote object host-
ed by IIS.

STEP BY STEP
3.18 Instantiating and Invoking an IIS-Hosted
Remote Object
1. Add a new Visual Basic .NET Windows Application
named StepByStep3-18 to the solution.
2. Add references to the .NET assembly
System.Runtime.Remoting and the project StepByStep3-9
(the interface assembly).
continues
06 0789728206 CH03 2/27/03 9:54 AM Page 250

250 Par t I EXAM PREPARATION

continued
3. In the Solution Explorer, delete the default Form1.vb. Add
a new form named DbConnectClient.vb. Set the new form
as the startup object for the project.
4. Add the following directives:
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Http
Imports StepByStep3_9 ‘ contains IDbConnect

5. Place two GroupBox controls, a TextBox control (txtQuery),


a Button control (btnExecute), and a DataGrid control
(dgResults) on the form. Set the Multiline property of
txtQuery to True. Refer to Figure 3.5 for the design of this
form.
6. Add the following code directly after the Windows Form
designer generated code:
‘ Declare a Remote object
Dim dbc As IDbConnect

7. Double-click the form and add the following code in the


Load event handler:

Private Sub DbConnectClient_Load( _


ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
‘ Register a Http client channel
Dim channel As HttpClientChannel = _
New HttpClientChannel()
ChannelServices.RegisterChannel(channel)

‘ Instantiate the remote class


dbc = CType( _
Activator.GetObject(GetType(IDbConnect), _
“http://localhost/StepByStep3-17/DbConnect.rem”), _
IDbConnect)
End Sub

8. Double-click the Button control and add the following


code in the Click event handler:
Private Sub btnExecute_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnExecute.Click
Try
06 0789728206 CH03 2/27/03 9:54 AM Page 251

Chapter 3 .NET REMOTING 251

‘ Invoke a method on the remote object


Me.dgResults.DataSource = _
dbc.ExecuteQuery(Me.txtQuery.Text)
dgResults.DataMember = “Results”
Catch ex As Exception
MessageBox.Show(ex.Message, _
“Query Execution Error”)
End Try
End Sub

9. Build the project. This step creates the remoting client


that can invoke an IIS-hosted remote object.
10. Set StepByStep3-18, the remoting client, as the startup
project. Select Debug, Start Without Debugging to run
the project. Enter a query in the text box and click the
button. The code invokes a method on the remote object,
which is created when the form loads. The code binds the
results from the remote method to the DataGrid control.

This client was similar to the client created in Step By Step 3.12;
however, there are a few things to note. The client is using the
HTTP channel to communicate with the server, no port number is
specified in the URL, and the URL ends with .rem.

Asynchronous Remoting
So far, all the method invocations that you studied in this chapter
have been synchronous. In a synchronous method call, the thread
that executes the method call waits until the called method finishes
execution.
Synchronous calls can make the user interface very non-responsive if
the client program is waiting for a long process to finish execution.
NOTE

Asynchronous Method Calls For


This is especially true for remote method calls in which additional asynchronous method calls, the
time is involved because the calls are made across the network. remote types need not explicitly sup-
port the asynchronous behavior. It is
The .NET framework has a solution for this in the form of asyn- up to the caller to decide whether a
chronous methods. An asynchronous method calls the method and particular remote call is asynchronous
returns immediately, leaving the invoked method to complete its or not.
execution.
06 0789728206 CH03 2/27/03 9:54 AM Page 252

252 Par t I EXAM PREPARATION

Understanding the Model of Asynchronous


Programming in the .NET Framework
In the .NET Framework, asynchronous programming is implement-
ed with the help of delegate types. Delegates are types that are
capable of storing references to methods of a specific signature. A
delegate declared as follows is capable of invoking methods that take
a string parameter and return a string type:
Delegate Function _
LongProcessDelegate(param As String) As String

So, if there is a method definition such as


Function LongProcess(Param As String) As String
...
End Function

Then the LongProcessDelegate can hold references to the method


like this:
Dim delLongProcess As LongProcessDelegate
delLongProcess = New LongProcessDelegate( _
AddressOf LongProcess)

Note the use of the AddressOf operator in returning the location of


the LongProcess function.
Once you have the delegate object available, you can use its
BeginInvoke() method to call the LongProcess() method asynchro-
nously, such as follows:
Dim ar As IAsyncResult = _
delLongProcess.BeginInvoke(“Test”, Nothing, Nothing)

The IAsync interface is used to monitor an asynchronous call and


relate the beginning and the end of an asynchronous method call.
When you use the BeginInvoke() method, the control will immedi-
ately come back to the next statement while the LongProcess()
method might still be executing.
To return the value of an asynchronous method call, you can call the
EndInvoke() method on the same delegate, such as

Dim result As String = delLongProcess.EndInvoke(ar)


06 0789728206 CH03 2/27/03 9:54 AM Page 253

Chapter 3 .NET REMOTING 253

However, it is important to know where to place the preceding


method call because when you call EndInvoke() and, if the
LongProcess() method has not yet completed execution,
EndInvoke() will cause the current thread to wait for the completion
of LongProcess(). A poor use of EndInvoke(), such as placing it as
the very next statement after BeginInvoke(), can potentially cause an
asynchronous method call to turn into a synchronous method call.
One of the alternatives to this problem is to use the IsCompleted prop-
erty of the ISyncResult object to check if the method has completed
the execution and call the EndInvoke() method only in such cases.
Dim result As String
If (ar.IsCompleted) Then
result = delLongProcess.EndInvoke(ar)
End If

But regular polling of the ar.IsCompleted property requires addition-


al work at the client side. In fact, there’s a better way to do this in the
form of the callback methods. In this technique, you can register a
method that is automatically invoked as soon as the remote method
finishes execution. You can then place a call to EndInvoke() method
inside the callback method to collect the result of method execution.
To implement a callback method with an asynchronous method invo-
cation, you need to take the following steps in the client program:
1. Define a callback method that you want to execute when the
remote method has finished execution.
2. Create an object of delegate type AsyncCallback to store the
reference to the method created in step 1.
3. Create an instance of an object that can receive a remote call
to a method.
4. Declare a delegate type capable of storing references of the
remote method.
5. Using the object from step 3, create a new instance of the del-
egate declared in step 4 to refer to the remote method.
6. Call BeginInvoke on the delegate created in step 5, passing
any arguments and the AsyncCallback object.
7. Wait for the server object to call your callback method when
the method has completed.
06 0789728206 CH03 2/27/03 9:54 AM Page 254

254 Par t I EXAM PREPARATION

Applying Asynchronous Programming


In the following sections, I’ll create a set of three projects to demon-
strate the use of callback methods for an asynchronous method call:
. The Remotable Class—The Remotable class exposes the
remote methods that are called from the client program.
. The Remote Server—I’ll use IIS to host the remotable object.
. The Client Program—The client program calls the remote
method asynchronously.

I’ll create a remotable class that is different from other remotable


classes used in this chapter to help you properly understand synchro-
nous and asynchronous method calls.
The remotable class used in Step By Step 3.19 is named
RemotableClass, and it has a single method named LongProcess().
The LongProcess() method waits for about five seconds to simulate a
long process call.

STEP BY STEP
3.19 Creating a Remotable Class
1. Add a new Visual Basic .NET Class Library named
StepByStep3-19 to the solution.

2. In the Solution Explorer, rename the default Class1.vb to


RemotableClass.vb.

3. Open the RemotableClass.vb and replace the code with


the following code:
Imports System
Imports System.Threading

Public Class RemotableClass


Inherits MarshalByRefObject
Public Function LongProcess(ByVal param As String) _
As String
Thread.Sleep(5000)
Return param
End Function
End Class
06 0789728206 CH03 2/27/03 9:54 AM Page 255

Chapter 3 .NET REMOTING 255

4. Select Build, Build StepByStep3-19. This step generates


the code for your class library and packages it into the file
StepByStep3-19.dll, which is located in the bin\ directo-
ry of your project.

RemotableClass is now ready to be hosted in a remoting host. In


Step By Step 3.20, I decided to host the remotable class using IIS
because it requires only a minimal amount of code. I’ll, of course,
control the remoting configuration by modifying the web.config file.

STEP BY STEP
3.20 Using IIS to Host a Remotable Class
1. Add a new Visual Basic ASP.NET Web Application
named StepByStep3-20 to the solution.
2. Add a reference to the project StepByStep3-19 (the
remotable class assembly).
3. Open the web.config file and add the following
<system.runtime.remoting> element inside the
<configuration> element:

<configuration>
<system.runtime.remoting>
<application>
<service>
<!-- Set the activation mode,
remotable object and its URL -->
<wellknown mode=”Singleton”
type=”StepByStep3_19.RemotableClass, StepByStep3-19”
objectUri=”RemotableClass.rem” />
</service>
</application>
</system.runtime.remoting>
...
</configuration>

4. The remoting server is now hosting the RemotableClass


contained in the assembly StepByStep3-19 for remote
invocation using the Singleton activation mode.
06 0789728206 CH03 2/27/03 9:54 AM Page 256

256 Par t I EXAM PREPARATION

Neither RemotableClass nor the remoting host contains any addi-


tional information that supports an asynchronous method call. The
asynchronous call is completely managed in the client code. So in
the final step, I’ll create a client application that calls a remote
method synchronously as well as asynchronously. Showing both
ways of calling a method will help you to understand the difference.

STEP BY STEP
3.21 Instantiating and Invoking a Client That Calls
Remote Object Methods Synchronously and
Asynchronously
1. Add a new Visual Basic .NET Windows Application
named StepByStep3-21 to the solution.
2. Add references to the .NET assembly
System.Runtime.Remoting.

3. Add a new class file named StepByStep3-19.vb.


4. Replace the code in the StepByStep3-19.vb file with code
to define the interface and location of the remote object:
Imports System
Imports System.Runtime.Remoting.Messaging
Imports System.Runtime.Remoting.Metadata
Imports System.Runtime.Remoting.Metadata.W3cXsd2001

<Serializable()> _
Public Class RemotableClass
Inherits System.MarshalByRefObject

<SoapMethod(SoapAction:=
“http://schemas.microsoft.com/
NOTE

Generating the Proxy Class The


➥clr/nsassem/StepByStep3_19.RemotableClass/
soapsuds tool will generate source
➥StepByStep3-19#LongProcess”)> _
code for a proxy class. However, it will Public Function LongProcess(ByVal param As String) _
only generate this code in C#, not in As String
VB .NET. The Step By Step 3.19 code Return (CType(CType(Nothing, Object), String))
End Function
was developed by running soapsuds
url:http://localhost/StepByStep3- End Class
20/RemotableClass.rem?wsdl -gc
-nowp and then manually translating 5. In the Solution Explorer, delete the default Form1.vb. Add
the C# code to VB .NET. a new form named SyncAsync.vb. Set this form as the
startup object for the project.
06 0789728206 CH03 2/27/03 9:54 AM Page 257

Chapter 3 .NET REMOTING 257

6. Add the following directives:


Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Http

7. Place a TextBox control (txtResults) and two Button con-


trols (btnSync and btnAsync) on the form. Refer to Figure
3.12 for the design of this form.
8. Add the following code directly after the Windows Form
designer generated code:
‘ Remotable object
Dim remObject As RemotableClass

‘ Create a delegate for the LongProcess method


‘ of the Remotable object
Delegate Function LongProcessDelegate( _
ByVal param As String) As String

‘ Declare a LongProcessDelegate
‘ object, an AsyncCallback
‘ delegate object and an IAsyncResult object
Dim delLongProcess As LongProcessDelegate
Dim ab As AsyncCallback
Dim ar As IAsyncResult

‘ Declare an integer variable to hold


‘ number of times the method is called
Dim counter As Integer

9. Double-click the form and add the following code in the


Load event handler:

Private Sub SyncAsync_Load( _


ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
‘ Register a Http client channel
Dim channel As HttpClientChannel = _
New HttpClientChannel()
ChannelServices.RegisterChannel(channel)

‘ Instantiate the remote class


remObject = CType(Activator.GetObject( _
GetType(RemotableClass), _
“http://localhost/StepByStep3-20/
➥ RemotableClass.rem”), _
RemotableClass)

‘ Create a AsyncCallback delegate object to hold


‘ the reference of the
‘ LongProcessCompleted method, which is
‘ called when the asynchronous call is completed
ab = New AsyncCallback( _
AddressOf LongProcessCompleted)

continues
06 0789728206 CH03 2/27/03 9:54 AM Page 258

258 Par t I EXAM PREPARATION

continued
‘ Create a LongProcessDelegate
‘ delegate object to hold
‘ the reference of the LongProcess method
delLongProcess = New LongProcessDelegate( _
AddressOf remObject.LongProcess)
End Sub

10. Double-click the btnSync control and add the following


code in the Click event handler:
Private Sub btnSync_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSync.Click
‘ Increment the method call counter
counter += 1
Dim param As String = String.Format( _
“Call: {0}, Type=Synchronous”, counter)

‘ Append the start message to the text box


txtResults.AppendText(String.Format( _
“{0}, Started at: {1}” & vbCrLf, _
param, DateTime.Now.ToLongTimeString()))

‘ Call the LongProcess method


‘ of the remotable object
remObject.LongProcess(param)

‘ Append the completed message to the text box


txtResults.AppendText( _
String.Format(“{0}, Completed at: {1}” & _
vbCrLf, _
param, DateTime.Now.ToLongTimeString()))
End Sub

11. Double-click the btnAsync control and add the following


code in the Click event handler:
Private Sub btnAsync_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnAsync.Click
‘ Increment the method call counter
counter += 1
Dim param As String = String.Format( _
“Call: {0}, Type=Asynchronous”, _
counter)
‘ Append the start message to the text box
txtResults.AppendText( _
String.Format(“{0}, Started at: {1}\n”, _
param, DateTime.Now.ToLongTimeString()))
06 0789728206 CH03 2/27/03 9:54 AM Page 259

Chapter 3 .NET REMOTING 259

‘ Call the BeginInvoke method to start


‘ the asynchronous method call
ar = delLongProcess.BeginInvoke(param, ab, Nothing)
End Sub

12. Add the following callback, LongProcessCompleted()


method definition, to the form’s code-behind file at the
class level:
Sub LongProcessCompleted(ByVal ar As IAsyncResult)
‘ Call the EndInvoke method to retrieve the return
‘ value of the asynchronous method call
Dim result As String = delLongProcess.EndInvoke(ar)

‘ Append the completed message to the text box


txtResults.AppendText( _
String.Format(“{0}, Completed at: {1}\n”, _
result, DateTime.Now.ToLongTimeString()))
End Sub

13. Build the project. This step creates a remoting client for
the RemotableClass remotable object.
14. Set the StepByStep3_21, the remoting client, as the startup
project. Select Debug, Start Without Debugging to run
the project. Click the Call LongProcess Synchronously
button to call the LongProcess() method synchronously.
You should see the start message appended to the text box,
and the application freezes for the next five seconds. After
the method call is completed, you notice a completed mes-
sage and you are now able to work with the application.
Now, click the Call LongProcess Asynchronously button to
call the LongProcess() method asynchronously. As soon as
the method starts, the start message is appended to the text
box. While the method is executing, you are able to still
work with the application (such as moving the form, click- FIGURE 3.12
The asynchronous method call invokes the
ing buttons and so on, although clicking the synchronous
method and transfers the control back to the
call button freezes the application). When the method is form. The asynchronous call uses a callback
completed, you are notified by appending the completed method to get a notification when the remote
message in the text box as shown in Figure 3.12. method finishes the execution.

In this program, I followed the same steps for an asynchronous


method call that were discussed earlier in the previous section,
“Understanding the Model of Asynchronous Programming in the
.NET Framework.”
06 0789728206 CH03 2/27/03 9:54 AM Page 260

260 Par t I EXAM PREPARATION

R E V I E W B R E A K

. You can choose to run the remoting host as a console applica-


tion, as a Windows service, or as an IIS application.
. You can use IIS as an activation agent only when the underly-
ing communication is in HTTP channel. Using IIS as an acti-
vation agent eliminates the need to write a separate server
program that listens on a unique port number (IIS uses the
port 80).
. When creating IIS hosted remote objects, you cannot specify
constructor parameters; therefore, activating CAO is not possi-
ble using IIS.
. You can invoke a method asynchronously by calling the
BeginInvoke() method on the delegate of that method.

. You can automatically get a notification when an asynchro-


nous method ends. However, you must first create another del-
egate object (of AsyncCallback type) that refers to the callback
method that you need to execute when the remote method
ends. And then you should pass the delegate to the callback
method as an argument to the BeginInvoke() method.
06 0789728206 CH03 2/27/03 9:54 AM Page 261

Chapter 3 .NET REMOTING 261

CHAPTER SUMMARY
The .NET Framework is a modern platform for building distributed
KEY TERMS
applications. In this chapter, you learned how to create distributed
applications using the remoting library of the .NET Framework. • Application domain

You learned that the remoting framework is made up of several ele- • Asynchronous call
ments such as a remotable class, remoting host, remoting client, • Channel
channel, formatters, and so on. Each of these elements is config-
urable and extensible. You practiced creating a remotable class and • Marshalling
hosting that in several modes such as server-activated SingleCall, • Proxy
server-activated Singleton, and client-activated objects. You also used
• Synchronous call
different channels, such as HTTP and TCP, and different format-
ters, such as SOAP and binary. • Remoting
I also demonstrated the use of a configuration file to configure • Runtime Host
remoting for a remoting host and a remoting client. You noted that
depending on your requirements, you can save the remoting config-
uration at two levels—one at the application level (.exe.config,
.dll.config, or web.config for IIS) and the other at machine-level
(machine.config).
Finally, I discussed the technique of invoking a remote method asyn-
chronously with the help of delegates. An asynchronous method
class makes a user-interface–based application, such as a Windows
forms application, quite responsive to user input despite the delays
caused in a remote method call.
06 0789728206 CH03 2/27/03 9:54 AM Page 262

262 Par t I EXAM PREPARATION

A P P LY Y O U R K N O W L E D G E
Exercises <application>
<service>
<!-- Set the
activation mode,
3.1 Using HTTP Channels with Binary
remotable object
Formatters and its URL -->
<wellknown mode=”Singleton”
As I discussed in the chapter, the default formatter for type=”StepByStep3_10.
an HTTP channel is SOAP and for a TCP channel is ➥ DbConnect, StepByStep3_10”
objectUri=
Binary. The formatters are used for serializing and dese-
“DbConnect.rem” />
rializing messages in the specified encoding. </service>
</application>
The chapter has made use of these default formatters in </system.runtime.remoting>
the examples. In this exercise, you’ll learn how to con- ...
</configuration>
figure a channel to use a formatter different from the
default one. 5. IIS is now hosting the StepByStep3-10.
DbConnect, the remotable class, as a server activat-
In particular, I’ll demonstrate how to use the binary
formatter with the HTTP channel. This combination ed object using the Singleton activation mode.
is especially useful when you want to optimize the per- 6. Add a new Visual Basic .NET Windows
formance of a remote object that is hosted on IIS. Application named Exercise3-1_Client to the
However, you should note that binary format is propri- solution.
etary to the .NET Framework.
7. Add a reference to StepByStep3_9.dll (the inter-
Estimated Time: 30 minutes. face assembly containing IDbConnect).
1. Launch Visual Studio .NET. Select File, New, 8. In the Solution Explorer, right-click project
Blank Solution, and name the new solution Exercise3_1_Client and select Add, Add New
310C03Exercises. Click OK. Item from the context menu. Add an Item
2. Add a new Empty Web Project named named Exercise3-1_Client.exe.config based on
Exercise3-1_Server to the solution.
the XML File Template.

3. Add references to StepByStep3-9.dll (the inter- 9. Open the Exercise3_1_Client.exe.config file


face assembly containing IDbConnect) and and modify it to contain the following code:
StepByStep3-10.dll (the remotable object, <configuration>
<system.runtime.remoting>
DbConnect) from the 310C03 solution. You’ll <application>
need to use the Browse button in the Add <channels>
<channel ref=”http”>
Reference dialog box to locate these libraries.
<serverProviders>
<formatter ref =
4. Add a new Web Configuration File to the pro-
“binary” />
ject. Open the web.config file and add the fol- </serverProviders>
lowing <system.runtime.remoting> element </channel>
</channels>
inside the <configuration> element: </application>
<configuration> </system.runtime.remoting>
<system.runtime.remoting> </configuration>
06 0789728206 CH03 2/27/03 9:54 AM Page 263

Chapter 3 .NET REMOTING 263

A P P LY Y O U R K N O W L E D G E
10. In the Solution Explorer, select the project and 16. Double-click the Button control and add the fol-
click the Show All Files button in the toolbar. lowing code in the Click event handler:
Move the Exercise3-1_Client.exe.config file Private Sub btnExecute_Click( _
from the project folder to the bin folder under ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
the project, where the Exercise3-1_Client.exe Handles btnExecute.Click
file will be created when the project is compiled. Try
‘ Invoke a method on
11. In the Solution Explorer, delete the default ‘ the remote object
Form1.vb. Add a new form named Me.dgResults.DataSource = _
dbc.ExecuteQuery(Me.txtQuery.Text)
DbConnectClient.vb and set it as the startup
dgResults.DataMember = “Results”
object for the project. Catch ex As Exception
MessageBox.Show(ex.Message, _
12. Place two GroupBox controls (grpQuery and “Query Execution Error”)
grpResults), a TextBox control (txtQuery), a End Try
End Sub
Button control (btnExecute), and a DataGrid con-
trol (dgResults) on the form. Set the Multiline 17. Build the project. Set the Exercise3-1_Client,
property of txtQuery to True. Refer to Figure 3.5 the remoting client, as the startup project. Select
for the design of this form. Debug, Start Without Debugging to run the pro-
ject. Enter a query in the text box and click the
13. Add the following directives:
button. The code invokes a method on the
Imports System.Runtime.Remoting
remote object. The remote object is serialized and
Imports StepByStep3_9
deserialized in binary format and is transported
14. Add the following code directly after the over the HTTP protocol. The code binds the
Windows Form designer generated code: results from the remote method to the DataGrid
‘ Declare a Remote object control.
Dim dbc As IDbConnect

15. Double-click the form and add the following Note that you should always configure the client to use
code in the Load event handler: the binary formatter when configuring the server (IIS)
to use the binary formatter. You can specify the desired
Private Sub DbConnectClient_Load( _
ByVal sender As System.Object, _ formatter for the client and need not specify the chan-
ByVal e As System.EventArgs) _ nel formatter for the server. The formatter requested by
Handles MyBase.Load
the client will be used to format data by the server for
‘ Load remoting configuration
RemotingConfiguration.Configure( _ that client.
“Exercise3-1_Client.exe.config”)

‘ Instantiate the remote class


3.2 Dynamically Publishing a WellKnown
dbc = CType( _ Object
Activator.GetObject( _
GetType(IDbConnect), _ WellKnown objects cannot be invoked from a client
“http://localhost/Exercise3-1_Server/ with a non-default constructor. You can create an
➥ DbConnect.rem”), IDbConnect)
End Sub
object using any constructor you wish, initialize it any-
way you wish, and then make it available to clients.
06 0789728206 CH03 2/27/03 9:54 AM Page 264

264 Par t I EXAM PREPARATION

A P P LY Y O U R K N O W L E D G E
Use RemotingServices.Marshal() to publish an existing ‘ object to transfer the
‘ object beyond application
object instance. ‘ boundaries with the specified uri
Dim dbcPubs As DbConnect = _
Estimated Time: 30 minutes. New DbConnect(“Pubs”)
RemotingServices.Marshal( _
1. Add a new Visual Basic .NET Console dbcPubs, “Pubs.uri”)
Application named Exercise3-2_Server to the
solution. Dim dbcNorthwind As DbConnect = _
New DbConnect(“Northwind”)
2. Add references to .NET assembly RemotingServices.Marshal( _
dbcNorthwind, “Northwind.uri”)
System.Runtime.Remoting, the StepByStep3-9.dll
(the interface assembly containing IDbConnect), Console.WriteLine( _
“Started server in the “ & _
and StepByStep3-10.dll (the remotable object,
“Singleton mode”)
DbConnect). Console.WriteLine( _
“Press <ENTER> to terminate “ & _
3. In the Solution Explorer, rename the default “server...”)
Module1.vb to DbConnectServer.vb. Open the file Console.ReadLine()
End Sub
and change the name of the module to
DbConnectServer in the module declaration. Set 6. Build the project. This step creates a remoting
this new module to be the Startup object for the server that creates the remotable object
project. StepByStep3_10.DbConnect and is capable of mar-

4. Add the following directives: shalling the remote object across application
boundaries.
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels 7. Add a new Visual Basic .NET Windows
Imports System.Runtime.Remoting. _
Channels.Tcp
Application named Exercise3-2_Client to the
Imports StepByStep3_10 solution.
5. Add the following code in the Main() method: 8. Add references to the .NET assembly
Sub Main() System.Runtime.Remoting, and the project
‘ Create and Register a StepByStep3-9 (the interface assembly).
‘ TCP server channel
‘ that listens on port 1234 9. In the Solution Explorer, delete the default
Dim channel As TcpServerChannel = _
Form1.vb. Add a new form named
New TcpServerChannel(1234)
ChannelServices.RegisterChannel( _ DbConnectClient.vb and set it as the startup
channel) object for the project.
‘ Create the remotable 10. Add the following directives:
‘ object here itself
‘ Call the RemotingServices.Marshal() Imports System.Runtime.Remoting
‘ method Imports System.Runtime.Remoting.Channels
‘ to marshal (serialize) Imports System.Runtime.Remoting. _
‘ the created remotable Channels.Tcp
Imports StepByStep3_9
06 0789728206 CH03 2/27/03 9:54 AM Page 265

Chapter 3 .NET REMOTING 265

A P P LY Y O U R K N O W L E D G E
11. Place three GroupBox controls (grpDatabases, Select Case cboDatabases. _
SelectedItem.ToString()
grpQuery and grpResults), a ComboBox control
Case “Pubs”
(cboDatabases), a TextBox control (txtQuery), a ‘ Instantiate the remote class
Button control (btnExecute), and a DataGrid con- dbc = CType( _
Activator.GetObject( _
trol (dgResults) on the form. Set the GetType(IDbConnect), _
DropDownStyle to DropDownList. “tcp://localhost:
➥ 1234/Pubs.uri”), IDbConnect)
12. Select the Items property of the cboDatabases Case “Northwind”
‘ Instantiate the remote class
control in the Properties window and click on the
dbc = CType( _
(…) button. This opens the String Collection Activator.GetObject( _
Editor dialog box. Enter the following names of GetType(IDbConnect), _
“tcp://localhost:
databases in the editor: ➥ 1234/Northwind.uri”), _
Northwind IDbConnect)
Pubs End Select
End Sub
Click OK to add the databases to the Items col-
lection of the cboDatabases control. 16. Double-click the btnExecute control and add the
following code in the Click event handler:
13. Add the following code directly after the Private Sub btnExecute_Click( _
Windows Form designer generated code: ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
‘ Declare a Remote object
Handles btnExecute.Click
Dim dbc As IDbConnect
Try
‘ Invoke a method on
14. Double-click the form and add the following ‘ the remote object
code in the Load event handler: Me.dgResults.DataSource = _
dbc.ExecuteQuery(Me.txtQuery.Text)
Private Sub DbConnectClient_Load( _
dgResults.DataMember = “Results”
ByVal sender As System.Object, _
Catch ex As Exception
ByVal e As System.EventArgs) _
MessageBox.Show(ex.Message, _
Handles MyBase.Load
“Query Execution Error”)
‘ Register a TCP client channel
End Try
Dim channel As TcpClientChannel = _
End Sub
New TcpClientChannel()
ChannelServices.RegisterChannel( _
channel)
17. Build the project. You now have a remoting
client ready to use.
cboDatabases.SelectedIndex = 0
End Sub 18. Set the Exercise3-2_Server, the CAO remoting
server, as the startup project. Select Debug, Start
15. Double-click the cboDatabases control and add
Without Debugging to run the project. You
the following code in the SelectedIndexChanged
should see a command window displaying a mes-
event handler:
sage that the server is started in the Singleton
Private Sub _ mode. You should also see messages that the
cboDatabases_SelectedIndexChanged( _
ByVal sender As System.Object, _ remote object is already created.
ByVal e As System.EventArgs) _
Handles cboDatabases.SelectedIndexChanged
06 0789728206 CH03 2/27/03 9:54 AM Page 266

266 Par t I EXAM PREPARATION

A P P LY Y O U R K N O W L E D G E
19. Now, set Exercise3_2_Client, the remoting 8. What are the two methods of the Activator class
client, as the startup project. Select Debug, Start that allow you to create instances of remote
Without Debugging to run the project. Select a objects?
desired database from the combo box, enter a
9. What are the advantages of using IIS server as an
query in the text box, and click the button. The
activation agent?
code invokes a method on the remote object that
is already created on the server. The code binds 10. What should you do while creating a remotable
the results from the remote method to the class so that its methods can be called asynchro-
DataGrid control. nously?
20. Now, again select Debug, Start Without
Debugging to run one more instance of the
remoting client. Select a different database from Exam Questions
the combo box, enter a query in the text box, and
click the button. You should see that the second 1. You are designing a distributed application that
instance of the client fetches the data from the hosts a remote object. You want only the autho-
selected database. rized client application to be capable of activating
the remote object. You want to write the applica-
tion with a minimum amount of code. Which of
the following channels enables you to achieve this
Review Questions objective? (Select two choices.)
1. What is an application domain? How does the A. HttpChannel
CLR manage an application domain? B. HttpServerChannel
2. What are MBR objects? What are their advan- C. TcpChannel
tages?
D. TcpServerChannel
3. What is a channel? What are the different types
of channels provided by the .NET Framework? 2. You are designing a company-wide order process-
ing system. This application is hosted on a server
4. What are the advantages and disadvantanges of in the company’s headquarters in Redmond, WA
the Binary and SOAP formatters? and is accessed by 1500 franchise locations
5. What are the two modes to create Server- throughout the world. The application specifica-
Activated objects? tion mentions that the franchisees should be able
to access the order-processing system even
6. When should you choose to create a Client-
through the firewalls. A large number of fran-
Activated object?
chisees access the application over a slow connec-
7. What is the benefit of using declarative configu- tion, and your objective is to maximize the
ration over programmatic configuration? performance of the application. Which of the
following channel and formatter combinations
would you choose in this scenario?
06 0789728206 CH03 2/27/03 9:54 AM Page 267

Chapter 3 .NET REMOTING 267

A P P LY Y O U R K N O W L E D G E
A. Use a TCP channel with a binary formatter Also, any configuration changes made to the pur-
chase order information system should not affect
B. Use a TCP channel with a SOAP formatter
any other applications running on that server.
C. Use an HTTP channel with a binary Which of the following files would you choose to
formatter configure remoting for the purchase order infor-
D. Use an HTTP channel with a SOAP format- mation system?
ter A. gwpoinfo.dll
3. You are designing a distributed application for a B. web.config
large automotive company. The application
C. global.asax
allows the part suppliers across the globe to col-
lect the latest design specifications for a part. The D. machine.config
application is heavily accessed by the suppliers.
5. You have designed a remotable class named
For greater scalability, you are required to design
ProductDesign. You now want to register this
the application so that it can be deployed in a
class with the remoting system in such a way that
load-balanced environment. How should you
the client program should be able to remotely
host the remotable object in this scenario?
instantiate objects of this class and invoke meth-
A. As a server-activated object in SingleCall acti- ods on it. You want there to be only one instance
vation mode of this class on the server irrespective of the num-
ber of clients connected to it. Which of the fol-
B. As a server-activated object in Singleton acti-
lowing code snippets fulfills your requirement?
vation mode
A.
C. As a client-activated object using the HTTP
channel RemotingConfiguration. _
RegisterWellKnownServiceType( _
D. As a client-activated object using the SOAP GetType(ProductDesign), _
“ProductDesign”, _
formatter WellKnownObjectMode.SingleCall)

4. You have been hired by Great Widgets Inc. to cre- B.


ate an application that allows their supplier to RemotingConfiguration. _
access the purchase order information in real time. RegisterWellKnownServiceType( _
You create the required classes that can be activat- GetType(ProductDesign), _
“ProductDesign”, _
ed remotely by the suppliers and package them WellKnownObjectMode.Singleton)
into a file named gwpoinfo.dll. You plan to host
this file using IIS as the remoting host. Your goal C.
is that after the application has been deployed, RemotingConfiguration. _
RegisterActivatedServiceType( _
there should be minimal steps involved to change
GetType(ProductDesign), _
the remoting configuration for this application. “ProductDesign”)
06 0789728206 CH03 2/27/03 9:54 AM Page 268

268 Par t I EXAM PREPARATION

A P P LY Y O U R K N O W L E D G E
D. </service>
</application>
RemotingConfiguration. _ </system.runtime.remoting>
RegisterWellKnownClientType( _
GetType(ProductDesign), _ C.
“ProductDesign”)
<system.runtime.remoting>
6. You have designed a remotable class that allows <application>
<service>
the user to retrieve the latest weather information <activated type=
for her region. You do not want to write a lot of “RemotingWeather.WeatherInfo,
code to create a custom remoting host, so you WeatherInfo”
/>
decide to host the application using IIS. The </service>
name of the remotable class is <channels>
<channel ref=”http server”
RemotingWeather.WeatherInfo, and it is stored
port=”80” />
in an assembly named WeatherInfo.dll. You want </channels>
the users to access this remotable class using the </application>
</system.runtime.remoting>
URL http://RemoteWeather.com/users/
WeatherInfo.rem. Which of the following remot- D.
ing configurations would you place in the <system.runtime.remoting>
web.config file so that client applications can cor- <application>
<client>
rectly retrieve weather information?
<wellknown
mode=”Singleton” type=
A.
“RemotingWeather.WeatherInfo,
<system.runtime.remoting> WeatherInfo”
<application> objectUri=”WeatherInfo.rem”
<service> />
<activated type= </client>
“RemotingWeather.WeatherInfo, </application>
WeatherInfo” </system.runtime.remoting>
/>
</service> 7. You are a software developer for LubriSol Inc.,
<channels> which manufactures chemicals for automobile
<channel ref=”http” port=”80” />
</channels> industries. Your company does major business
</application> with ReverseGear Inc., which is the largest manu-
</system.runtime.remoting>
facturer of heavy vehicles in the country.
B. ReverseGear, Inc. uses a .NET remoting applica-
<system.runtime.remoting>
tion that allows its suppliers to check the daily
<application> parts requirements. Your objective is to create a
<service> client application to the ReverseGear, Inc.’s appli-
<wellknown
mode=”Singleton” type= cation that retrieves the information for parts
“RemotingWeather.WeatherInfo, produced by your company. All you know about
WeatherInfo”
the server application is its URL, which is
objectUri=”WeatherInfo.rem”
/> http://ReverseGearInc.com/Suppliers/Req.rem.
06 0789728206 CH03 2/27/03 9:54 AM Page 269

Chapter 3 .NET REMOTING 269

A P P LY Y O U R K N O W L E D G E
You want the quickest solution. What should you 9. You are developing a remoting client to access a
do in order to successfully write a client applica- server-activated remotable object hosted at the
tion? URL tcp://finance:1234/Budget. You have
obtained an interface assembly of this remote
A. Contact ReverseGear, Inc. to ask for the
object. This assembly contains an interface
interface and include references to the inter-
named IBudget that is implemented by the
face in the client project.
remote class. You want to instantiate the remote
B. Open the URL in the Web browser and select object to invoke a method named
View, Source to find out how the remote class GetDepartmentBudget() that accepts a string
is structured. value and returns a double value containing the
C. Use the Visual Studio .NET Add Web department budget. Given the following code,
Reference feature to add a reference to the what should you write in line 08 in order to suc-
remote class in the client project. cessfully invoke the GetDepartmentBudget()
method? Line numbers are for reference only.
D. Use the soapsuds tool to automatically gener- 01: ‘ Register a TCP client channel
ate the metadata and include the reference to 02: Dim channel As TcpClientChannel = _
this metadata in the client project. 03: New TcpClientChannel()
04: ChannelServices. _
8. You want to host a remotable class via the .Net 05: RegisterChannel(channel)
06: Dim budget As IBudget
remoting framework so that remote clients can 07: ‘ Instantiate the remote class
instantiate the class and invoke methods on it. 08:
09: ‘ Invoke the remote method
The remotable class does not have any user inter-
10: Dim budgetValue As Double = _
face, but it must use Integrated Windows authen- 11: budget.GetDepartmentBudget(“HR”)
tication to authenticate the users. Which of the
following techniques should you use to host the A.
remotable class? You want a solution that requires budget = CType( _
Activator.GetObject(GetType(IBudget), _
you to write minimum code. “tcp://finance:1234/Budget”), IBudget)

A. Use a console application as a remoting host. B.


B. Create a Windows service and use that to budget = CType( _
host the remotable class. Activator.CreateInstance( _
GetType (IBudget), _
C. Use a Windows forms application to host the “tcp://finance:1234/Budget”), IBudget)

remotable class. C.
D. Use Internet Information Services (IIS) as a budget = New IBudget()
remoting host.
D.
RemotingConfiguration. _
RegisterWellKnownClientType( _
GetType (IBudget), _
“tcp://finance:1234/Budget”)
Budget = new IBudget()
06 0789728206 CH03 2/27/03 9:54 AM Page 270

270 Par t I EXAM PREPARATION

A P P LY Y O U R K N O W L E D G E
10. You are developing an application that enables 17: RemotingConfiguration. _
18: RegisterWellKnownServiceType _
client programs to instantiate a class named 19: (GetType(MsdsInfo), _
Inventory. You want the remote object to be 20: “MsdsInfo”, _
created on the server so that it can access the 21: WellKnownObjectMode.Singleton)
22: End Sub
inventory database. However, you want client
programs to have control of the creation and the A. Remove the statement in lines 14 through 16.
lifetime of the remote objects. Which of the fol-
B. Add the following statements just before line
lowing methods of the RemotingConfiguration
17:
class would you choose to register the remotable
ChannelServices.RegisterChannel(tcpChannel)
class with the remoting system on the server? ChannelServices.RegisterChannel( _
httpChannel)
A. RegisterWellKnownServiceType()
B. RegisterWellKnownClientType() C. In the statement in lines 11 through 13,
replace TcpServerChannel with TcpChannel
C. RegisterActivatedServiceType() and similarly in the statement in lines 14
D. RegisterActivatedClientType() through 16, replace HttpServerChannel with
HttpChannel.
11. You work for a large chemical manufacturing
company that has four production units across D. Use same port numbers in the statements in
the country. Your team has the responsibility of line 13 and line 16.
designing a distributed application that allows 12. One of your co-workers has written the following
different production units to share and update code as part of a client application that activates a
material safety information for various products. remote object. She is complaining that her pro-
One of your co-workers is creating a remoting gram is not compiling. What should she modify
host to host a server-activated object using the in the program to remove this error? (Select all
following code, but she is getting an error. What that apply.)
should she do to resolve this error? 01: Function CreateObject() As DbConnect
01: Imports System.Runtime.Remoting 02: ‘ Create channel
02: Imports System.Runtime. _ 03: Dim channel As TcpClientChannel = _
03: Remoting.Channels 04: new TcpClientChannel(1234)
04: Imports System.Runtime.Remoting. _ 05: ChannelServices.RegisterChannel( _
05: Channels.Tcp 06: channel)
06: Imports System.Runtime.Remoting. _ 07: RemotingConfiguration. _
07: Channels.Http 08: RegisterWellKnownClientType( _
08: 09: GetType(DbConnect), _
09: Sub Main() 10: “tcp://localhost/DbConnect”)
10: ‘ Create and Register channels 11:
11: Dim tcpChannel _ 12: dbc = new DbConnect()
12: As TcpServerChannel = _ 13: return dbc
13: New TcpServerChannel(7777) 14: End Function
14: Dim httpChannel As _
15: HttpServerChannel = _
16: New HttpServerChannel(8888)
06 0789728206 CH03 2/27/03 9:54 AM Page 271

Chapter 3 .NET REMOTING 271

A P P LY Y O U R K N O W L E D G E
A. Change line 8 to use the When you deploy the application, users complain
RegisterWellKnownServiceType() method that when they click the button named Validate
instead of the Address, the windows application freezes and they
RegisterWellKnownClientType() method. can’t take further actions until the address has been
verified. What should you do to improve the
B. Change the URL in line 10 to “tcp://
responsiveness of the application?
localhost:1234/DbConnect”.
A. Use the binary formatter instead of the SOAP
C. Remove the port number from the construc-
formatter.
tor of TcpClientChannel() in line 4.
B. Use the TCP channel to communicate instead
D. Change the code in line 10 to
of the HTTP channel.
objectUri=“DbConnect”.
C. Modify the remotable class to support asyn-
13. The soapsuds tool (soapsuds.exe) can be used to
chronous method calls.
automatically generate the interface assembly for
the remotable object. Which of the following D. Modify the Windows application to call the
statements related to the soapsuds tool are methods asynchronously on the remote object.
FALSE? (Select two options.)
15. When you derive a class from
A. The soapsuds tool can be used to generate MarshalByRefObject to make the class remotable,
metadata for server-activated objects. which of the following members of the class are
not remoted? (Select all that apply.)
B. The soapsuds tool can be used to generate
metadata for client-activated objects. A. Non-static public methods
C. The soapsuds tool can be used to generate B. Static methods
metadata for remotable objects registered
C. Non-static private methods
through the HTTP channel.
D. Non-static public properties
D. The soapsuds tool can be used to generate
metadata for remotable objects registered
through the TCP channel.
14. You have designed a Windows application that is Answers to Review Questions
used by the shipping department of a large distrib- 1. The application domain, or AppDomain, is the
ution house. The Windows application instantiates basic unit of isolation for running applications in
a remotable class hosted on Internet Information the CLR. The CLR allows several application
Services (IIS). The remotable class provides various domains to run within a single Windows process.
services to the Windows application such as The CLR ensures that code running in one appli-
address validation and calculation of shipping rates. cation domain cannot affect other application
domains. The CLR can terminate an application
domain without stopping the entire process.
06 0789728206 CH03 2/27/03 9:54 AM Page 272

272 Par t I EXAM PREPARATION

A P P LY Y O U R K N O W L E D G E
2. MBR objects are remotable objects that derive 7. The main benefit of using declarative configura-
from the System.MarshalByRefObject class. MBR tion over programmatic configuration is that you
objects always reside on the server. The client need not recompile the application after changing
application domain only holds a reference to the remoting settings in the configuration file.
MBR objects and uses a proxy object to interact The changes are picked up automatically.
with MBR objects. They are best suited when the
8. The GetObject() (for server-activated objects)
remotable objects are large or when the function-
and CreateInstance() (for client-activated objects)
ality of the remotable objects is only available in
methods are the two methods of Activator class
the server environment on which it is created.
that allow you to create instances of the remote
However, they increase the number of network
objects.
roundtrips between the server application domain
and the client application domain. 9. Using IIS as an activation agent offers the follow-
ing advantages:
3. A channel is an object that transports messages
across remoting boundaries such as application • You need not write a separate server program
domains, processes, and computers. The .NET to register the remotable classes.
Framework provides implementations for HTTP • You need not worry about finding an avail-
and TCP channels. able port for your server application. You can
4. The Binary formatter represents messages in a just host the remotable object and IIS auto-
compact and efficient way, whereas the SOAP matically using the port 80.
formatter is very verbose. The SOAP formatter • IIS can provide other functionalities such as
can be used to communicate in heterogenous authentication and secure socket layers (SSL).
environments, whereas the binary formatter can
be understood only by the .NET applications. 10. A remotable object is capable of being called
5. The two modes to create SAO are SingleCall and asynchronously by default; therefore, no extra
Singleton. SingleCall activation mode creates a efforts are required in order to create remote
remote object for each client request. The object is objects that can be called asynchronously.
discarded as soon as the request completes.
Singleton activation mode creates a remote object
only once, and all the clients share the same copy.
Hence, SingleCall SAOs are stateless, and Answers to Exam Questions
Singleton SAOs maintain state global to all clients. 1. A and B. Your objective is to provide access to
6. CAO are created for each client whenever the only authorized clients. Authorization is a func-
client requests. Hence, CAOs are best suited tion of the remoting host. IIS is the only available
when clients want to maintain private session remoting host that provides you with this capabil-
with remote objects, when the clients want to ity. IIS only supports HTTP communication.
control the lifetime of the objects, or when they Therefore, you can use either the HttpChannel or
want to create a customized remote object with HttpServerChannel channel because both allow
non-default properties. you to listen to incoming messages from clients.
06 0789728206 CH03 2/27/03 9:54 AM Page 273

Chapter 3 .NET REMOTING 273

A P P LY Y O U R K N O W L E D G E
IIS does not support TcpChannel and 7. D. Because you know that the server’s .NET
TcpServerChannel; therefore, if you use these remoting application is using HTTP protocol,
channels, you’ll have to write additional code to you can use the Soapsuds tool to automatically
implement security, and this is not desired in the generate the metadata for the server.
given scenario.
8. D. You should use IIS as the remoting host
2. C. Firewalls generally allow HTTP messages to because IIS has built-in support for Integrated
pass through, and the binary formatter provides a Windows authentication. You’ll have to write
size-optimized format of encoding data. Using additional code to achieve this with the other
TCP might require administrators to open addi- techniques.
tional ports in the firewall, whereas the SOAP
9. A. In a case in which you just have an interface to
format is verbose when compared to TCP and
a class and not the original class, you cannot use
would take bandwidth, which is not a desirable
the New operator to instantiate the remote object.
solution for clients using slow connections.
You should instead use the static methods of the
3. A. Only server-activated objects in SingleCall acti- Activator class. The Activator.GetObject() method
vation mode support load-balancing because they is used to instantiate a server-activated object,
do not maintain state across the method calls. whereas Activator.CreateInstance() method is used
to instantiate a client-activated object.
4. B. You should store the remoting configuration
in a web.config file. This file is an XML-based 10. C. Your requirement is to register a client-activated
configuration file that is easy to modify and does remote object; therefore, you’ll use the
not require a separate compilation step. Storing a RegisterActivatedServiceType() method. The
configuration setting in gwpoinfo.dll or RegisterActivatedClientType() method is used to
global.asax requires the additional step of compi- register the CAO with the remoting system in the
lation before the settings come into effect. The client’s application domain. The other two options
machine.config file is not suggested because any are for creating the server-activated objects.
changes done to it will affect all the applications
11. B. In the preceding program, although you have
running on the server.
created an instance of TcpServerChannel and
5. B. When you want to create just one instance of a HttpServerChannel objects, you haven’t yet regis-
remote object irrespective of the number of clients, tered them with the remoting framework. You’ll
you must create a server-activated (WellKnown) register the channels using the RegisterChannel()
object in the Singleton activation mode. method of the ChannelServices class.
6. B. IIS only supports WellKnown or server-acti- 12. B and C. When creating a client channel, you
vated objects. Therefore, you must use the should not specify a port number with the chan-
<WellKnown> element instead of the <activated> nel constructor. Instead, the port number should
element. Also, you are specifying the configura- be used with the URL of the remote object.
tion for the server; therefore, you must use the
13. B and D. The soapsuds tool is capable of generat-
<server> element instead of the <client> element
ing metadata for server-activated objects on the
inside the <application> element to configure the
HTTP channel.
WellKnown object.
06 0789728206 CH03 2/27/03 9:54 AM Page 274

274 Par t I EXAM PREPARATION

A P P LY Y O U R K N O W L E D G E
14. D. The issue in the question is not of speed You can make the user interface more responsive
but of responsiveness. This behavior is by simply calling the remote method asynchro-
because the Windows application is calling the nously. No modifications are needed on the
methods on the remote object synchronously. remotable object to achieve this behavior.
15. B and C. Only non-static public methods, prop-
erties, and fields participate in remoting.

Suggested Readings and Resources

1. Visual Studio .NET Combined Help 2. Ingo Rammer. Advanced .NET Remoting.
Collection Apress, 2002.
• .NET Remoting Overview 3. www.iana.org/assignments/port-numbers—
List of assigned port numbers.
• Remoting Examples