Async Config

Topics: Web Api
Oct 5, 2011 at 2:09 AM

Hi guys,

What's the "magic" that Glenn made in his async task sample that just returning Task<T> is enough to async works well? For example: if I run this service:

[ServiceContract]
public class TestService
{
    [WebGet(UriTemplate = "")]
    public Task<IEnumerable<User>> GetUsersAsync()
    {
        //....
    }
}

So, I receive this error when I open it on the browser:

System.Threading.Tasks.Task`1[System.Collections.Generic.IEnumerable`1[TestService.User]] cannot be serialized because it does not have a parameterless constructor.

But, if I put the TaskServiceAttribute and TaskOperationAttribute, all works well:

[TaskService]
[ServiceContract]
public class TestService
{
    [TaskOperation]
    [WebGet(UriTemplate = "")]
    public Task<IEnumerable<User>> GetUsersAsync()
    { 
        //....
    }
}
Coordinator
Oct 5, 2011 at 3:45 AM

The Task-based async support is part of the Web API Enhancements. To enable it you need to use WebApiConfiguration to configure your Web API. If you use the MapServiceRoute extension method, then WebApiConfiguration will get used by default and Task-based async should just work.

Take a look at the TaskAsync sample in the prototype branch to how to wire up a simple service that uses Task-based async.

Let me know if you run into any issues.

Daniel Roth

From: israelaece [email removed]
Sent: Tuesday, October 04, 2011 7:10 PM
To: Daniel Roth
Subject: Async Config [wcf:274787]

From: israelaece

Hi guys,

What's the "magic" that Glenn made in his async task sample that just returning Task<T> is enough to async works well? For example: if I run this service:

[ServiceContract]
public class TestService
{
[WebGet(UriTemplate = "")]
public Task<IEnumerable<User>> GetUsersAsync()
{
//....
}
}

So, I receive this error when I open it on the browser:

System.Threading.Tasks.Task`1[System.Collections.Generic.IEnumerable`1[TestService.User]] cannot be serialized because it does not have a parameterless constructor.

But, if I put the TaskServiceAttribute and TaskOperationAttribute, all works well:

[TaskService]
[ServiceContract]
public class TestService
{
    [TaskOperation]
    [WebGet(UriTemplate = "")]
    public Task<IEnumerable<User>> GetUsersAsync()
    { 
        //....
    }
}