1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Creating and Managing Microsoft .NET Remoting Objects pptx

53 441 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 53
Dung lượng 536,69 KB

Nội dung

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 Runtime on a remote host on the network as if it were a local software object running in the same Common Language Runtime. CERTIFICATION OBJECTIVE 4.01 Overview of .NET Remoting The process for using software components (objects) was traditionally one where you as a developer had to code the location of the component and how you were to use that component. There are two distinct ways to use a component in an application—in process and out of process. In process is when a component is executing inside the same application domain as the application that is using the component. All calls to the component are local to the application. Out of process is when the component is located in a different execution process, either on the l ocal host or on a remote host. The client application uses a proxy component that encapsulates the out-of-process component. The system called .NET Remoting is the .NET Framework’s method for providing built-in support for out-of-process components. In this section, you will learn about the building blocks of .NET Remoting—server objects, the channel formatter, well-known objects, and how to register the objects as well as the application communication. The exam will test your knowledge on how to design, build, configure, and use .NET remote software components. The language that is used in the exam is Visual Basic .NET, but the questions are focused on the concepts rather than on the language. Communication Issues The process that a .NET application executes in is called an application domain. The boundaries of this application domain are such that the application cannot access any resources directly through that boundary. In order for an application located in one application domain to communicate with another application in a separate application domain, there must be some communications facility between the two applications, as you can see in Figure 4-1 P:\010Comp\CertPrs8\653-6\ch04.vp Wednesday, October 30, 2002 9:42:51 AM Color profile: Generic CMYK printer profile 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 enabled in the firewall. The answer to these issues (proprietary nature, and security) is to use standard protocols that can be used securely on the Internet for the communication. That is what .NET Remoting uses for communication. In Table 4-1, you can see how .NET Remoting and DCOM compare. You can expect to need to know some things about DCOM for the exam, such as determining what is the best technology to use in a given situation. Remember that DCOM needs multiple ports open on the firewall for access on the Internet. 4 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 FIGURE 4-2 The DCOM model P:\010Comp\CertPrs8\653-6\ch04.vp Wednesday, October 30, 2002 9:42:52 AM Color profile: Generic CMYK printer profile Composite Default screen Overview of .NET Remoting 5 CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 / Chapter 4 The .NET Remoting Architecture The objects (building blocks) that are used in the .NET Remoting architecture appear in the following list: ■ Server object Located at the server, provides the service to the client. ■ Channel Connects the server and the client. Used to send request messages 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. TABLE 4-1 .NET Remoting Versus DCOM P:\010Comp\CertPrs8\653-6\ch04.vp Wednesday, October 30, 2002 9:42:52 AM Color profile: Generic CMYK printer profile Composite Default screen 6 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 of packaging up method calls, with parameters and return values, and passing them across process boundaries. You can select from one of the two marshaling methods— by reference and by value. You use the by reference method when you want to send a reference to the object rather than a copy of the object to the client. By value is the opposite—a copy of the object is sent to the client. Many server objects cannot or should not be copied and moved to some other process for execution. Extremely large objects with many methods can be poor choices for copying, or passing by value, to other processes. Usually, a client needs only the information returned by one or a few methods on the server object. Copying the entire server object, including what could be vast amounts of internal information or executable structures unrelated to the client’s needs, would be a waste of bandwidth as well as of client memory and processing time. In addition, many objects expose public functionality but require private data for internal execution. Copying these objects could enable malicious clients to examine internal data, creating the potential for security problems. Finally, some objects use data that simply cannot be copied in any understandable way. A FileInfo object, for example, contains a reference to an operating system file, which has a unique address in the server process’s memory. You can copy this address, but it will never make sense in another process. The alternative is that the server process passes the client process a reference to the 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 well as other details related to the call are transported through the channel to the remote object. The HTTP channel uses the SOAP protocol to pass messages between the client and the server. These are the features of the HTTP channel: ■ It provides communication between client and server using the HTTP protocol. ■ It provides encoding of data in SOAP. ■ The server receives HTTP requests and sends HTTP responses in ASP.NET and on a TCP socket (port 80 by default). ■ It opens by default a maximum of two concurrent connections on the server. This number can be adjusted. The TCP channel uses a binary stream to pass SOAP-encoded messages between client and server; these are the features of the TCP channel: It provides communication between client and server using the TCP protocol. ■ It provides encoding of data in a binary stream and SOAP. ■ It opens as many connections as there are threads. Overview of .NET Remoting 7 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:52 AM Color profile: Generic CMYK printer profile Composite Default screen 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 Imports System.Runtime.Remoting Imports System.Runtime.Remoting.Channels.Http . Dim chan as HTTPChannel chan = new HTTPChannel() ChannelServices.RegisterChannel(chan) P:\010Comp\CertPrs8\653-6\ch04.vp Wednesday, October 30, 2002 9:42:53 AM Color profile: Generic CMYK printer profile Composite Default screen Overview of .NET Remoting 9 CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET / Lind / 222653-6 / Chapter 4 Formatter The role of the formatter is to encode and decode the messages that are sent between the client and the server on the channel. Some default formatters are based on the channel that is selected, as is shown in Table 4-2. The SOAP formatter is found in the following class: System.Runtime.Serialization.Formatters.SOAP.SoapFormatter. And the binary formatter is found here: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter. Registration of Well-Known Objects The server object needs to be known to the network, and that task is performed by calling the RegisterWellKnownServiceType() method of System.Runtime .RemotingConfiguration. This method is called with information about the server object as the parameters, as you can see in the following code segment: RemotingConfiguration.RegisterWellKnownServiceType(Type.GetType _ ("RemotingSamples.HelloServer, object"), "SayHej", _ 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 object can be found. The following configuration file is used by the client: Name#<application_name> Assembly#<Assembly_Name>#<Remote_Application_Name> #<Full_Type_Name>=<Object_URI> RemoteApplication#<Remote_Application_Name>#<Remote_Application_URI> Channel#<Channel_Assembly_Name>#<Channel_Full_type_Name> #[port-<Port_Number>] At this point, the client application is all set to call the server application. Activation The server object has two distinct ways of being activated—SingleCall and Singleton. These activation modes can be defined as follows: ■ SingleCall mode This mode creates a new instance of the server object for every client connection. Use this mode for server objects that don’t share data. ■ Singleton mode This mode is where one server object is used by all clients. The Singleton mode is used when the data is to be shared between all clients. The client application uses the System.Activator.GetObject() method to get a proxy that represents the remote object. The same call is used for both the Singleton and SingleCall modes. The Type parameter indicates the activation type of the remote object, and the URL is the location of the object as shown in the following code segment: System.Activator.GetObject(Type, URL) P:\010Comp\CertPrs8\653-6\ch04.vp Wednesday, October 30, 2002 9:42:53 AM Color profile: Generic CMYK printer profile Composite Default screen [...]... Basic NET / Lind / 222653-6 / Chapter 4 Creating and Managing Microsoft NET Remoting Objects The remoting communication process 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... Services and Server Components Development with Visual Basic NET / Lind / 222653-6 / Chapter 4 Creating and Managing Microsoft NET Remoting Objects 10 Implement the click event for the btnRemoteHello button In this event handler, you will activate the remote server and then call the Greeting() method to use it Private Sub btnRemoteHello_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles... Chapter 4: Web Services and Server Components Development with Visual Basic NET / Lind / 222653-6 / Chapter 4 Creating and Managing Microsoft NET Remoting Objects The configuration file identifies the application in the element as well as the channel type and namespace 3 Save the project Now there is only one task left, and that is to deploy the... 4: Web Services and Server Components Development with Visual Basic NET / Lind / 222653-6 / Chapter 4 Creating and Managing Microsoft NET Remoting Objects 6 Click Next and set the Permissions for the virtual directory as shown next: 7 Click Next and then Finish to complete the Wizard The final step is to run the client from Exercise 4-8 You do that by opening the RemoteHello project and run the application... Web Services and Server Components Development with Visual Basic NET / Lind / 222653-6 / Chapter 4 Creating and Managing Microsoft NET Remoting Objects Visual Basic NET requires that the libraries that are part of the application be added on the command line This way, the Server application is correctly compiled If you want to see the server in action, you can execute it from the command line at this... MCAD/MCSD XML 32 Chapter 4: Web Services and Server Components Development with Visual Basic NET / Lind / 222653-6 / Chapter 4 Creating and Managing Microsoft NET Remoting Objects SCENARIO & SOLUTION How do I decide whether I need to use a Singleton or SingleCall server-activated object? In the case of server-activated objects, the decision between Singleton and SingleCall activation mode is one of... Services and Server Components Development with Visual Basic NET / Lind / 222653-6 / Chapter 4 Create a NET Remoting Object Using a TCP Channel 13 EXERCISE 4-1 A NET Remoting Server Using a TCP Channel This exercise is built using a text editor and the command-line compiler for Visual Basic NET 1 Open the Visual Studio NET command prompt from Start | Programs | Microsoft Visual Studio NET | Microsoft. .. Services and Server Components Development with Visual Basic NET / Lind / 222653-6 / Chapter 4 Creating and Managing Microsoft NET Remoting Objects 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. .. Components Development with Visual Basic NET / Lind / 222653-6 / Chapter 4 Creating and Managing Microsoft NET Remoting Objects 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: . 4 Creating and Managing Microsoft .NET Remoting Objects CERTIFICATION OBJECTIVES 4.01 Overview of .NET Remoting 4.02 Create a .NET Remoting Object. 4: Creating and Managing Microsoft .NET Remoting Objects CertPrs8 / MCAD/MCSD XML Web Services and Server Components Development with Visual Basic .NET

Ngày đăng: 21/12/2013, 19:15

TỪ KHÓA LIÊN QUAN