.NET: Using Remoting in .NET


Previous Topic Previous Next Topic Next
Xoc Software
Training
RVBA Conventions
Maya Calendar Program
Company Information
Tools
ASP.NET and Other Tips
.NET: Debugging Designer Features of a Custom Control in Visual Studio
.NET: Setting the Default Font in a Windows Mobile/Compact Framework Custom Control
.NET Fixing C# XML Comments so they Work in Windows XP SP2
.NET: Getting and Setting the Application Version Number
.NET: Getting the Path of the Executing Assembly
.NET: Retrieving Assembly Attributes
.NET: Setting the RootFolder to Other Values in the FolderBrowserDialog in .NET
.NET: Sizing Columns in a ListView Control in .NET
.NET: Using Remoting in .NET
ASP.NET: Constructing a Graphic on the Fly in ASP.NET
ASP.NET: Controlling Caching in ASP.NET Web Forms
ASP.NET: How to use the FrontPage Server Extensions with ASP.NET
ASP.NET: Seeing What is in the ViewState in ASP.NET Web Forms
ASP.NET: Using Forms Authentication in ASP.NET
ASP.NET: View Trace Information on your ASP.NET Web Pages
ASP: Create XML from an ADO query
ASP: Detect Incomplete Loads
ASP: Including an ASP.NET Web Page In a Classic ASP Web Page
ASP: Process .HTM Files with Scripts and Server Side Includes
ASP: QuickSort Algorithm
ASP: Retrieve all server variables from IIS
ASP: Send Email from Active Server Page
HTML: How to Create a Non-Scrolling Region in HTML
IE: Allowing Only Certain ActiveX Controls to Run in Internet Explorer
IIS: Creating a web site for testing in IIS Server
IIS: Creating Multiple Web Sites within IIS on Windows 2000 and Windows XP Professional
IIS: IIS/Visual InterDev Problems and Fixes
IIS: Redirect a domain such as xoc.net to www.xoc.net
SQL Server: Execute SQL Server Updategram
Web Design: Design for People with Disabilities
Web Design: Keep a Web Page out of the Google Cache
Windows: Get HTTP Header of a Web Page using Telnet
Windows: Testing Domain Names without DNS
Windows: Using Hosts File to Access Web Sites with XP SP2
Windows: Windows XP Command Line Tools
Windows Mobile: Reprogramming the Push-to-Talk Button on the AT&T Tilt
Articles
Miscellaneous
Downloads
Links
Search
Email

Other Xoc managed sites:
http://grr.xoc.net
http://www.986faq.com
http://www.mayainfo.org
https://mayacalendar.xoc.net
http://www.yachtslog.com

While Web Services are getting all the attention in .NET, there are other techniques that it supports that are more efficient for communicating between processes. The advantage of a Web Service is that it is a platform neutral mechanism that uses SOAP as the transport mechanism. However, if you are in control of both sides of the communication between processes, it is more efficient to use Remoting. As an example, I use Remoting as the mechanism for having an ASP.NET page talk to a Windows Service.

The basic idea is that you register a TCP channel on one side of the communications, then register a class as a well-known service type. From the other side you can instantiate the class and do things to it. The two processes can be running on the same machine, or they could be on different machines that have tcp access to each other.

So if you have one process running, it can run this code to register MyClass on tcp port 8040. The port is just an arbitrary high number that you use on both sides of the conversation:

chan = new TcpChannel(8040); 
ChannelServices.RegisterChannel(chan); 
RemotingConfiguration.RegisterWellKnownServiceType(
    typeof(MyNameSpace.MyClass), "MyClass", WellKnownObjectMode.Singleton); 

You will also need a class, in this case called MyClass. This class will need to inherit MarshalByRefObject.

using System; 

namespace MyNameSpace 
{ 
    public class MyClass : MarshalByRefObject 
    { 
        public String Check() 
        { 
            return "Success"; 
        } 
    } 
}

On the other side, when you want to call the class, you execute this code:

MyClass mc = (MyClass) Activator.GetObject(typeof(MyClass), "tcp://localhost:8040/MyClass"); 
System.Windows.Forms.MessageBox.Show(mc.Check())

You will need to replace localhost with the domain name or IP address of the machine that you are connecting to if it is not the current machine. If going through a firewall, that port will have to be open. (Web Services go through port 80 by default, so it usually doesn't require any firewall configuration).

Also, if any objects are passed back from the call, they need to have the serializable attribute on them. For example, if you had a collection class called Units, you could pass an object created from this class back from a method by declaring the class like this:

[Serializable] public class Units : CollectionBase 
...

Top