Introduction
This article has been excerpted from the book "The Complete Visual C# Programmer's Guide" by the Authors of C# Corner.
Let's see another aspect of .NET Remoting. This will put you in mind the COM+ style of remote object usage. We will develop an example using a shared interface. The interfaces also afford us a good means for using black-box objects. We have used this feature with type library (TLB) files with COM+ objects. To develop against a type library is sufficient to use services on a COM server. The client calls GetObject on an endpoint without knowing what the exact object type is; all it knows is that the object implements an interface. This illustrates how we can build a client that does not reference a server object at compile-time. The sample code for this example consists of four parts: a shared interface, the remote object that implements this interface, a server, and a client that will use this interface method via an unknown object. Listing 23.17 displays the code for the shared interface.
Listing 23.17. SOAP Shared Interface3 (Listing23.17.cs)
// Compile with:
// csc /t:library /out:share3.dll Listing23.17.cs
using System;
namespace ExampleRemoting
{
public interface IDateTime
{
string DateTimeMethod(string name);
}
}
Listing 23.18 contains the code for the object that implements the shared interface.
Listing 23.18. SOAP Object That Implements Share 3 (Listing23.18.cs)
// Compile with:
// csc /t:library /r:share3.dll /out:object3.dll Listing23.18.cs
using System;
namespace ExampleRemoting
{
public class DateTimeServer : MarshalByRefObject, IDateTime
{
public DateTimeServer()
{
Console.WriteLine("DateTime server activated");
}
~DateTimeServer()
{
Console.WriteLine("DateTime server Object Destroyed");
}
public string DateTimeMethod(string name)
{
string strMessage = "Hi " + name + ". Here is the current DateTime: " + DateTime.Now;
Console.WriteLine(strMessage);
return strMessage;
}
}
}
The code for the server that delivers the remote methods appears in Listing 23.19.
Listing 23.19. SOAP Server3 (Listing23.19.cs)
// Compile with:
// csc /r:share3.dll /r:object3.dll Listing23.19.cs
/* Sample output:
Press <enter> to exit
DateTime server activated
DateTime server activated
Hi Bozo the clown. Here is the current DateTime: 8/19/2002 4:32:12 AM
DateTime server Object Destroyed
DateTime server Object Destroyed
*/
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
namespace ExampleRemoting
{
public class Example
{
public static void Main()
{
HttpChannel channel = new HttpChannel(8888);
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType(
Type.GetType("ExampleRemoting.DateTimeServer, Object3"), "SayDateTime",
WellKnownObjectMode.SingleCall);
Console.WriteLine("Press <enter> to exit.");
Console.ReadLine();
}
}
}
Listing 23.20 shows the code for the client that connects to the server and uses the methods exposed.
Listing 23.20. SOAP Client3 (Listing23.20.cs)
/* After executing the Listing23.19.exe, use the SOAPSUDS tool to create
object3.dll proxy:
soapsuds -url:http://127.0.0.1:8888/SayDateTime?WSDL -oa:object33.dll
*/
// Compile with:
// csc /r:object33.dll Listing23.20.cs
/* Sample output:
Hi Bozo the clown. Here is the current DateTime: 8/19/2002 4:32:12 AM
*/
// Client3
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
namespace ExampleRemoting
{
public class Client
{
public static void Main()
{
HttpChannel channel = new HttpChannel();
ChannelServices.RegisterChannel(channel);
// Create an object of type interface
IDateTime obj = (IDateTime)Activator.GetObject(typeof(ExampleRemoting.IDateTime), "http://127.0.0.1:8888/SayDateTime");
if (obj == null)
Console.WriteLine("Could not locate server!");
else
Console.WriteLine(obj.DateTimeMethod("Bozo the clown"));
}
}
}
Conclusion
I hope this article has helped you in understanding Common Interfaces using C#. See other articles on the website on .NET and C#.
The Complete Visual C# Programmer's Guide covers most of the major components of C# and the .NET environment. It is geared toward the intermediate programmer but contains enough material to satisfy the advanced developer.