Using Channel Protocols and Formatters

Channels are a .NET Framework class from the System.Runtime.Remoting namespace. These are the objects that transport messages and data across process or machine boundaries. A channel registered by the remote server application can listen on a specific endpoint, wait for an incoming message, and then send a response back to the calling client application. The channel registered by the client can also send and receive data and messages. Obviously, channel protocols and port numbers must match for the communication between client and server to be successful.

The .NET Framework provides two commonly used formatter classes. The formatter is responsible for writing the object’s description and data so that this information can be sent across the network connection. This is called serialization. Serialization is the process of creating a representation of an object and its state that can be transferred across the network from one component to the other. The SOAP formatter uses a format of XML to write the information in a standardized way that can be understood by other applications. The binary formatter creates a binary data stream that is understood by other .NET applications.

In this section, you will learn the capabilities of these two classes and see some code examples.

Selecting a Channel Protocol and Formatter

.NET Remoting channels support two basic communication protocols; these are represented by the HTTPChannel class and the TCPChannel class.

The HTTP channel uses the familiar Hypertext Transport Protocol (HTTP), a widely used standard on the Internet, to pass data. By default, the HTTP channel uses the Simple Object Access Protocol (SOAP) formatter to send the message call as an XML document. The standard SOAP message format is also used by XML Web services and is explained in detail in Chapter 4.

The Transmission Control Protocol (TCP) channel uses a lower-level network transmission protocol and by default formats messages by using the binary formatter class, which creates a binary data stream. This results in a smaller and faster transmission, but requires that clients on both ends of the transmission are using the .NET Framework and can understand this format. The TCP channel also does not support some security mechanisms that are provided when using the HTTP protocol and hosting your remote server in IIS, such as Secure Sockets Layer (SSL) or Windows integrated security to authenticate users.

For the greatest interoperability and to take advantage of the enhanced security features, Microsoft recommends using the HTTP channel with the SOAP formatter. If you are working within a closed network, and all the applications participating are running managed code, you might choose the TCP channel for its faster performance.

You can also choose to use the binary formatter with an HTTP channel or the SOAP formatter with a TCP channel if your application design is better served by these options. This can be accomplished by supplying the type of formatter to use—either as a parameter to one of the overloaded constructor methods of the channel object or in a configuration file (configuration files are covered later in this chapter, in the section titled “Using a Configuration File”). It is also possible to extend the .NET Framework classes to create customized channels and formatters to add functionality to your applications—for example, to implement custom security features. However, this is outside the scope of the exam and this book.

Registering a Channel

The server application must register a channel before any clients can contact it. When you register a TCP channel or an HTTP channel, you must assign a port number so that communications can be directed to the application. Port numbers 0 through 1023 are reserved for common applications (for example, web browsers use port 80 by convention), so you should not specify these port numbers for your .NET Remoting channels. You can specify any other port number (up to 65,535) when you register a channel. Be careful that you are not trying to use a port that is already in use by another application running on the same computer. Microsoft SQL Server, for example, uses ports 1443 and 1434.

The sample code in Listings 3.1 and 3.2 show how to register a channel and assign a port. Listing 3.1 assigns a port number of 8085 to the TCPChannel object. Listing 3.2 assigns a port number of 8086 to the HTTPChannel object. In order to use these objects in your code you will have to add a reference to your project to the System.Runtime.Remoting namespace.

Later in this chapter you will see how to register a channel and assign a port by using a configuration file instead of placing the instructions in your source code.

Listing 3.1: Registering a TCPChannel

start example
Imports System.Runtime.Remoting Imports System.Runtime.Remoting.Channels Imports System.Runtime.Remoting.Channels.Tcp Public Class Server     Public Shared Sub Main()       Dim myTCPChan As New TcpChannel(8085)       ChannelServices.RegisterChannel(myTCPChan)     End Sub End Class
end example

Listing 3.2: Registering an HTTPChannel

start example
 Imports System.Runtime.Remoting Imports System.Runtime.Remoting.Channels Imports System.Runtime.Remoting.Channels.Http Public Class Server     Public Shared Sub Main()       Dim myHTTPChan As New HttpChannel(8086)       ChannelServices.RegisterChannel(myHTTPChan)     End Sub End Class
end example



MCAD/MCSD(c) Visual Basic. NET XML Web Services and Server Components Study Guide
MCAD/MCSD: Visual Basic .NET XML Web Services and Server Components Study Guide
ISBN: 0782141935
EAN: 2147483647
Year: 2005
Pages: 153

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