.NET Remoting and the Web

I l @ ve RuBoard

Programming with .NET is all about building distributed applications. You can use the .NET Remoting framework to build applications that communicate with remote objects ”over a network, for example. ASP.NET is another programming framework designed for building scalable applications that present Web pages as the user interface.

The .NET Remoting Architecture

Remoting in .NET is based on object references and proxies. Figure 2-13 shows the .NET Remoting architecture. When a client application wants to use a remote object, it can obtain a remote object reference, called an ObjRef , from an Activator (which is part of the Remoting runtime). The Activator can use information specified in the application configuration file to create the object reference, or the client application can specify in code the type of object reference it requires. The object reference contains information about the remote class, its address, and the transport mechanisms that can be used to contact it.

Figure 2-13. The .NET Remoting architecture

A client can activate a remote reference using the new operator or one of the Activator methods , such as CreateInstance or GetObject . When the reference is activated, a TransparentProxy object is instantiated . This object is responsible for intercepting method calls and bundling parameters into a format suitable for transmission over the network. It passes the bundled data to a RealProxy object, which does the actual sending and receiving of data. If you want to customize the manner in which data is transmitted (to use load balancing and direct requests to different servers, for example), you can create your own RealProxy classes using inheritance. (The TransparentProxy class cannot be extended, however.)

The RealProxy object transmits the bundled data as a message using a channel. A channel is an object that is responsible for sending and/or receiving physical data using a given format and protocol. A message is a serialized request, formatted according to the type of transport implemented by the channel. The .NET Framework supports two types of channels: HTTP, which uses SOAP encoding, and TCP, which uses a binary encoding.

When a request has been transmitted to a server, a channel on the server side of the network unmarshals the data and passes it to the stack builder . The stack builder creates a stack frame comprising the details passed from the client, and then it invokes the appropriate method in the target object. The target object itself sees the request as an ordinary method call. Any return values are passed back from the object and are intercepted by the Remoting runtime services on the server. They are marshaled and sent back through the channel to the RealProxy object on the client machine, where they are unmarshaled and eventually returned to the client process.

The whole architecture is highly customizable ”you can implement custom marshaling, encryption, auditing, or any other type of processing ”but you can use the standard predefined components if you have no special requirements.

Remote Object Activation

A remote object can be managed and activated by a client or by a server. Server activation is useful if an object does not maintain private state between method calls, or if multiple clients need to access the same remote object. In this model, a host application, running on the server machine, is responsible for creating a channel that clients can contact and then registering a class that implements the remote object. A client that sends a request to the channel is then connected to an instance of the registered object. When an object is registered by a host application in this way, it can specify one of two modes, Singleton or SingleCall .

The Singleton mode specifies that all client requests be directed to the same long-running instance of the object. The object is created once, when the first client requests a connection, and the next client will access the same object. This arrangement allows multiple clients to share the object's data. You should make sure that such an object is capable of handling and synchronizing multiple simultaneous client requests.

The SingleCall mode creates a new instance of the remote object on each method call. The target object is destroyed at the end of the method call.

Client activation allows the client application to control the lifetime of an object and does not require a server host application. The Activator object that the client uses to obtain the ObjRef causes the .NET Remoting runtime to instantiate an object on the server machine. A client-activated object is private to the client ”two clients cannot share the same object. The client controls the lifetime of the remote object in the same way it would a local object.

Chapter 11 covers remoting and activation in more detail.

ASP.NET

Originally, Web pages contained just HTML. HTML allowed you to create static Web pages. Then Microsoft invented Active Server Pages (ASP), which allowed you to build Web pages whose content was determined dynamically. The contents of a Web page could be generated using embedded code mixed in with static HTML markup. While this was good in its time, it did have its problems.

The first issue was speed. ASP pages are interpreted, and the code embedded on an ASP page has to be examined, parsed, and compiled to machine code every time the page is accessed. While this delayed compilation makes ASP pages easy to deploy and update (you just copy the ASP file that comprises your page to the correct folder on the Web server), speed and scalability are affected.

With .NET, Microsoft updated the ASP model and created ASP.NET. ASP.NET compiles pages the first time they are accessed and caches them on the Web server. A subsequent request for the same page causes the previously compiled version to be retrieved from the cache and used. If the page is updated, the ASP.NET runtime detects the change, throws away any cached version of the page, and recompiles the new version the next time the page is accessed.

Another issue with ASP is that script code implementing business logic and HTML presentation logic are mixed together. This can make pages difficult to maintain. ASP.NET allows you to separate the business logic from the presentation logic. An ASP.NET file can contain just HTML, but with a directive that refers to a file containing the business logic code. This is known as code behind forms . The code itself can be written in a number of languages, including C#, J#, Visual Basic .NET, and JScript.

A major feature of ASP.NET is the ability to use Web Forms and Web Controls. Web Forms provide the user interface. You can create them using the Visual Studio .NET IDE. You can drop Web Controls onto forms, resize them, and set properties ”it's as simple as programming in Visual Basic! Web Forms and Web Controls also define events ”a command button has a clicked event, for example. You write handlers that respond to these events in your chosen language. All these features allow you to create Web Forms very quickly. The event handlers themselves run on the Web server. The code generated by ASP.NET can automatically remember the state of the controls on a form. After an event has been processed , the same form can be redisplayed with its contents intact or updated.

You'll learn a lot more about ASP.NET in Chapter 16.

Web Services

ASP.NET also provides support for Web services. When Microsoft Internet Information Services (IIS) is configured to use ASP.NET, it listens for incoming SOAP requests as well as requests for regular ASP.NET Web pages. The listener component inside ASP.NET decodes each SOAP request and loads the component that actually implements the service (which is referred to as a Web method ). The listener then unmarshals any parameters in the SOAP request and invokes the Web method in the component, passing the parameters to it. As far as the Web service component is concerned , it is dealing with an ordinary method call, and it knows nothing about SOAP. When the method call is completed, the Web service component passes any return values and output parameters back to the listener, which marshals them into a SOAP response and sends them back to the client. This scenario is illustrated in Figure 2-14.

Figure 2-14. IIS handling a Web service request

Chapter 17 and Chapter 18 cover Web services in detail.

I l @ ve RuBoard


Microsoft Visual J# .NET (Core Reference)
Microsoft Visual J# .NET (Core Reference) (Pro-Developer)
ISBN: 0735615500
EAN: 2147483647
Year: 2002
Pages: 128

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net