Deano
Deano

Reputation: 2895

System.Threading.Tasks.Task - cannot be serialized because it does not have a parameterless constructor.

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?

Upvotes: 1

Views: 1990

Answers (2)

Bellarmine Head
Bellarmine Head

Reputation: 3647

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.

Upvotes: 2

Related Questions