Learning Objectives
After completing this session, you will be able to: Develop .NET Remoting service
Develop and configure Remoting Server and Client
Remoting Application:
.NET remoting enables you to build widely distributed applications easily, whether the application components are all on one computer or spread out across the entire world. You can build client applications that use objects in other processes on the same computer or on any other computer that is reachable over its network. You can also use .NET remoting to communicate with other application domains in the same process.
.NET remoting provides an abstract approach to interprocess communication that separates the remotable object from a specific client or server application domain and from a specific mechanism of communication. As a result, it is flexible and easily customizable. You can replace one
communication protocol with another or one serialization format with another without recompiling the client or the server. In addition, the remoting system assumes no particular application model. You can communicate from a Web application, a console application, a Windows Service – from almost anything you want to use. Remoting servers can also be any type of application domain. Any application can host remoting objects and provide its services to any client on its computer or network.
To use .NET remoting to build an application in which two components communicate directly across an application domain boundary, you need to build only the following:
A remotable object.
A host application domain to listen for requests for that object. A client application domain that makes requests for that object.
Build a Remotable Type
To enable objects in other application domains to use an instance of your class, your class must inherit from MarshalByRefObject.
using System;
public class RemotableType : MarshalByRefObject {
private string StringValue = "This is the RemotableType."; public string StringMethod()
{
return StringValue; }
Host Application
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 two 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 two default channels, HttpChannel (which uses SOAP formatting) and TcpChannel (which uses binary formatting). HttpChannel is a good channel to start with because in some scenarios, it 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. Refer 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 on a per-application-domain basis, the application domain must be running to listen for requests. 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.
Build a Hosting Application
Create a configuration file for the remote class. The host application must be able to find the configuration file to load the configuration for the remote class, and therefore, the configuration file should be saved in the same directory as the host application, or it will not be found and an exception will be thrown. The following code shows the Listener.exe.config configuration file for a host application domain.
<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>
Load the configuration file that configures the remote class: // Listener.cs
using System;
using System.Runtime.Remoting;
public class Listener{
public static void Main(){
RemotingConfiguration.Configure("Listener.exe.config"); Console.WriteLine("Listening for requests. Press Enter to exit...");
Console.ReadLine(); }
}
Build a Client Application
To build a client of the remote type, your application must register itself as a client for that remote object, and then invoke it as though it were within the client's application domain. The .NET remoting system will intercept your client calls, forward them to the remote object, and return the results to your client. The following code procedure describes how to build a simple remoting client.
Create a client configuration file so that the client application can locate the remote object, and save the file in the same folder as the client application. For example the following configuration file tells the remoting system that the type information for the RemotableType remote object can be found in the RemotableType assembly, and that the client should attempt to create and use a RemotableType object located at http://localhost:8989/RemotableType.rem.
<configuration>
<system.runtime.remoting> <application>
<client>
<wellknown type="RemotableType, RemotableType" url="http://localhost:8989/RemotableType.rem" /> </client> </application> </system.runtime.remoting> </configuration> // Client.cs using System; using System.Runtime.Remoting; public class Client
{
{
RemotingConfiguration.Configure("Client.exe.config"); RemotableType remoteObject = new RemotableType(); Console.WriteLine(remoteObject.StringMethod()); }
}
Remoting Task List
.NET remoting is one of several ways to establish communication between application domains using the .NET Framework. You must decide which features your application requires and
consider the resources you have at your disposal before choosing a particular development model for your distributed application. For guidance, see Choosing Communication Options in .NET . The following task lists describe the fundamental steps required to build a basic .NET remoting
application.
Host Tasks
The following steps are required to publish any service for use from outside your application domain:
1. Design your service.
a. Choose a host application domain. b. Choose an activation model. c. Choose a channel and port.
d. Decide how the client will obtain your service's metadata.
2. Implement your host application domain. Remoting hosts might be Windows Services, console applications, Windows Forms applications, Internet Information Services (IIS) processes, or ASP.NET applications. Requirements for each type of application vary, so you should read the documentation describing how to build the type of application you want to use. For more information, see Windows-based Applications or ASP.NET Web Applications. In the host, configure the remoting system for activation mode and other information, such as application name and endpoint. If you want to programmatically configure the system, you do not need to use a configuration file. If you use a configuration file, you must load that file into the system by calling RemotingConfiguration.Configure. 3. In the host, create the appropriate channel and register it with the system by calling
ChannelServices.RegisterChannel. If you use a configuration file, then you must load that file into the system by calling RemotingConfiguration.Configure.
4. The host cannot run without the published class, but the way you build your host environment with your service's implementation depends on how you want to share the public interface of your service.
a) If you are implementing an XML Web service (using an HttpChannel with the default SOAP serialization), your client can obtain the information in three ways:
i. Using the Soapsuds tool (Soapsuds.exe) to extract the information from the endpoint.
ii. Downloading an assembly that contains the metadata. iii. Downloading the source code for an interface.
b) If you are implementing another type of service (for example, using a TcpChannelobject) your client can obtain the information in two ways:
iv. Downloading an assembly that contains the metadata. v. Downloading the source code for an interface.
In either case, the way you package your service in your own hosting application domain will depend on how you want to publish the metadata necessary for others to consume the service.
Client Tasks
The following basic steps are required to consume any service for use from outside your application domain:
1. Design your client.
a) Choose a client application domain.
b) Determine the activation mode and either the client activation URL or the well- known object URL of the remote type.
c) Consider whether you need to register a channel and port. d) Obtain the remote type's metadata.
2. Implement your client application domain. Remoting hosts might be Windows Services, console applications, Windows Forms applications, Internet Information Services (IIS) processes, or ASP.NET applications. Requirements for each type of application vary, so you should read the documentation describing how to build the type of application you want to use. For more information, refer Windows Applicationsor ASP.NET Web Applications.
3. Configure the client remoting system with the activation mode and other type information, such as application name and object Uniform Resource Identifier (URI). If you want to programmatically configure the system, you do not need to use a configuration file. If you use a configuration file, you must load that file into the system by calling
RemotingConfiguration.Configure.
4. Create the appropriate channel and register it with the system by calling
ChannelServices.RegisterChannel. If you use a configuration file, you must load that file into the system by calling RemotingConfiguration.Configure.
Summary
Server activation is used when remote objects are not required to maintain any state between method calls.
Single Call objects service one and only one request coming in.
These objects service multiple clients and hence share data by storing state information between client invocations.
Client activation objects are server-side objects that are activated upon request from the client.
Test your Understanding
1. How do you make an object remotable?
2. What is the formatting type used by HTTP channel? 3. What is the formatting type used by TCP channel? 4. What is the namespace used for Remoting?