Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6858927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:13:14+00:00 2026-05-27T02:13:14+00:00

I am trying to make a WCF REST method entirely asynchronous (I don’t want

  • 0

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
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:13:14+00:00Added an answer on May 27, 2026 at 2:13 am

    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());
    }
    
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make my WCF service method to return JSON-object, but it
I'm currently trying to find good way to make calls to WCF services in
This is on .Net 4, full framework. I'm trying to make a simple winforms
So I'm trying to create a C# WCF REST service that is called by
I am trying to use dependency injection with WCF REST (WebGet) and am having
I am trying to make a post request to my restful WCF service. The
I'm trying to make a call to a remote WCF service from within an
I'm trying to make a POST or PUT request to a WCF Service from
I have 2 WCF services now and I want to construct the WCF Service
I've been trying to get WCF security working for my project, and have had

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.