Following on from my previous
post, here's the client code;
using System;
using System.ServiceModel;
namespace Gudge.Samples {
[ServiceContract]
public interface ISimple {
[OperationContract]
string Ping ( string text );
}
public interface ISimpleChannel : ISimple, IProxyChannel { }
class Client {
static void Main(string[] args) {
ChannelFactory<ISimpleChannel> cf = new ChannelFactory<ISimpleChannel>("SimpleEndpointConfig");
ISimpleChannel sc = cf.CreateChannel();
Console.WriteLine("{0}", sc.Ping("Hello, World!"));
}
}
}I didn't use svcutil to generate a proxy, so I have the same ISimple interface as the server along with another interface, ISimpleChannel that inherits from the service contract (ISimple) and the IProxyChannel interface. If you use svcutil, it writes this code for you.
In Main() I create a channel factory for the ISimpleChannel interface, and, by passing a string to the constructor, tell the ChannelFactory class to get from config the various settings needed to communicate with the server. I then call CreateChannel to actually set up the channel to the server, then make the call to the service operation, Ping.
The config file for the client looks like this;
<!-- client.exe.config -->
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0" >
<system.serviceModel>
<bindings>
<basicProfileBinding>
<binding configurationName ="SimpleClientBinding" />
</basicProfileBinding>
</bindings>
<client>
<endpoint
configurationName="SimpleEndpointConfig"
address="http://localhost:8080/simple"
bindingSectionName="basicProfileBinding"
bindingConfiguration="SimpleClientBinding"
contractType="Gudge.Samples.ISimple, Client" />
</client>
</system.serviceModel>
</configuration>
Unlike the server config, the client config doesn't use the serviceType attribute, rather the endpoint element contains all the information we need in this case. The bindingSectionName and bindingConfiguration elements serve the same purpose here as they did in the server config; they reference the binding element. Again, as with the server, I'm not modifying the default behaviour of the basicProfileBinding so the binding element doesn't have any attributes beyond the configurationName attribute.
The configurationName attribute on the endpoint element is what the channel factory uses to match against the parameter passed into the ChannelFactory<ISimpleChannel> constructor.
The address attribute gives the address of the service, where messages will be sent.
Lastly the contractType gives the type of the service contract, followed by the assembly it can be found in, in this case, client.exe.
So, not very much code (or config) to get a simple, self-hosted server and manually written client up and running.
Posted
May 09 2005, 04:43 AM
by
martin-gudgin