0
votes

I developed my code based on this posting : Asynchronous Programming Model in WCF with async/await

There was no compilation error but when hosted it generates below specified error :

Type 'System.Threading.Tasks.Task`1[System.String]' cannot be serialized. Consid er marking it with the DataContractAttribute attribute, and marking all of its m embers you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.

What could be the reason for this.

1
Does any of your data contract have the new Task\Task<K> class as datamember? Async tasks are new in WCF, and available through the Async CTP release for WCF. Look at sample provided as part of CTP to understand how it is working. But going by the error the WCF framework is trying to serialize a Task class which is not meant to be serialize. - Chandermani
Hi, Thank you for your reply. I would like to attach the code segment which would be easy to understand. I do not return a datacontract oject just returning the Task<string>. method --------- public async Task<string> HelloAsync(string name) { return await Task.Factory.StartNew(() => "hello " + name); } --------- - Desmond

1 Answers

1
votes

It looks like one of your methods is either returning a Task object, or has a Task object as one of its parameters.

Tasks cannot be passed across the web-service boundary - this is the cause of your problem.

Update: I had a look at the link you got this code from, and it looks like the code provided is conceptual only. If you look closely at the wording, he says 'will' instead of 'can'.

WCF vNext will adopt the Async model in both the client and the server side, and provide new Task-based overloads for some of the most used asynchronous APIs.

Since a Task cannot be passed over the web-service boundary, it will not work.