1
votes

I am currently using the WCF Web API and it is working great with synchronous tasks. However, when I try and implement asynchronous operations using Task/Task like so:

    [WebGet(UriTemplate = "landmark")]
    Task<IEnumerable<Closest>> GetLandmarkAsync(float latitude, float longtitude)
    {
        return Task.Factory.StartNew(() => CalculateClosestThing(latitude,     longtitude));
    }

I am getting this error:

System.Threading.Tasks.Task`1[System.Collections.Generic.IEnumerable`1[ContentRepository.Data.Entities.Closest]] cannot be serialized because it does not have a parameterless constructor. 

Now, the first thing I did was check the Closest Class and add a parameterless constructor like so:

public class Closest
{
    public Closest() { }

    public double Distance { get; set; }
    public string Name { get; set; }
}

But I'm still getting the same error. I've tried everything and clearly there is a parameterless constructor on the class. Has anyone ever experienced this? Any ideas?

2
try returning a Closest[] instead of IEnumerable.Mark W
Do you really want to marshal a task over the wire? Of course it won't work. This would require marshaling the delegate as well, which is impossible.Aliostad
@MarkW I am still getting the same error even if I try that: System.Threading.Tasks.Task`1[ContentRepository.Data.Entities.Closest[]] cannot be serialized because it does not have a parameterless constructor.Deano
@Aliostad Hey, Im just following the example on the WCF codeplex documentation - wcf.codeplex.com/…. Mine is similar to their example...Deano
as mentioned above why return a task? Just async call the function (inside a task)Mark W

2 Answers

2
votes

Ensure you new up a WebApiConfiguration obj (derived from HttpConfiguration) and set it as the default Http configuration with a call to routes.SetDefaultHttpConfiguration() in the host (I'm using an MVC host).

I had the same problem as you before I switched from HttpConfiguration to WebApiConfiguration.

For more details see here.