Powered by Blogger.

Sunday, March 2, 2014

Type System.Threading.Tasks.Task1[System.String] cannot be serialized error in WCF



I faced this issue when trying to consume a WCF service in Asp.Net.

Before reading this you can also check out Steps to create wcf service and host wcf service as windows service in C#.Net.

The full error message that i got is below:

Type 'System.Threading.Tasks.Task`1[System.String]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute

Cause:
Exact cause I also do not know but is related to .Net framework version.

Solution:
If you are using svcutil to generate the proxy class from the WCF url, then we need to add one more attribute to it and the error will go.

You can follow this article on how to Generate WCF Proxy using SvcUtil.exe.

Here we are using like below:

svcutil /language:cs /out:generatedproxy.cs /config:app.config net.tcp://localhost/Design_Time_Addresses/SLicense/Service1/

Now we need to add the /syncOnly attribute at the end like below:

svcutil /language:cs /out:generatedproxy.cs /config:app.config net.tcp://localhost/Design_Time_Addresses/SLicense/Service1 /syncOnly

The will generate the generatedproxy.cs file. Use this .cs file, the error will not come.





0 comments

Post a Comment