1. Introduction HTTP Remote Server
Like TCP, the HTTP is a protocol for transmitting the ‘Hyper Texts’. The Server which acts on this protocol is called HTTP Server. In the early examples, we used “TCP Channel” to talk with the remote objects on the Server machine. Also, the client projects added reference to the server project to resolve the remote object types. This method works well when a same organization develops both Server and Clients. When a Client and Server are from different organization, giving the code to the Client is not desirable.
In this article, we will learn how to use the ‘HTTP Channel’ as transmission protocol. Also, we will create the Metadata Proxy from this HTTP Remote Server deployed remote objects using the ‘SoapSuds’ command line utility. This article does not explain the basic of remoting. You can read the first article on Dot.Net remoting before going on with this one.
2. Creating HTTP Remote Server
2.1 The Remote Class – RServer
The HTTP Remote Server in our example is a C# console application. It has a simple remote object called RServer. When we created the sample, we gave same name to the project and class. But, it is not a good idea having the same name for namespace and class name. So, I would recommend separating the class name from the Server namespace. As usual, we derive our class from the
MarshalByRefObject
to make it a remote object. Below is the code for it:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//Server 01: Required Declarations using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; //Server 02: Http based Server class RServer : MarshalByRefObject { public void TestObject() { Console.WriteLine("Object Test. Test Object Function called" + Environment.NewLine); } } |
In the
TestObject
function, we are just printing some message. This way we can ensure the server is receiving the call from Client and processing it. Also, note that this time we will use the HTTP Channel and hence in the name-space, we included the ‘
Channels.Http
’.
2.2 Register Remote Object With HTTP Channel
In our HTTP Remote Server application main, we create a http channel and then register the remote object as a Single-Call object. As the basic example (First article on Dot Net remoting) has all the needed explanation, I am skipping those repeated details here. Below is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
static void Main(string[] args) { //Server 03: Create a http channel for soap and register it. HttpChannel httpchnl = new HttpChannel(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 Metadata DLL Through SoapSuds
Once our HTTP Remote Server is ready, we are all set to create the Metadata Assembly using SoapSuds. The client code will talk to the Server through this assembly. Remember, in the previous example when we used the TCP/IP communication channel, we added the whole Server project as a reference. The alternate technique to that is having the declaration in a separate assembly (DLL) and shipping it to the client. Here we will generate separate Metadata DLL for our HTTP Remote Server and going to ship it to the client.
To generate the Metadata DLL, First, we must run the HTTP Server project or EXE. When the Server is in the running state, we launch the Dot net command prompt and access the “SoapSuds” utility. SoapSuds will generate the Metadata DLL for the Client access. Below are 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,
- Option ‘oa’ specifies the output path and name of the DLL
- Option ‘URL’ specifies the HTTP URL to the remote object for which we want to create the Metadata DLL
Video: Generate Metadata DLL Using SoapSuds [No Audio]
4. Client Consuming the Metadata DLL
The client is also Visual C# console application. Once the client project is created, we provide reference to the Required Remoting Assembly references from Dotnet Framework. You can refer the Base Article to know how to do it. After this, the reference to the Metadata DLL is given using the ‘Browse Tab’ of the ‘Add Reference’ dialog box. With this reference set, we have access to the remote object, and we can create it using the new operator.
Video: Setting Metadata DLL Reference in Client [No Audio]
5. Accessing HTTP Remote Server Via Metadata DLL
After referring the Metadata DLL built on the HTTP Remote Server machine using soapsuds, we can access the Remote object using the ‘new’ operator just like you create normal objects. Now, we will explore the client side coding.
5.1 Using the Reference
RServer
is the namespace in the Server. Note, in the Server implementation the Remote class as well as the namespace both have same same. The below ‘
using
’ statement will give access to the Remote Object through client-side proxy.
1 2 |
//Client 01: Use the Meta data dll. using RServer; |
5.2 Creating Remote Object
Next we create the remote object using the ‘new’ operator. But, in the background, through the Metadata DLL, C# is making a call to the wrapped assembly to get the proxy. This proxy object calls the real object on the Server. Also, it knows the communication protocol which is HTTP here, Remote Server’s IP address and Port Number of the communication.
1 2 3 4 |
//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(); |
5.3 Calling Remote Method Of HTTP Server
The remaining lines of code are simple as it just makes a call to the function exposed by the Remote object. In user perspective, it is just an object. But the function call is executed on our HTTP Remote Server. We can observe that by looking at the Server machine’s console window. Below is the piece of code that does not require much explanation:
1 2 3 4 5 6 |
//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: Shows How the Remote Method Call Worked
Source Code Download: HTTP Remote Server Example
Categories: Remoting
Tags: Channels.Http, Metadata DLL, RegisterChannel, RegisterWellKnownServiceType, SoapSuds