Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a WCF REST method entirely asynchronous with the Task Parallel Library?

Tags:

I am trying to make a WCF REST method entirely asynchronous (I don't want to block anywhere). Essentially I have a simple service with 3 layers: Service, Business Logic and Data Access Layer. The Data Access Layer is accessing a database and it can take several second to get a response back from that method.

I don't understand very well how to chaining of all those method work. Can someone please help me to complete the sample I am trying to write below? I don't understand well the pattern used by WCF and I didn't find much documentation on the subject.

Can someone help me to complete the following example? In addition, how can I measure that the service will be able to handle more load than a typical synchronous implementation?

using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Threading.Tasks;

namespace WcfRestService1
{
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = 
        AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class Service1
    {
        private BusinessLogic bll = new BusinessLogic();

        // Synchronous version
        [WebGet(UriTemplate = "/sync")]
        public string GetSamples()
        {
            return bll.ComputeData();
        }

        // Asynchronous version - Begin
        [WebGet(UriTemplate = "/async")]
        [OperationContract(AsyncPattern = true)]
        public IAsyncResult BeginGetSampleAsync(AsyncCallback callback, 
            object state)
        {
            Task<string> t = bll.ComputeDataAsync();

            // What am I suppose to return here
            // return t.AsyncState; ???
        }

        // Asynchronous version - End
        public List<SampleItem> EndGetSampleAsync(IAsyncResult result)
        {
            // How do I handle the callback here?
        }
    }

    public class BusinessLogic
    {
        public Task<string> ComputeDataAsync()
        {
            DataAccessLayer dal = new DataAccessLayer();
            return dal.GetData();
        }

        public string ComputeData()
        {
            Task<string> t = this.ComputeDataAsync();

            // I am blocking... Waiting for the data
            t.Wait();

            return t.Result;
        }
    }

    public class DataAccessLayer
    {
        public Task<string> GetData()
        {
            // Read data from disk or network or db
        }
    }
}
like image 564
Martin Avatar asked Nov 07 '11 17:11

Martin


2 Answers

Here's an example. I got it working with help from the following posts:

Edit: Added an example of an async client

Implement Classic Async Pattern using TPL
http://pfelix.wordpress.com/2008/06/27/wcf-and-the-asyncpattern-property-part-1/ http://pfelix.wordpress.com/2008/06/28/wcf-and-the-asyncpattern-part-2/

Here's a little do-nothing service:


namespace WcfAsyncTest
{
    [ServiceContract]
    public interface IAsyncTest
    {
        [OperationContract(AsyncPattern=true)]
        IAsyncResult BeginOperation(AsyncCallback callback, object state);

        string EndOperation(IAsyncResult ar);
    }

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class Service1 : IAsyncTest
    {
        public IAsyncResult BeginOperation(AsyncCallback callback, object state)
        {
            Task result = Task.Factory.StartNew((x) =>
                {
                    // spin to simulate some work
                    var stop = DateTime.Now.AddSeconds(10);
                    while (DateTime.Now < stop)
                        Thread.Sleep(100);
                }, state);
            if (callback != null)
                result.ContinueWith(t => callback(t));
            return result;
        }

        public string EndOperation(IAsyncResult ar)
        {
            ar.AsyncWaitHandle.WaitOne();
            return "Hello!!";
        }
    }
}


And here's the client (command line):


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestClient
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = new ServiceReference1.AsyncTestClient();
            var result = client.Operation();
            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
}

if you put trace points on the service, you can see that WCF really is calling EndOperation for you.

Async Client Example

First, you will need to generate an async proxy. You can do that by right-clicking on the Service Reference (in the References Folder of your project), and choosing "Configure Service Reference". Check the "Generate Asynchronous Operations" checkbox.

Now your client proxy will have some new members that weren't there before. Here's how to use them:


// this is in the command-line test client
// no changes to your service required.
static void AsyncTest()
{
  var client = new ServiceReference1.AsyncTestClient();
  client.OperationCompleted += new EventHandler(client_OperationCompleted);
  client.OperationAsync();
  Console.WriteLine("Operation Running");
}

static void client_OperationCompleted(object sender, ServiceReference1.OperationCompletedEventArgs e)
{
  if (e.Error == null)
    Console.WriteLine("Operation Complete.  Result: " + e.Result);
  else
    Console.WriteLine(e.Error.ToString());
}


like image 90
JMarsch Avatar answered Jan 19 '23 01:01

JMarsch


here is an implementation of a service that implements Async. In this the callback of wcf is passed all the way to the ado.net's sql command. When the command returns, it would invoke the service's EndXXX method, which would invoke Business layer, which would finally invoke EndXXX of SqlCommand. Let me know if you face any issues

public class Service
    {
        private BusinessLogic businessLayer = new BusinessLogic();
        public IAsyncResult BeginAnyOperation(AsyncCallback callback, object userState)
        {
            return businessLayer.BeginComputeData(callback, userState);
        }
        public string EndAnyOperation(IAsyncResult result)
        {
            return businessLayer.EndComputeDate(result);
        }
    }

    public class MyState<T> : IAsyncResult
    {
        public MyState() { }
        public object AsyncState { get; set; }
        public WaitHandle AsyncWaitHandle { get; set; }
        public bool CompletedSynchronously
        {
            get { return true; }
        }
        public bool IsCompleted { get; set; }
        public AsyncCallback AsyncCallback { get; set; }
        public T Result { get; set; }
        public IAsyncResult InnerResult { get; set; }
    }

    public class BusinessLogic
    {
        private DataAccessLayer dal = new DataAccessLayer();
        public IAsyncResult BeginComputeData(AsyncCallback callback, object state)
        {
            return dal.BeginGetData(callback, state);
        }
        public string EndComputeDate(IAsyncResult asyncResult)
        {
            return dal.EndGetData(asyncResult);
        }
    }

    public class DataAccessLayer
    {
        public IAsyncResult BeginGetData(AsyncCallback callback, object state)
        {
            var conn = new SqlConnection("");
            conn.Open();
            SqlCommand cmd = new SqlCommand("myProc", conn);
            var commandResult = cmd.BeginExecuteReader(callback, state, System.Data.CommandBehavior.CloseConnection);
            return new MyState<string> { AsyncState = cmd, InnerResult = commandResult };
        }
        public string EndGetData(IAsyncResult result)
        {
            var state = (MyState<string>)result;
            var command = (SqlCommand)state.AsyncState;
            var reader = command.EndExecuteReader(state.InnerResult);
            if (reader.Read())
                return reader.GetString(0);
            return string.Empty;
        }
    }
like image 41
np-hard Avatar answered Jan 19 '23 00:01

np-hard