Asynchronous Operations in WCF


Request - response service operations in WCF cause the client to block while the service operation is executing. If a service takes five seconds to complete, the client application will freeze for the duration of the call waiting for the response.

You can use asynchronous programming pattern to enable a caller of any synchronous method to call it asynchronously. For example, while a web browser is downloading images in a web page, you can still scroll the page or navigate elsewhere.

Service Code

namespace WCFService
{
    [ServiceContract]
    public interface IUserService
    {
        [OperationContract]
        string Authenticate(string UserName, string A_CryptKey);

        [OperationContract]
        List<User> GetApprovedUser();
    }
}

namespace WCFService
{
    public class UserService : IUserService
    {
        public string Authenticate(string UserName, string A_CryptKey)
        {
            using (var Context = new UserServiceEntities())
            {
                var user = Context.Users.Where(c => c.UserName == UserName && c.A_CryptKey == A_CryptKey).FirstOrDefault();
                if (user != null)
                    return Convert.ToString(user.UserID) + "," + Convert.ToString(user.B_CryptKey);
                else
                    return string.Empty;
            }

        }

        public List<User> GetApprovedUser()
        {
            using (var Context = new UserServiceEntities())
            {
                List<User> User = Context.Users.Where(c => c.IsApproved == true).ToList();
                return User;
            }
        }
    }
}

Service configuration

<system.serviceModel>
    <
behaviors>
      <
serviceBehaviors>
        <
behavior>
          <!--
To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <
serviceMetadata httpGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <
serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </
serviceBehaviors>
    </
behaviors>
    <
serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

In client project solution explorer right click and select "Add Service reference". Now click on "Advanced" button in the Add Service reference dialog box and select "Generate Asynchronous Operations" check box. It will generate IAsyncResult class and two methods Begin<OperationName> and End<OperationName>.

First Begin<OperationName> is called by the client and then the client can continue executing code on its current thread while the asynchronous operation executes in a different thread. The client calls End<OperationName> for each call to Begin<OperationName> to get the result. A delegate is passed by the client to the Begin<OperationName>, which is called when the asynchronous operation is completed and can store state information from the Begin<OperationName> call.

WCF Client code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UserService;
public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        UserServiceClient proxy = new UserServiceClient();
        IAsyncResult arUser;
        arUser = proxy.BeginAuthenticate("Mukesh", "XY12@9", AuthenticateCallback, proxy);

        //arApproveUser = proxy.BeginGetApprovedUser(GetApprovedUserCallback, proxy);
    }

    // Asynchronous callbacks
    static void AuthenticateCallback(IAsyncResult ar)
    {
        string user = ((UserServiceClient)ar.AsyncState).EndAuthenticate(ar);
    }
    //static void GetApprovedUserCallback(IAsyncResult ar)
    //{
    //   List<UserService.User> user = ((UserServiceClient)ar.AsyncState).EndGetApprovedUser(ar);
    //}
}


Client side configuration

<system.serviceModel>
    <
bindings>
      <
basicHttpBinding>
        <
binding name="BasicHttpBinding_IUserService" closeTimeout="00:01:00"
          openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
          allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
          maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
          messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
          useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </
binding>
      </
basicHttpBinding>
    </
bindings>
    <
client>
      <
endpoint address="http://localhost:52949/UserService.svc" binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_IUserService" contract="UserService.IUserService"
        name="BasicHttpBinding_IUserService" />
    </client>
  </
system.serviceModel>
 

Up Next
    Ebook Download
    View all
    Learn
    View all