Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Microsoft Visual C++ .NET Professional Projects - Premier Press

.pdf
Скачиваний:
180
Добавлен:
24.05.2014
Размер:
26 Мб
Скачать
☆

880 Project 10 NETWORKING AND REMOTING IN VC++ .NET

Proxy Objects – Client Side

By this time, you already know that when a client application domain activates a remote object (marshaled by reference), a proxy object is created, which contains the reference of the remote object, at the client side. The proxy object thus represents the remote object at the client side. Then, whenever the client makes method calls to the server object, the proxy object handles the calls. In this section, I take you on a detailed tour of these Yproxy objects and the way they work. The following are the steps that describeLthe working of proxy objects:

1. At the server side, a remote objectFis registered in an application domain on a remote computer. The object is marshaled by reference, and in the process produces the ObjRefMobject. This object contains information about the remote objectAso that the remote object can be accessed from anywhere on theEnetwork. This information includes the strong name of the class, the class’s hierarchy, the name of all the interfaces that the class implements,Tthe URI of the object, and the details regarding all the registered channels. When the remoting system receives the request for the remote object, the system uses the object URI to retrieve the ObjRef instance for that object.

2. At the client side, when the client activates the remote object by calling the new or any activator function, such as CreateInstance(), the remoting system uses the object URI of the remote object to retrieve the ObjRef instance of that object. In this process, the remoting system creates an instance of the TransparentProxy class and the RealProxy class. Out of these two instances, only the instance of the TransparentProxy class called the proxy object is returned to the client. The TransparentProxy class, registered with the CLR, contains a list of all the classes and interface methods of the remote object.

3.When the client makes a function call to the remote object, the instance of the TransparentProxy class checks the validity of the function call and checks if the instance of the remote object resides in the same application domain as the proxy. If yes, the method call is routed to the actual object. However, if the instance of the remote object resides in a differ-

ent application domain, the call parameters are packaged into an IMessage object and forwarded to the instance of the RealProxy class by calling its Invoke method. Then, the instance of the RealProxy class takes charge of forwarding the messages to the remote object.

Team-Fly®

INTRODUCING REMOTING

Chapter 30

881

 

 

 

 

4.The results from the remote object are streamed as the ObjRef object to the client, where it is unmarshaled to produce an instance of the TransparentProxy class, which is returned to the client.

5.During unmarshaling, the method information of the remote object is

extracted from the ObjRef object and the instances of TransparentProxy and RealProxy are created.

Channel Services

In the beginning of the chapter, you learned that a transport mechanism is required for interprocess communication. In case of communication between remote objects across application domains, the transport mechanism is required to transport information, such as the method call and call parameters, across the application domains. This underlying transport mechanism is provided by the

.NET channel service classes in the namespace System::Runtime::Remoting::

Channels.

The channels that the .NET Framework supplies include HTTP, TCP, and SMTP channels. In addition to these channels, third parties can plug in their own channels (using IChannel) to integrate heterogeneous applications.

All channels are derived from IChannel and implement one or both of the interfaces, IChannelReceiver and IChannelSender. The channels that implement both interfaces enable communication in both directions.

Before I tell you how the channels actually work, I’ll first state certain rules that a channel must follow:

Before a client can call a remote object, at least one channel must be registered with the remoting system. Always remember that channels must be registered prior to registering objects.

A process can contain multiple application domains, and channels are registered per application domain. Therefore, all channels that a process registers automatically get destroyed when a process dies.

The same channel that listens to the same port cannot be registered more than once.

Clients communicate with remote objects using registered channels.

882 Project 10 NETWORKING AND REMOTING IN VC++ .NET

Working of Channels

To understand how channels enable communication between application domains, consider a server application that hosts a remote object. Before a client can communicate with the remote object at the server application, the server application must register channels and the remote objects (to be exposed) with the remoting system.

NOTE

Remember that remote objects do not own channels. In fact, they share channels. Also, remember that at least one channel must be registered with the remoting system before a remote object is registered.

Channels are registered with the ChannelServices class and remote objects are registered with the RemotingServices class. When registering a remote object, the following information needs to be supplied:

The name of the assembly in which the class is contained

The Type name of the remote object

The object URI

The type of the object for server activation, which can be SingleCall or

Singleton

To register a remote object, the preceding information is passed as parameters to

the RegisterWellKnownType method of the RemotingServices class. The follow-

ing code shows the registration of an object as a well-known type:

TcpChannel* pChannel2 = new TcpChannel(8085);

ChannelServices::RegisterChannel(pChannel2);

Type* pType = __typeof(nsRemoting::CQuote);

RemotingConfiguration::RegisterWellKnownServiceType(pType, “Quote”,

WellKnownObjectMode::SingleCall);

Another method of registering a remote object is by storing the preceding information in a configuration file and then calling the ConfigureRemoting method, by passing the name of the configuration file as a parameter.

After the server application registers channels and the remote objects with the remoting system, the channel automatically starts listening at the specified port

INTRODUCING REMOTING

Chapter 30

883

 

 

 

 

for any client requests, and the ObjRef object gets created for the registered remote object. When the client sends a request on a channel, the remoting system examines the message to identify the target object reference. If the object reference is found, the system forwards the call to the respective object.

Secured Communication

Channels provide the underlying transport mechanism for communication between application domains. When messages travel to and from the application domains, the security of messages is required, especially when developing business applications. The security issue is taken care of by encoding messages before sending them and then decoding messages at the receiving end. This encoding and decoding of messages between application domains is implemented through the

.NET serialization formatters in the namespace System::Runtime::Serialization:: Formatters.

The .NET formatters include two native formatters:

Binary. TCP channels use binary formatters to serialize all messages in binary format. Then, the TCP channel transports the encoded message to the target URI using the TCP protocol.

SOAP. HTTP and SMTP channels use SOAP to serialize messages in XML format. Then, the encoded messages are transported to the target URI using the SOAP protocol.

NOTE

The Binary formatter belongs to the System::Runtime::Serialization::Formatters::Binary namespace. The SOAP formatter belongs to the System::Runtime::Serialization::Formatters::Soap namespace. By default, the HTTP and SMTP channels use SOAP, whereas TCP channels use binary payload to communicate.

In addition to the two native formatters available with .NET, the serialization formatters are also pluggable. Serialization formatters are pluggable by implementing the IRemotingFormatter interface. Then, you can select a combination of the channel and the formatter depending on the security needs of your application.

884 Project 10 NETWORKING AND REMOTING IN VC++ .NET

Remoting Metadata and Configuration

Files

Remoting metadata is the information about components that enables cross-lan- guage programming. The Remoting system uses the information contained in the metadata to create proxy objects at the client side. Similarly, formatters use metadata to encode and decode method calls.

Clients also require information stored in metadata to access remote objects. Clients can use the following tools to access the metadata information:

The .NET Assembly. The server object creates a metadata assembly and distributes the same to the clients. Then, when the client is compiled, the client can reference the assembly. This method is used when both the server and client components are managed components.

Web Services Description Language (WSDL). The remote object generates a WSDL file that contains the information about the object and its methods. For a client to be able to communicate to the object using SOAP, the client should be able to read and generate SOAP requests corresponding to the WSDL file.

The SOAPSUDS utility. The .NET clients can use the SOAPSUDS utility to download the XML schema, which is generated at the server, and generate the source files that contain only the metadata and not the source code.

In addition to metadata information, the remoting information is stored in a configuration file (.cfg) for a given object. Each application domain contains a configuration file of its own. The information that a configuration file contains includes the following:

Information pertaining to the host application

Name of objects

URI of objects

Channels being registered

Lease time information for server objects (covered in the next section of this chapter)

The configuration files contain remoting information, which can be generated programmatically. However, keeping a separate configuration file has one main

INTRODUCING REMOTING

Chapter 30

885

 

 

 

 

advantage. Because configuration files contain the remoting information separately, which is transparent to the client code, any future changes can be reflected by modification in the configuration files. However, on the other hand, if the remoting information were generated programmatically, you would have to edit and recompile the source file.

Object Lifetime

Based on the manner in which a remote object is activated, the .NET Framework supports two types of objects:

Client-activated objects. Activated from the client side. The lifetime of these objects is determined by the lease manager; the object dies when the lease expires.

Server-activated object. Activated at the server side. These objects can

be Single Call or Singleton.

Next, you’ll learn how the lease manager controls the lifetime of the client-acti- vated objects.

When a client activates a remote object, the lease is created with a default lease time. The lifetime of the remote object is determined by this lease. When the lease reaches a value 0, the lease is said to expire, and consequently the remote object does not remain connected to the .NET Framework. When the application domain has been freed of all references of the remote object, the object will be collected when the garbage collection (GC) occurs.

The leases for various objects are administered by the lease manager, which is available with each application domain. Leases implement the ILease interface. Each time a lease is created, the lease time is set to the maximum of the current LeaseTime. The lease time also includes the RenewOnCallTime. When the lease is expired, the lease manager looks for sponsors (implementing the ISponsor interface) that are given an opportunity to renew the lease.

Although objects have a default lease time, the default lease time can be extended in several ways so that remote objects have a longer lifetime. The following are the different ways to extend the lease time:

The lease time of the server object can be set to infinity. This prohibits the collection of objects during garbage collection cycles.

886Project 10 NETWORKING AND REMOTING IN VC++ .NET

The client can renew the lease time of the server object. To do so, the

client first calls the RemotingServices.GetLifetimeService method.

This method returns the lease of the server object from the lease manager of the application domain. Then, to renew the lease, the client can call the Lease.Renew method.

The client can register a sponsor for a lease with the lease manager of the application domain. Then, when the lease expires, the client can request the sponsor for renewal of the lease.

If the RenewOnCallTime property of the lease is set, the lease gets renewed by the amount specified in the RenewOnCallTime property, whenever the object is called.

Summary

In this chapter, you were introduced to the .NET remoting architecture and how it works. You have learned the intricacies of how remote objects are created, exposed, and passed at the server side. Then, you learned how remote objects are called at the client side. You also learned about the .NET communication channels that are required for communication between different application domains.

Chapter 31

Implementing

Remoting

In the previous chapter, you learned about how remoting plays an important part in the .NET architecture. You have learned the concepts behind creating remoting servers and clients. In this chapter, you will create a remoting application,

complete with a server and a client.

NJ Finances – The Scenario

NJ Finances (NJF) is a reputed company based in New York. The company has a large number of employees who work in different departments such as administration, sales, and marketing. Although the company is a financial services company, most of its employees aren’t very sound with basic concepts in finance. It is not really necessary for all employees to be financially savvy in order to function at their best; for example, a manager in the administration department does not really need to know about the best mutual funds around. However, the management feels that being financially educated is not only beneficial at a personal level but will also help an employee who is not involved in the core activities of the company to better appreciate the moves and initiatives of the company. Toward that end, the company has many tutorials on financeand money manage- ment–related topics on the company’s intranet.

However, the employees are not enthusiastic to learn about finance-related topics, and their response to the management’s initiatives is not overwhelming. Therefore, the company has instituted monetary incentives to promote the initiative. This scheme is still at a preliminary stage. In order to qualify, an employee has to take quizzes on various topics. The company has its own MIS and software department, which has been asked to create an application that will allow employees who think they know enough about a topic to take an online quiz. The software development is all set to create the “Quiz Application.”

Project Life Cycle

The development life cycle of a project, as discussed in Chapter 8, usually involves three phases:

IMPLEMENTING REMOTING

Chapter 31

889

 

 

 

 

Project initiation

Project execution

Project deployment

In the project-initiation phase, the project plan was prepared and the development team for the project was also identified. The development team further prepared a comprehensive list of tasks involved in the execution and deployment phases of the project’s life cycle.

Now, the team is in the project-execution phase and has analyzed the requirements that the applications should cater to.

Requirements

The application needs to be installed on the NJ Finances network, which is Win- dows-based. The Quiz Application will consist of a server part and a client part. The client part can be executed from the machine of any employee who wants to take the quiz. The server part will run on a central Windows 2000 server using the .NET platform. For networking plumbing, the application will make use of

.NET Remoting.

Eventually, once the scheme of monetary incentives becomes fully applicable, the test items and the results will be stored in a database. For now, the team at NJF has decided to create an application that will simply have a server and a client part. The questions will be hard-coded in the server component.

Design

During this phase, the functionality to be implemented and the technology to be used are determined. It was decided that the application would make use of .NET Remoting for its networking requirement. The client will be a GUI application that will display the test items, allow the user to answer, and in the end display the result.

Construction

During this phase, the NJF team will construct the application. The primary tasks that the team needs to perform are:

Design and create the server application

Design and create the client application