Understanding .NET Remoting - Hosting the Remote Object in a Managed .NET Application Executable
(Page 5 of 8 )
In the last section, we hosted the remote .NET object in IIS and then used an ASP.NET page to invoke the object. Now, let's see how we can host the same object in a managed .NET application executable. Take a look at this snippet of code that shows you how your managed application host should register a channel and expose the remote object at a well known remote URN endpoint.
namespace MyDinerHost
{
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.TCP;
class DinerHost
{
public static void Main(String[] args) {
// Create and register the TCPChannel
// Let's listen at port 8080
TCPChannel chan = new TCPChannel(8080);
ChannelServices.RegisterChannel(chan);
// Register the object at a well known URI End point
RemotingServices.RegisterWellKnownType ("MyDiner",
"MyDiner.Diner",
"MyDiner/Diner.soap",
WellKnownObjectMode.Singleton);
// Ready to listen for requests.
// Up and running until the user presses a key
Console.WriteLine("John Doe's Pizza Menu Query Host is up and running");
Console.WriteLine("Press any key to shut me down .....");
Console.ReadLine();
Console.WriteLine("Pizza Service going down. Please Wait .....");
// Unregister the TCP Channel
ChannelServices.UnregisterChannel(chan);
}//Main
}//class
}//namespace You will also need to place the MyDiner.dll assembly in the same directory as the host application or register it with the global assembly cache so that the host application can locate the assembly that contains the Diner object.
You can compile the source from command line using:
csc /out:MyDinerHost.exe /target:exe /r:System.Runtime.Remoting.dll,MyDiner.dll DinerManagedHost.cs This time we will try using a TCP channel for our remote invocation. You will notice how the Main entry point in the application registers a TCP Channel, so that it can listen for incoming requests on a specified port. In the earlier IIS hosting example, IIS took care of listening for HTTP requests.
Now, since we are hosting the object in our very own hosting application, we need to register the channel and then listen for requests on that channel. In the code snippet that we just saw, the ChannelServices.RegisterChannel method call registers a TCP channel and listens on port 8080 for requests. Then, we expose the remote object at a well-known URN endpoint using RemotingServices.RegisterWellKnownType.
The last parameter in RegisterWellKnownType defines the actual activation policy. In this example, we use the Singleton activation policy that uses the same object instance to service every client request. We will take a more detailed look at the various activation policies in the Lifetime management and Activation policies section. So if you use the earlier ASP.NET client to access the service hosted in your .NET executable, you would just need to register a TCP channel in the client instead of a HTTP Channel.
The code snippet below shows how to register a TCP Channel and then activate and invoke a method on the remote object:
// Create and register a tcp channel
TCPChannel chan = new TCPChannel(8081);
ChannelServices.RegisterChannel(chan);
// Create an instance of the remote object
Diner dinerObj = (Diner)Activator.GetObject(typeof(Diner),
"tcp://localhost:8080/MyDiner/Diner.soap");
// Get Todays Menu from the remote Pizza Service
if(dinerObj != null) m_listPizza = dinerObj.getPizzaMenu();
// Unregister the channel
ChannelServices.UnregisterChannel(chan);Next: Transport Channels >>
More ASP.NET Articles
More By Wrox Team