Partilhar via


How to: Build a Hosting Application

This topic is specific to a legacy technology that is retained for backward compatibility with existing applications and is not recommended for new development. Distributed applications should now be developed using the Windows Communication Foundation (WCF).

By itself, the RemotableType class defined in the How to: Build a Remotable Type topic is not special. To enable objects in other application domains to create instances of this object remotely, you must build a host or listener application to do the following things:

  • Choose and register a channel, which is an object that handles the networking protocols and serialization formats on your behalf.

  • Register your type with the .NET remoting system so that it can use your channel to listen for requests for your type.

The .NET Framework includes three default channels, HttpChannel (which uses SOAP formatting by default), TcpChannel (which uses binary formatting by default), and IpcChannel (which uses binary formatting by default). HttpChannel can be used through firewalls without opening a port and it supports standard security and authentication protocols. For more information about choosing channels that suit your scenario, see Channels.

You can build listener applications using any type of application domain — a Windows Forms application, an ASP.NET Web application, a console application, a Windows Service (also known as a Windows NT Service), or any other managed application domain. Because remote configuration is done for each application domain, the application domain must be running to listen for requests.

NoteNote

Unlike COM, remoting does not start the host or server application for you. This is an important difference between .NET remoting and remote activation in COM.

Configuration can be done programmatically or by using an application or machine configuration file.

The remoting system uses the information in this file to listen for and route remote requests to an instance of a remotable type. The file specifies the server-activation mode, the type name and assembly of the type on behalf of which it is to listen, and the object Uniform Resource Identifier (URI) or external name of the object. (For more information about object URIs and remoting, see Activation URLs.)

NoteNote

Although there are only a few settings in the preceding configuration file, most of the problems using .NET remoting occur because some of these settings are either incorrect or do not match the configuration settings for client applications. It is easy to mistype a name, forget a port, or neglect an attribute. If you are having problems with your remoting application, check your configuration settings first.

Using a configuration file enables you to change the remoting configuration without recompiling your executable, among other things. For information about the configuration of the .NET remoting infrastructure, see Remoting Settings Schema.

NoteNote

See How to: Compile and Run a Basic Remoting Application for complete instructions on how to build and run this sample.

To implement a simple host application domain that uses a configuration file

  1. Continuing on from the How to: Build a Remotable Type, create another directory under remoting and call it host. Create a configuration file for the remote class. The host application must be able to load the configuration for the remote class, and therefore, the configuration file should be saved in the same directory as the host application's assembly, or it is not found and an exception is thrown. The following code shows a configuration file that specifies the remote object is a Singleton, its implementation is a class called RemotableType located in an assembly called RemotableType. Next, an HttpChannel is registered listening on port 8989. Save this file in the remoting\listener directory. The filename should follow the pattern of app-name.exe.config. In this case it is called listener.exe.config.

    <configuration>
       <system.runtime.remoting>
          <application>
             <service>
                <wellknown 
                   mode="Singleton" 
                   type="RemotableType, RemotableType" 
                   objectUri="RemotableType.rem"
                />
             </service>
             <channels>
                <channel ref="http" port="8989"/>
             </channels>
          </application>
       </system.runtime.remoting>
    </configuration>
    
  2. Create a new source file for your language of choice. At the top of the source file import the System.Runtime.Remoting namespace:

    Imports System
    Imports System.Runtime.Remoting
    
    using System;
    using System.Runtime.Remoting;
    
  3. In the Main method, load the configuration file that configures the remote class, display a message letting the user know the host is running, and then wait for a key press. Save this file in the remoting\listener directory.

    Public Class Listener
        Public Shared Sub Main()
            RemotingConfiguration.Configure("Listener.exe.config", False)
            Console.WriteLine("Listening for requests. Press enter to exit...")
            Console.ReadLine()
        End Sub
    End Class
    public class Listener
    {
        public static void Main()
        {
            RemotingConfiguration.Configure("Listener.exe.config", false);
            Console.WriteLine("Listening for requests. Press enter to exit...");
            Console.ReadLine();
        }
    
    }
    
  4. Copy the RemotableType.dll generated in How to: Build a Remotable Type into the remoting\listener directory. The host application must reference this assembly. Compile this class into an executable by typing the following command:

    vbc /r:RemotableType.dll Listener.vb
    csc /noconfig /r:RemotableType.dll Listener.cs
    
  5. You now have an assembly called Listener.exe. Try running it now to see if the configuration succeeds. A security dialog may be displayed if a firewall is currently blocking the 8989 port. If so click the "Unblock" button to temporarily open the firewall on that port.

  6. For information about how to use the remotable type, see How to: Build a Client Application.

Example

' Listener.vb
Public Class Listener
    Public Shared Sub Main()
        RemotingConfiguration.Configure("Listener.exe.config", False)
        Console.WriteLine("Listening for requests. Press enter to exit...")
        Console.ReadLine()
    End Sub
End Class
// Listener.cs
using System;
using System.Runtime.Remoting;

public class Listener
{
    public static void Main(string[] args)
    {
        RemotingConfiguration.Configure("Listener.exe.config", false);
        Console.WriteLine("Listening for requests. Press enter to exit...");
        Console.ReadLine();
    }
}

See Also

Tasks

How to: Build a Hosting Application

Reference

Remoting Settings Schema

Concepts

Configuration of Remote Applications
Server Activation

Other Resources

Building a Basic .NET Framework Remoting Application