WCF Clients
See Faults for error trapping FaultException/ TimeoutException and client.Abort() (don't use "using")
WCF ClientBase
Client proxies (via Visual Studio Service Reference, or svcutil, or manual) inherit ClientBase<IService>
- Duplex clients with callbacks inherit from DuplexClientBase<IService>
- For duplex clients, create an InstanceContext with your Callback class and pass it to the proxy (a DuplexClientBase)
ChannelFactory
When you have shared DLLs between client and service, you don't have to use svcutil/Service Reference to generate a proxy. Instead create the proxy dynamically using ChannelFactory<T>:
using (var factory = new ChannelFactory<IService>(
new NetTcpBinding(),
"net.tcp://localhost:13024/calculator/"))
{
var proxy = factory.CreateChannel();
var result = proxy.Add(1, 2);
}
Async Clients
Generate async code (in VS Service Reference/Advanced):
Async with AsyncCallback
using(var proxy = new CalculatorClient())
{
var cb = new AsyncCallback(
(asyncResult) =>
{
var client = (CalculatorClient)asyncResult.AsyncState ;
var result = client.EndAdd(asyncResult);
});
proxy.BeginAdd(1, 2, cb, null);
}
Async with *Completed event
using (var client = new Service1Client())
{
client.GetDataCompleted += (sender, e) =>
{
//GetDataCompletedEventArgs
var result = e.Result;
};
client.GetDataAsync(1);
client.Close();
}