Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Visual Studio - generated async WCF calls?

My OperationContract:

public List<MessageDTO> GetMessages()
        {
            List<MessageDTO> messages = new List<MessageDTO>();
            foreach (Message m in _context.Messages.ToList())
            {
                messages.Add(new MessageDTO()
                {
                    MessageID = m.MessageID,
                    Content = m.Content,
                    Date = m.Date,
                    HasAttachments = m.HasAttachments,
                    MailingListID = (int)m.MailingListID,
                    SenderID = (int)m.SenderID,
                    Subject = m.Subject
                });
            }
            return messages;
        }

In Service Reference configuration I checked the option "Generate asynchronous operations". How do I use the generated GetMessagesAsync()? In the net I found examples that use AsyncCallback, however I'm not familiar with that. Is there a way to use it in some friendly way like async and await keywords in .NET 4.5? If not, what should I do to invoke the method asynchronously?

like image 794
pmichna Avatar asked Dec 08 '12 13:12

pmichna


People also ask

How do you call async method in WCF?

To call a WCF service method asynchronously you need to create an AsyncCallback delegate and pass this delegate to the asynchronous proxy method. Note that the AsyncCallback delegate is executed on a worker thread and so this doesn't block the main thread of the application.

What is asynchronous messages in WCF?

In Windows Communication Foundation (WCF) applications, a service operation can be implemented asynchronously or synchronously without dictating to the client how to call it. For example, asynchronous service operations can be called synchronously, and synchronous service operations can be called asynchronously.

How use async await in C#?

The async keyword turns a method into an async method, which allows you to use the await keyword in its body. When the await keyword is applied, it suspends the calling method and yields control back to its caller until the awaited task is complete. await can only be used inside an async method.

How do you create asynchronous method in C#?

You can use await Task. Yield(); in an asynchronous method to force the method to complete asynchronously. Insert it at beginning of your method and it will then return immediately to the caller and complete the rest of the method on another thread.


1 Answers

If you select 'Generate asynchrounous operations', you will get the 'old' behavior where you have to use callbacks.

If you want to use the new async/await syntax, you will have to select 'Generate task-based operations' (which is selected by default).

When using the default Wcf template, this will generate the following proxy code:

  public System.Threading.Tasks.Task<string> GetDataAsync(int value) {
      return base.Channel.GetDataAsync(value);
  }

As you can see, there are no more callbacks. Instead a Task<T> is returned.

You can use this proxy in the following way:

public static async Task Foo()
{
    using (ServiceReference1.Service1Client client = new ServiceReference1.Service1Client())
    {
        Task<string> t = client.GetDataAsync(1);
        string result = await t;
    }
}

You should mark the calling method with async and then use await when calling your service method.

like image 158
Wouter de Kort Avatar answered Oct 04 '22 10:10

Wouter de Kort