This article has been excerpted from book "The Complete Visual C# Programmer's Guide" from the Authors of C# Corner.
Let's see another aspect of .NET Remoting. This will put you in mind of 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);
System.Console.WriteLine("press <enter> to exit.");
System.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"));
}
}
}
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 theclown"));
}
}
}
Conclusion
Hope this article would have 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 that make up C# and the .net environment. The book is geared toward the intermediate programmer, but contains enough material to satisfy the advanced developer. |