Approach One: using proxy client)
http://msdn.microsoft.com/en-us/library/ms730059.aspx
event-driven asynchronous calling model
Service Side
[ServiceContract]
public interface IService
{
[OperationContract]
double Add(double a, double b);
}
public class Service : IService
{
public double Add(double a, double b)
{
return a + b;
}
}
C:\windows\system32>svcutil http://localhost:8000/mex /a /tcv:Version35
Client Side
static void Main(string[] args)
{
Console.WriteLine("Press <ENTER> to terminate client once the output is displayed.");
Console.WriteLine();
// Create a client
ServiceClient client = new ServiceClient();
// AddAsync
double value1 = 100.00D;
double value2 = 15.99D;
client.AddCompleted += new EventHandler<AddCompletedEventArgs>(AddCallback);
client.AddAsync(value1, value2);
Console.WriteLine("Add({0},{1})", value1, value2);
Console.ReadLine();
}
// Asynchronous callbacks for displaying results.
static void AddCallback(object sender, AddCompletedEventArgs e)
{
Console.WriteLine("Add Result: {0}", e.Result);
}
Approach Two: Using Channel Factory
http://msdn.microsoft.com/en-us/library/bb885132.aspx
Service Side
[ServiceContract]
public interface IService
{
[OperationContract]
double Add(double a, double b);
}
public class Service : IService
{
public double Add(double a, double b)
{
return a + b;
}
}
C:\windows\system32>svcutil http://localhost:8000/mex /a
Client Side
static void Main(string[] args)
{
ChannelFactory<IServiceChannel> factory = new ChannelFactory<IServiceChannel>("BasicHttpBinding_IService");
IServiceChannel channelClient = factory.CreateChannel();
// BeginAdd
double value1 = 100.00D;
double value2 = 15.99D;
IAsyncResult arAdd = channelClient.BeginAdd(value1, value2, AddCallback, channelClient);
Console.WriteLine("Add({0},{1})", value1, value2);
Console.ReadLine();
}
static void AddCallback(IAsyncResult ar)
{
double result = ((IServiceChannel)ar.AsyncState).EndAdd(ar);
Console.WriteLine("Add Result: {0}", result);
}
}
Approach Three: Add Service Reference
Client Code:
static void Main(string[] args)
{
ChannelFactory<IService> factory = new ChannelFactory<IService>("BasicHttpBinding_IService");
IService channelClient = factory.CreateChannel();
// BeginAdd
double value1 = 100.00D;
double value2 = 15.99D;
IAsyncResult arAdd = channelClient.BeginAdd(value1, value2, AddCallback, channelClient);
Console.WriteLine("Add({0},{1})", value1, value2);
Console.ReadLine();
}
static void AddCallback(IAsyncResult ar)
{
double result = ((IService)ar.AsyncState).EndAdd(ar);
Console.WriteLine("Add Result: {0}", result);
}
OR
static void Main(string[] args)
{
Console.WriteLine("Press <ENTER> to terminate client once the output is displayed.");
Console.WriteLine();
// Create a client
ServiceClient client = new ServiceClient();
// AddAsync
double value1 = 100.00D;
double value2 = 15.99D;
client.AddCompleted += new EventHandler<AddCompletedEventArgs>(AddCallback);
client.AddAsync(value1, value2);
Console.WriteLine("Add({0},{1})", value1, value2);
Console.ReadLine();
}
// Asynchronous callbacks for displaying results.
static void AddCallback(object sender, AddCompletedEventArgs e)
{
Console.WriteLine("Add Result: {0}", e.Result);
}