简体   繁体   中英

Why to Implement Asynchronous Service if Asynchronous Proxies on client does the exact same thing

Initially I have my Service Contract :

 [ServiceContract]
    interface IDataRetrieve
    {            
       [OperationContract]
       List<OrderDetails> GetOrderDetails(string FilterValue);
    }

And Implemented the Service as :

public List<OrderDetails> GetOrderDetails(string filterValue)
        {
            //My Operation
        }

Now After following the article from MSDN , I updated my Service Contract

 [ServiceContract]

interface IDataRetrieve
{
    [OperationContractAttribute(AsyncPattern = true)]
    IAsyncResult BeginGetOrderDetails(string filterValue, AsyncCallback callback, object asyncState);

    List<OrderDetails> EndGetOrderDetails(IAsyncResult result);
}

And the Implementation:

 public IAsyncResult BeginGetOrderDetails(string filterValue, AsyncCallback callback, object asyncState)
        {
            this.FilterValue = filterValue;
            var task = Task<List<OrderDetails>>.Factory.StartNew(this.WorkerFunction, asyncState);

            return task.ContinueWith(res => callback(task));
        }

public List<OrderDetails> EndGetOrderDetails(IAsyncResult result)
        {
            return ((Task<List<OrderDetails>>)result).Result;
        }
  public List<OrderDetails> WorkerFunction(object state)
        {
           //My Operation
        }

But While using both the implementation in ASP.NET observed the same result being returned in same fashion.

So , Why should I take the extra step to implement asynchronously while the same can be accomplished just by creating proxies in client side? I think using Task is not the only benefit here?

Am I missing something important !!

Over the past few years, our team has received a good deal of sound WCF assistance from Wenlong Dong's blog, so we generally value his thoughts. As such, I recommend the following article on WCF asynchronous programming.

http://blogs.msdn.com/b/wenlong/archive/2009/02/09/scale-wcf-application-better-with-asynchronous-programming.aspx

"For blocking calls such as I/O operations or remote calls, you should always thinking about using asynchronous patterns. In those cases, you would be able to reduce number of threads used in the system while waiting for operations to complete. Here are a few cases where you would like to use asynchronous patterns:

  1. A WCF service operation is waiting for a database operation to complete.
  2. A WCF service operation calls into another WCF client proxy of a backend WCF service.
  3. A UI thread waits for data from WCF client calls."

Regards,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM