WebReference.com - Part 1 of Chapter 6: Professional C# Web Services, from Wrox Press Ltd (4/8)

To page 1To page 2To page 3current pageTo page 5To page 6To page 7To page 8
[previous] [next]

Professional C# Web Services

Machine.config

We can find predefined channels in the machine-wide configuration file machine.config. This configuration file is in the directory %SystemRoot%\Microsoft.NET\Framework\<vx.x.x>\CONFIG.

Six channels are predefined in this file, as we can see in the following XML segment. The id attribute defines the identifier of the channel that can be used with the ref attribute as we have done in the application configuration file to reference the tcp server channel. The type attribute defines the class and assembly name of the channel. With the id we can easily guess the protocol that is used by channel. The id also indicates if the channel can be used on the client or server side. The http and tcp channels include both client and server functionality:

      <channels>
          <channel
              id="http"
              type="System.Runtime.Remoting.Channels.Http.HttpChannel, 
              System.Runtime.Remoting,
               Version=1.0.3300.0, Culture-Neutral,PublicKeyToken-b77a5c561934e089" />
          <channel
              id="http client"
              type="System.Runtime.Remoting.Channels.Http.HttpClientChannel, 
              System.Runtime.Remoting,
               Version=1.0.3300.0, Culture-Neutral,PublicKey Token-b77a5c561934e089" />
          <channel
              id="http server"
              type="System.Runtime.Remoting.Channels.Http.HttpServerChannel, 
              System.Runtime.Remoting,
               Version=1.0.3300.0, Culture-Neutral,PublicKey Token-b77a5c561934e089" />
          <channel
              id="tcp"
              type="System.Runtime.Remoting.Channels.Tcp.TcpChannel, 
              System.Runtime.Remoting,
               Version=1.0.3300.0, Culture-Neutral,PublicKey Token-b77a5c561934e089" />
          <channel
              id="tcp client"
              type="System.Runtime.Remoting.Channels.Tcp.TcpClientChannel, 
              System.Runtime.Remoting,
               Version=1.0.3300.0, Culture-Neutral,PublicKey Token-b77a5c561934e089" />
          <channel
              id="tcp server"
              type="System.Runtime.Remoting.Channels.Tcp.TcpServerChannel, 
              System.Runtime.Remoting,
               Version=1.0.3300.0, Culture-Neutral,PublicKey Token-b77a5c561934e089" />
      </channels>

Starting the Channel

All the server has to do is read the configuration file and activate the channel. This can be done with a single call to the static method RemotingConfiguration.Configure().

Here the server is implemented in a console application. RemotingConfiguration.Configure() reads the configuration file SimpleServer.exe.config to configure and activate the channel. The creation of the remote object and communication with the client is done by the remoting infrastructure; we just have to make sure that the process doesn't end. We do this with Console.ReadLine() that will end the process when the user enters the return key:

// SimpleServer.cs
using System;
using System.Runtime.Remoting;
namespace Wrox.Samples
{
   class SimpleServer
   {
      static void Main(string[] args)
      {
         RemotingConfiguration.Configure("SimpleServer.exe.config");
         Console.WriteLine("Press return to exit");
         Console.ReadLine();
      }
   }
}

We compile the file SimpleServer.cs to a console application:

csc /target:exe SimpleServer.cs

We have to either copy the assembly of the remote object class to the directory of the server executable, or make a shared assembly and install it in the global assembly cache. The compiler doesn't complain that we are not referencing it because we didn't use the type MyRemoteObject in our server application. But the class will be instantiated from the remoting framework by reading the configuration file, so the assembly must be in a place where it can be found. If you get the exception System.Runtime.Remoting.RemotingException: cannot load type Wrox.Samples.MyRemoteObject while running the client application, remember this issue.


To page 1To page 2To page 3current pageTo page 5To page 6To page 7To page 8
[previous] [next]

Created: February 13, 2002
Revised: February 13, 2002


URL: http://webreference.com/programming/csharp/webservices/chap6/1/4.html