Problem with Synchronous Calls: Request/Response Message Exchange Pattern
1. Client is blocked; this is a problem if
a) Client could do other work
b) Server logic takes a long time to complete
2. Services usually aren’t designed to efficiently handle concurrent incoming requests
a) Service could be bombarded and overwhelmed with request
b) Messages are processed in a “first-come, first-served” style
Asynchronous Call: Addresses problem of client being blocked
Setting to make call Asynchoronus:
You need to take 2 step to call service Asynchoronusly
1. Add async = true in page properties.
2. While taking reference of service go and click on Advanced tab at left bottom and checked check box “Generate asynchronous operations” as shown in below screenshot:
Service Code:
namespace WCFServiceAsyncCall
{
using System.ServiceModel;
///<summary>
/// Summary description for NewService
///</summary>
[ServiceBehavior()]
publicclassNewService : INewService
{
#region INewService Members
privateint p;
publicint getBalance(int endPoint)
{
for (int i = 0; i < endPoint; i++)
{
p = p + i;
}
return p;
}
#endregion
}
[ServiceContract()]
publicinterfaceINewService
{
[OperationContract]
int getBalance(int startingpoint);
}
}
Client Console App Code:
using System;
namespace ConsoleApplication1
{
classProgram
{
staticvoid Main(string[] args)
{
ServiceBalance.NewServiceClient client = new ServiceBalance.NewServiceClient();
Console.WriteLine("Start");
client.getBalanceCompleted += newEventHandler<ServiceBalance.getBalanceCompletedEventArgs>(client_getBalanceCompleted);
client.getBalanceAsync(5);
Console.WriteLine("End");
Console.Read();
}
staticvoid client_getBalanceCompleted(object sender, ServiceBalance.getBalanceCompletedEventArgs e)
{
Console.WriteLine("Service Method called.");
}
}
}