WebReference.com - Part 1 of Chapter 6: Professional C# Web Services, from Wrox Press Ltd (5/8)
[previous] [next] |
Professional C# Web Services
Client
Creating the client is as simple as creating the server. Here we will create a client using a configuration file.
Client Configuration File
The client configuration file SimpleClient.exe.config uses the XML
<client> element to specify the URL of the server using
protocol://hostname:port/application. In this example we use tcp as the
protocol, and the server runs on localhost with the port number 9000. The
application name of the server is defined with the name attribute of the
<application> element in the server configuration file.
The <wellknown> element specifies the remote object we want to access.
As in the server configuration file, the type attribute defines the type of the remote object
and the assembly. The url attribute defines the path to the remote object. Appended to the
URL of the application is the endpoint name MyRemoteObject. The channel that is configured
with the client can again be found in the configuration file machine.config, but this time
it is the client channel:
<configuration>
<system.runtime.remoting>
<application name="SimpleClient">
<client url="tcp://localhost:9000/SimpleServer">
<wellknown
type="Wrox.Samples.MyRemoteObject, MyRemoteObject"
url =
"tcp://localhost:9000/SimpleServer/MyRemoteObject"
/>
</client>
<channels>
<channel ref="tcp client" />
</channels>
</application>
</system.runtime.remoting>
</configuration>
Client Application
As in the server, we can activate the client channel by calling
RemotingConfiguration.Configure(). Using configuration files we can simply use new
to create the remote object. Next we call the method Hello() of this object:
// SimpleClient.cs
using System;
using System.Runtime.Remoting;
namespace Wrox.Samples
{
class SimpleClient
{
static void Main(string[] args)
{
RemotingConfiguration.Configure("SimpleClient.exe.config");
MyRemoteObject obj = new MyRemoteObject();
Console.WriteLine(obj.Hello());
}
}
}
We can compile the client to a console application as we've done before with the server:
csc /target:exe /reference:MyRemoteObject.dll SimpleClient.cs
The assembly of the remote object must also be copied for the client application as well, but here it is clearer as we have to reference it explicitly in the compiler command.
[previous] [next] |
Created: February 13, 2002
Revised: February 13, 2002
URL: http://webreference.com/programming/csharp/webservices/chap6/1/5.html

Find a programming school near you