Tài liệu Creating and Managing Microsoft .NET Remoting Objects - Pdf 90

4
Creating
and Managing
Microsoft .NET
Remoting Objects
CERTIFICATION OBJECTIVES
4.01 Overview of .NET Remoting
4.02 Create a .NET Remoting Object
Using a TCP Channel
4.03 Create a .NET Remoting Object
Using an HTTP Channel
4.04 Client-Activated Objects
4.05 Asynchronous Methods

Two-Minute Drill
Q&A
Self Test
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 4
P:\010Comp\CertPrs8\653-6\ch04.vp
Wednesday, October 30, 2002 9:42:51 AM
Color profile: Generic CMYK printer profile
Composite Default screen
2
Chapter 4: Creating and Managing Microsoft .NET Remoting Objects
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind /
222653-6 / Chapter 4
I
n this chapter, you will explore the distributed object system called .NET Remoting that is
built into the .NET Framework. The short description of .NET Remoting is that it enables
you to interact with software objects that are running under the Common Language

Composite Default screen
The problem with the architecture in Figure 4-1 is that the developer must fully
define the communication and thus know at design time where the remote object is
located and how the network is configured to allow for communication. The first
attempt to solve this communications dilemma was to define a protocol that
allowed two processes to communicate through process boundaries. The generic
name for this protocol is Remote Procedure Call (RPC), and there are a number
of implementations of the RPC protocol. Microsoft uses Distributed Component
Object Model (DCOM) to refer to their version, for example, but DCOM is not
compatible with the RPC used by CORBA.
DCOM uses a proprietary binary protocol to communicate between the different
processes. To alleviate the need for the developer to know the communication code,
DCOM uses a proxy in the client process that encapsulates the remote process and a
stub in the server process that encapsulates the client—the proxy and stub are classes
that encapsulate the networking code. In Figure 4-2, you can see where the proxy
and stub are inserted.
Overview of .NET Remoting
3
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 4
FIGURE 4-1
Communication between two application domains
P:\010Comp\CertPrs8\653-6\ch04.vp
Wednesday, October 30, 2002 9:42:51 AM
Color profile: Generic CMYK printer profile
Composite Default screen
DCOM is a very secure protocol, but it is also proprietary and closed, making
DCOM a Microsoft-only protocol that is tied to the Windows operating system.
DCOM uses a number of TCP/IP ports in order to work and thus poses a security
threat if DCOM is used through a firewall, because those extra ports will have to

from the client and response messages from the server.

Formatter Performs the encoding and decoding of the messages sent
between client and server.

Registration of well-known objects The server object is registered to make
it known on the network.

Configuration of remoting The server object can also be made known on
the network by configuration at the client rather than through registration.

Activation The client activates the server object.
In the following sections, you will learn more about these objects.
Server Object
Before you start coding the server object, you need to determine how the server
object will be marshaled during its remoting. The term marshal refers to the process
.NET Remoting DCOM
No built-in security—relies on other components
to provide the security.
DCOM is secure and can use encrypted
transmissions.
There is a many-to-one relationship between
clients and the server.
There is a one-to-one relationship between client
and server.
Open architecture that can be expanded and
extended at will.
Closed architecture.
The client manages the lifetime of the server. The server manages its own life-time—resulting in
the server having to ask the client if it can unload.

server object, not a copy of the object. Clients can use this reference to call the server
object. These calls do not execute in the client process. Instead, the remoting system
collects all information about the call and sends it to the server process (marshals),
where the call is made to the server object on the client’s behalf. The result of the
call is then sent back to the client. Thus, resources are used for only the critical
information—the call, call arguments, and any return values or exceptions.
Remember that operating system resources should always be marshaled
by reference.
To configure the server object to be marshaled by reference, your class inherits
from System.MarshalByRefObject.
Public Class RemoteHello
Inherits MarshalByRefObject
P:\010Comp\CertPrs8\653-6\ch04.vp
Wednesday, October 30, 2002 9:42:52 AM
Color profile: Generic CMYK printer profile
Composite Default screen
...
End Class
To select the marshal by value model, the class does not inherit from
System.MarshalByRefObject, as in this example.
Public Class RemoteHello
...
End Class
If the remote class is not derived from
MarshalByRefObject
, the object
will be passed by value.
Channel
Applications in the .NET Framework communicate using either HTTP or TCP
channels. When a client calls a method on a remote object, the parameters as

8
Chapter 4: Creating and Managing Microsoft .NET Remoting Objects
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind /
222653-6 / Chapter 4
If you are developing a number of applications using .NET Remoting, it is
easy to make the mistake of using an HTTP Channel to connect to a server
application domain that listens with a TCP Channel. If you do, the client will
receive the following exception: “The underlying connection was closed: An
unexpected error occurred on a receive.” If you receive this exception, you
should check for mismatched channels.
To support the channels, you will need to import the namespace System.Runtime
.Remoting.Channels, as well as one of the following namespaces: for the HTTP
channel, System.Runtime.Remoting.Channels.Http, or for the TCP channel,
System.Runtime.Remoting.Channels.Tcp.
To use a channel, you need to create an object based on the channel object, as in
this example for a TCP channel:
Imports System.Runtime
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels.Tcp
...
Dim chan as TCPChannel
chan = new TCPChannel()
You then register the channel using the
System.Runtime.Remoting.ChannelServices.RegisterChannel() method as in
this example:
...
ChannelServices.RegisterChannel(chan)
...
The equivalent code for an HTTP channel is as follows:
Imports System.Runtime

WellKnownObjectMode.SingleCall)
After calling RegisterWellKnownServiceType(), the server object should wait for a
request from a client, a blocking call.
Channel Default Formatter
HTTP SOAP
TCP Sockets
TABLE 4-2
The Default Formatters
P:\010Comp\CertPrs8\653-6\ch04.vp
Wednesday, October 30, 2002 9:42:53 AM
Color profile: Generic CMYK printer profile
Composite Default screen
10
Chapter 4: Creating and Managing Microsoft .NET Remoting Objects
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind /
222653-6 / Chapter 4
Configuration of Remoting
An alternative to using the preceding method is to use a text file that contains the
information on where the server object can be found. The following example shows
the structure of the configuration file that is used by the server object:
Name#<Application_Name>
WellKnownObject#<Full_Type_Name>#<Assembly_Name>#<Full_Type>
#<Activation_Mode>
Channel#<Channel_Assembly_Name>#<Channel_Full_type_name>
#[Port=<port_number>]
The Server calls the method
System.Runtime.Remoting.RemotingServices.RemotingConfiguration.Configure()
passing the name of the configuration file to the method. The server should now
wait for a request from the client.
The client program can also use the same method to control where the server

A second way of activating the remote object is to use the System.Activator
.GetInstance() method. The client must be configured for this practice to work.
Given that the client is configured to use the remoting object RemHello, the remote
object can be activated using either of the following lines:
remObj As RemHello = new RemHello()
or
remObj = System.Activator.GetInstance(TypeOf RemHell)
Now put it all together so that you can see how all these parts fit in the .NET
Remoting architecture. Figure 4-3 shows you how the parts interact.
The proxy that is located in the client environment is also called a transparent
proxy because the client process is not aware that the method calls to the remote
object actually pass through the proxy. The transparent proxy looks exactly like the
remote object to the client application. There is a real proxy (called the RealProxy)
that actually performs the communication between the client and the server. In
Figure 4-4, you can see the different objects and their communication.
FIGURE 4-3
The .NET Remoting Interactions
P:\010Comp\CertPrs8\653-6\ch04.vp
Wednesday, October 30, 2002 9:42:53 AM
Color profile: Generic CMYK printer profile
Composite Default screen
That is all the theory about .NET Remoting. Now you are going to create a
couple of remote servers and then consume them.
CERTIFICATION OBJECTIVE 4.02
Create a .NET Remoting Object Using a
TCP Channel
Start by building a Hello World .NET Remoting server—this particular server will
use a TCP channel.
12
Chapter 4: Creating and Managing Microsoft .NET Remoting Objects

Basic .NET source file named HelloObj.vb. When you are prompted to
create a new file, click Yes.
C:\VB\Hello>Notepad HelloObj.vb
5.
Create the remote object by defining a new class as follows:
' HelloObj.vb
Imports System
Namespace HelloWorld
Public Class HelloServer
Inherits MarshalByRefObject
' The constructor only writes something on the console
Public Sub New()
' Uncomment the following line to see diagnostics
' System.Console.WriteLine("Building Hello...")
End Sub
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 4
Create a .NET Remoting Object Using a TCP Channel
13
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 4
P:\010Comp\CertPrs8\653-6\ch04.vp
Wednesday, October 30, 2002 9:42:54 AM
Color profile: Generic CMYK printer profile
Composite Default screen
' This is where we do some work greeting our caller
Public Function HelloMethod(str As String) As String
' Uncomment the following line to see diagnostics printed
' Print diagnostics on the console
' Console.WriteLine("Saying Heja to " + str)

Using you favorite editor, create a new Visual Basic .NET source file, named
HelloServer.vb.
4.
Enter the following code in the HelloWorld.vb source file:
14
Chapter 4: Creating and Managing Microsoft .NET Remoting Objects
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind /
222653-6 / Chapter 4
P:\010Comp\CertPrs8\653-6\ch04.vp
Wednesday, October 30, 2002 9:42:54 AM
Color profile: Generic CMYK printer profile
Composite Default screen
' HelloServer.vb
Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Namespace HelloWorld
Public Class RemoteHello
<STAThread> _
Public Shared Sub Main()
' Declare and create the channel object
Dim chan As TcpServerChannel
' Make the TCP channel wait on port 4242
chan = New TcpServerChannel(4242)
' Register the channel
channelServices.RegisterChannel(chan)
' Register the Remote object
RemotingConfiguration.RegisterWellKnownServiceType( _
Type.GetType("HelloWorld.HelloServer, HelloObj"), _

ENTER
to exit. Go
ahead and exit from the server now.
Next you need a client application that uses the .NET Remoting server.
EXERCISE 4-3
Build a Client Application Using a TCP Channel
The client application is built in the same directory as the server:
1.
Open a Visual Studio .NET command prompt.
2.
Navigate to the C:\VB\Hello folder.
3.
Using your favorite editor, create a new Visual Basic .NET source file and
call it HelloClient.vb.
4.
Enter the following code in the source file:
Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.TCP
Namespace HelloWorld
Public Class Client
Shared Sub Main
Dim chan As New TCPChannel()
ChannelServices.RegisterChannel(chan)
Dim obj As HelloServer
obj = CType(Activator.GetObject(Type.GetType _
("HelloWorld.HelloServer,HelloObj"), _
"tcp://localhost:4242/SayHello"),
HelloServer)

2.
Navigate to C:\VB\Hello in both of the command prompts.
3.
In one command prompt window start the Server by entering HelloServer
followed by
ENTER
. The result is shown here:
4.
Switch to the second command prompt window and start the client by
typing HelloClient followed by
ENTER
; the result should be as shown next:
P:\010Comp\CertPrs8\653-6\ch04.vp
Wednesday, October 30, 2002 9:42:54 AM
Color profile: Generic CMYK printer profile
Composite Default screen
5.
The server process should indicate that you said Hello to a client as is shown next:
That concludes the TCP channel exercises. Next you are going to build a more
involved application using the HTTP channel.
CERTIFICATION OBJECTIVE 4.03
Create a .NET Remoting Object Using an
HTTP Channel
This section will explore the use of Internet Information Services (IIS) and the
HTTP channel and how you can build .NET Remoting components using the
Visual Studio .NET.
Projects that will be used for remoting are started as class libraries. That way the
project template will contain the needed settings. Build the Server first.
EXERCISE 4-5
Build a Remote Object Using an HTTP Channel

Create a default constructor for the Hello class. The code should look as
follows:
Public Class Hello
Inherits System.MarshalByRefObject
Public Sub New()
' Default constructor
End Sub
End Class
Create a .NET Remoting Object Using an HTTP Channel
19
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 4
P:\010Comp\CertPrs8\653-6\ch04.vp
Wednesday, October 30, 2002 9:42:54 AM
Color profile: Generic CMYK printer profile
Composite Default screen
20
Chapter 4: Creating and Managing Microsoft .NET Remoting Objects
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind /
222653-6 / Chapter 4
The method that will be the actual service is the Greeting function. The
Greeting function should return a greeting that is personalized. Add the
Greeting method as follows:
Public Function Greeting(ByVal name As String) As String
Return String.Concat("Hello, ", name)
End Function
10.
Build the class library, and make a note of the directory the Hello.dll file is
located in (\bin).
That is all the work involved in building the entire .NET Remoting object. In

5.
Change the name of the XML file to web.config.
6.
After you click Open, the XML file is created in the bin directory as is
shown here:
P:\010Comp\CertPrs8\653-6\ch04.vp
Wednesday, October 30, 2002 9:42:55 AM
Color profile: Generic CMYK printer profile
Composite Default screen
22
Chapter 4: Creating and Managing Microsoft .NET Remoting Objects
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind /
222653-6 / Chapter 4
7.
The new item is opened in the XML editor, and the normal XML processing
directive is inserted as is shown next:
8.
Change the content of the web.config file to the following:
<configure>
<system.runtime.remoting>
<application>
<service>
<Wellknown mode="Singleton"
type="HelloAgain.Greeting, Greeting"
objectUrl="Greeting.soap">
</Wellknown>
</service>
</application>
</system.runtime.remoting>
</configure>

P:\010Comp\CertPrs8\653-6\ch04.vp
Wednesday, October 30, 2002 9:42:55 AM
Color profile: Generic CMYK printer profile
Composite Default screen
24
Chapter 4: Creating and Managing Microsoft .NET Remoting Objects
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind /
222653-6 / Chapter 4
6.
Add a reference to the HelloAgain server by choosing Project | Add
Reference…. You need to use the Browse button and find the directory that
the HelloAgain.dll is located in.
P:\010Comp\CertPrs8\653-6\ch04.vp
Wednesday, October 30, 2002 9:42:56 AM
Color profile: Generic CMYK printer profile
Composite Default screen
Create a .NET Remoting Object Using an HTTP Channel
25
CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 /
Chapter 4
7.
Add a reference to the System.Runtime.Remoting.dll library.
8.
Import the following namespaces into the code module for the frmHello form:
Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.http
Imports HelloAgain
9.


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status