SIGN UP MEMBER LOGIN:    
ARTICLE

Accessing Remote Objects Through HTTP Channel

Posted by Sivaraman Dhamodaran Articles | .NET Remoting in C# November 01, 2011
In this article we will look at how we generate a wrapped proxy dll on the server object using SoapSuds. Then we have a look at how the client make use of it.
Reader Level:
Download Files:
 

1. Introduction

 

In the previous examples we used TCP Channel to communicate the remote object in the server machines. Also we have the reference to the server project in the client development projects.  Giving the code to client is not advisable as the client can go ahead and strip the given to know some of the implementation details.

 

In this article we will look at how we use the HTTP channel for communication and how do we create the Meta-data proxy from the server deployed remote objects using the SoapSuds command line utility. OK. Let us start. The explanation is reduced here. Read my first article on Dot.Net remoting link for basic details on remoting.

 

2. The HTTP remote Server

  1. The server is a C# console application. It has simple remote object called RServer. Please note that the namespace name also RServer. It is not good having namespace and class name in the same name. So I would recommend separating the class name from the Server namespace. As usual our RServer class is derived from the MarshalByRefObject class to make it remote object.  Below is the code for it:

     

    //Server 01: Required Declarations

    using System.Runtime.Remoting;

    using System.Runtime.Remoting.Channels;

    using System.Runtime.Remoting.Channels.Http;

     

    //Server 02: Http based Server

    classRServer : MarshalByRefObject

    {

        publicvoid TestObject()

        {

            Console.WriteLine("Object Test. Test Object Function called" + Environment.NewLine);

        }

    }

     

    In the constructor, we are just printing some message so that we can ensure server object is created or not by looking at the console window of the server. Also note that this time we are using the http channel and hence included the channels.http (using System.Runtime.Remoting.Channels.Http;)
     

  2. In the server application main, we are creating a http channel and then registering the remote object as a singlecall object. As the basic example (First article on DotNet remoting) has all the required explanation, I am skipping those repeated details here. Below is the code:

     

    staticvoid Main(string[] args)

    {

        //Server 03: Create a http channel for soap and register it.

        HttpChannel httpchnl = newHttpChannel(7212);

        ChannelServices.RegisterChannel(httpchnl,false);

     

        // Server 04: Register the server object

        RemotingConfiguration.RegisterWellKnownServiceType(typeof(RServer),"RServer",

            WellKnownObjectMode.SingleCall);

     

        // Server 05: Make the server running

        Console.WriteLine("Server Started" + Environment.NewLine);

        Console.ReadLine();

    }

3. Generating a MetaData dll to share with client
 

Once the server is ready we are all set to go ahead and create the Metadata assembly that can be shared with the client piece code. Remember in the previous example when I used the TCP communication channel, I usually ass whole server project as the reference. The alternate technique to that is having the declaration in a separate and assembly (dll) and shipping that to the client. Here we are going to generate separate metadata to generate a dll for our server and going to ship that dll to the client.

 

To generate the Metadata dll, first run the server project or Exe. When the server is in the running state, launch the Dot net command prompt and access the SoapSuds utility. Below is the option switches used to generate the metadata assembly for our server example:

 

SoapSuds -oa:<OutputName>.dll -Url:http://Localhost:<PortNo>/<RemoteObject>?Wsdl

 

In the above command,

oa - option specified the output path and name of the dll

-url - option specifies the http url to the remote object for which we want to create the meta data dll

 

Creating the dll is shown in the below video.

 

Video: Here

 

4. Consuming the meta data dll in the client

 

The client is also Visual C# console application. Once the client project is created, after giving the reference to dotnet remoting, the reference to the meta data dll is given using the browse tab of the add reference dialog box. Once the reference is given, we have access to the remote object and we can create it using the new operator.

 

Getting the reference to the meta data dll in the client project is shown in the below video.

 

Video: Here

 

5. Accessing the Remote object through Http

 

Once you created the reference to the Meta dll formed in the server machine using soapsuds, you can simply access the remote object using the new operator just like you create normal objects. Below is the explanation for the code:

  1. RServer is the name space in the server. Note that in the server implementation the Remote class name as well as the namespace name both is same.
     

    //Client 01: Use the Meta data dll.

    using RServer;
     

  2. The remote object is created using the new operator. But, in the background the Meta data dll it makes a call to the wrapped assembly to get the actual proxy to the real object in the server end. And it know the communication protocol, server address and port number of the communication.

     

    //Client 02: As we have proxy to the remote as wrapped meta data, you can use new oprator

    //              to create the object

    Console.WriteLine("Creating the Instance of Remote object\n");

    RServer.RServer RemoteObj = new RServer.RServer();

     

  3. The remaining lines of code is simple as it just makes a call to the function exposed by the above created object. In user perspective it is just an object. But, the function call is executed on the server. You can observe that by looking at the sever machine's console window. Below is the piece of code that does not require much explanation:

     

    //Client 03: Calling the Remote method

    Console.WriteLine("Press any key to Make a call to Remote function\n");

    Console.ReadLine();

    RemoteObj.TestObject();

    Console.WriteLine("Press any key to Close");

    Console.ReadLine();

     

    Video:Here

     

    Note: The sample is created using the VS2005 IDE.

 

Login to add your contents and source code to this article
share this article :
post comment
 

It is well formated before the publishing. Looks decent. My thanks to the editor who involved in it.

Posted by Sivaraman Dhamodaran Nov 02, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor