Tip 5: Building the Client Piece
Now that you've established an interface and a server-side business object, you need to write code to access this back-end object.
As stated earlier, the client piece has neither the code nor the reference to the customer business object. All you have is the interface; as you're about to find out, that (and a port number and address) is all you need.
The following code snippet demonstrates how to access the external class.
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using SimpleInterfaces;
ICustomer oRemoteCustomer;
Type tCustomer = typeof(ICustomer);
ChannelServices.RegisterChannel( new
TcpClientChannel());
oRemoteCustomer =
(ICustomer)Activator.GetObject( tCustomer,
"tcp://localhost:8228/CustomerBzObject");
DataSet DsTemp = oRemoteCustomer.GetCustomer(123);
The code does the following:
- Adds a reference to the System.Runtime.Remoting namespace (not shown)
- Includes several .NET remoting namespaces, as well as the interface class
- Defines an object reference to the ICustomer interface. You'll eventually use the object reference to access the back-end method GetCustomer, as if you had direct access to it.
- Defines a type reference to ICustomer (the server-side needs this to match up on object types)
- Opens a TCP channel (for purposes of demonstration, you're hard-coding port 8228 and the TCP address)
- Activates the remote object and casts the return value to ICustomer. At this point, oRemoteCustomer can access any properties and methods defined in ICustomer.
 | |
| Figure 1: Client-side remoting uses a strongly-typed Interface. |
Perhaps most important of all, you did not use reflection to accomplish any of this. You were able to build a strongly-typed solution.
Figure 1 demonstrates that at design-time, all methods that ICustomer implements are shown in IntelliSense. Challenge yourself and your colleagues to use strongly typed elements as much as possible. Design-time discovery and type-safe checking are valuable, especially in a multi-developer environment.
Tip 6: Building the Remoting Server Listener
There's just one remaining piece: on the domain that contains the method code for the business object, you need to build a listener. The listener checks the specified port in the client code and registers the business object for remote access:
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
TcpServerChannel Tcps;
int nTCPPort = 8228;
Tcps = new TcpServerChannel(nTCPPort);
ChannelServices.RegisterChannel(Tcps);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(SimpleCustomerBzObject.CustomerBzObject),
"CustomerBzObject", WellKnownObjectMode.Singleton);
Although the code appears a bit esoteric, the method names are self-explanatory. The domain that contains the business object registers a TCP port channel and CustomerBzObject. As you look back to the client-side code, you can see how the object type references must match.