February 01, 2008

Remoting, Remote Computing

Remote Computing

http://del.icio.us/rss/learnerplates/remoting
http://del.icio.us/search/?fr=del_icio_us&p=remoting&type=user


There are 2 main ways in .NET to invoke a method and/or create an instance of an object on machine other than the one your
1. Webservices
2. .NET Remoting.

1. Webservices allow the client to communicate with the server be emitting an receiving XML, these XML messages are first wrapped on both the client and server side into SOAP envelopes..NET Remoting on the other hand allows a remote client to invoke a method through Proxy classes.

2. .NET Remoting
You can access remote objects in 2 ways, by reference and by value. The first manipulates the actual original object on the server(the objects class must be MarshalByRefObject) , the second a copy of the original object is used which means any changes made will only effect the local copy, copy on the client, of the object (the objects class must be Serializable).

The communication between Server and Client is taken care of for you by a Proxy class. All you have to do is create your remote class, inherit from MarshalByRefObject (or implement the ISerializable interface depending on how your client will be calling your remote object), usually inherit from MarshalByRefObject .
Create a Server class which creates an a remote instance of the remote class and also create a Channel to allow the client to communicate with the remote object.


Here's a very simple Remote class:

public class RemotingClass : MarshalByRefObject
{
public string HelloWorld()
{
return "Hello World!";
}
}
Or better still, in order to hide your actual remote objects implentation, create an interface in a common dll instead (below) of a concrete class (above).
Include the dll as a reference on you Server side and implement the interface with some class. On the Client side also include the dll as a reference and use the interface type to get your remote object instance.
like so:
public interface IRemotingClass
{
string HelloWorld();
}
And change your remote class implementation accordingly
public class RemotingClass : MarshalByRefObject, IRemotingClass
{
public string HelloWorld()
{
return "Hello World!";
}
}
The client code calling this method could be a standalone app (Console App) or a web application (Http Client, running on IIS), the Console App would use TcpChannel the Http Client would use HttpChannel.
Here's a simple Server class (Note if you plan on running your server as a web app or in a web application, place this code inside your Global.asax files Application_Start method and remove the port numbers they're no longer required, if you do this your RemotingClass.cs will need to be in the App_Code dir) :

using System.Runtime.Remoting;
using System.Runtime.Remoting.Services;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
public partial class ServerApp
{
public void main()
{
// Create an instance of a channel
HttpChannel channel = new HttpChannel(1234);//the client must use this port too
ChannelServices.RegisterChannel(channel); // Register as an available service with the name
HelloWorld RemotingConfiguration.RegisterWellKnownServiceType(
typeof (Remoting.RemotingClass),
"RemotingClass.soap", //The name used by client when requesting this remote object, this tells IIS to deal with the request as a Remote request and let the .NET Remoting take care of it.
WellKnownObjectMode.Singleton); // Create an instance of the remote object

}


The Client code then creates an remote instance of the object using a url with the name just given, "RemotingClass.soap", and a channel to the same object with the same port 1234.


class ClientStartup
{
static void Main(string[] args)
{
//Send out messages on any port port
HttpChannel httpChannel = new HttpChannel();
//Requests will be sent through Http, which are SOAP messages.
ChannelServices.RegisterChannel(httpChannel);
//Get a reference to the remote object using the url
//Note that the same port number is used as on the Server, 1234
IRemotingClass remoteClass = (IRemotingClass)Activator.GetObject(
typeof(IRemotingClass),
"http://localhost:1234/RemotingClass.soap");

string result = remotingClass.HelloWorld();
Console.WriteLine("The Customer has been gotten on the client remotely with the age : " + result);
}
}

Hosting the Remote objects on IIS

When your objects live in a Web Application i.e. in IIS, you can use IIS's power to do some of the work for you, like security.

The above example can be run on IIS by simply placing a version of the code we had in the Server's main method into our web applications Global.asax files Application_Start method. This ensures that the remote service is initiallized only once i.e. when the web app starts.

The code will have to change slightly

HelloWorld RemotingConfiguration.RegisterWellKnownServiceType( typeof (Remoting.RemotingClass), "RemotingClass.soap", WellKnownObjectMode.Singleton);

As you can see the code has been simplified, there's no HttpChannel or Port number, because this remote object will not be referenced by the client using a Url instead of a machine and Port number i.e. using http://localhost//RemotingClass.soap

like so

HttpChannel httpChannel = new HttpChannel();
ChannelServices.RegisterChannel(httpChannel);
IRemotingClass remoteClass = (IRemotingClass)Activator.GetObject( typeof(IRemotingClass), http://localhost///RemotingClass.soap");
string result = remotingClass.HelloWorld();
Console.WriteLine("The Customer has been gotten on the client remotely with the age : " + result);

SoapSuds.exe

In the above examples we've used an interface on both Server and Client sides, this interface hid our true implementation from the Client, all the Client is aware of is the interface. This works fine but there is another way to implement this and that is with a representation of our remote class that is created from the WSDL of our remote object. The WSDL of our remote object can be accessed using the Url to our remote object followed by ?WSDL, (remember this in SOAP and webservices).
The SoapSuds.exe is an app found in the Visual Studio SDK e.g. C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\soapsuds.exe.
You pass soapsuds.exe your WSDL output and it generates a class representation of your remote object, this representation will be very similar to your real object on the Server side with additional SOAP attributes.
e.g.
C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0>soapsuds -url:http://localhost//RemotingClass.soap?wsdl -od:C:\temp

This class can now be used by the Client instead of the Interface IRemotingClass.

But what is the usefulness of this?
One use of this is that is provides the concrete class instead of the interface which is required when you are using Configurable Settings on your Server side.
Configurable settings allow you to dynamically change the remote object name and port at runtime using you config file, one issue with this however is that the Client side requires the concrete implentation in order to reference it.
e.g.
<configuration><
system.runtime.remoting><
application><
service><wellknown mode="Singleton" type="RemotingClass, RemotingClassAssemblyName"objectUri="RemotingClass.rem"/>
</service>
<channels>
<channel name="MyChannel" priority="100" ref="http"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>

No comments: