Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Task-based client-side WCF service calls .NET 4.0

In Visual Studio 2012 there is a handy "Generate task-based operations" option that you can use to generate Task and Task<T> versions of WCF service calls.

I am using the Microsoft.Bcl.Async NuGet package to take advantage of async/await in my .NET 4.0 project. However, I seem to be unable to generate the Task-based operations using the Configuration Service Reference wizard in VS 2012 (the option is disabled).

As far as I can tell this only works if the project targets .NET 4.5. Does anyone know of a way to generate the Task-based operations with a .NET 4.0 client?

like image 861
cordialgerm Avatar asked Jun 26 '13 02:06

cordialgerm


1 Answers

You can use this workaround to create async task-based wcf client in .Net 4.0

  • Open NuGet and add the "Microsoft.Bcl.Async" package to your .Net 4.0 project
  • Create a new solution with a new .Net 4.5 project with the same name that your .Net 4.0 project
  • Add a service reference (to the desired web service) with the same name that the service reference created in the .Net 4.0 project
  • Be sure that the "Generate task-based operations" option under "Allow Generation of asynchronous operations" is checked in the advanced options dialog
  • Open the service reference folder of your .Net 4.5 project ("NET_4.5_PROJECT_FOLDER\Service References\YOUR__SERVICE_REFERENCE_FOLDER\")
  • Copy all the files from folder
  • Open the service reference folder of your .Net 4.0 project ("NET_4.0_PROJECT_FOLDER\Service References\YOUR__SERVICE_REFERENCE_FOLDER\")
  • Paste the files (to the .Net 4.0 service reference folder) that you have copied before (from the .Net 4.5 service reference folder)

In conclusion, you have to generate the service reference in .Net 4.5 and copy it to your .Net 4.0 project. It is mandatory to add the "Microsoft.Bcl.Async" package to your .Net 4.0 project.

EDIT

I have discovered that this workaround only works if .Net Framework 4.5 is installed on the computer. If you run the program in a computer with .Net Framework 4.0 installed it doesn't work (Windows XP is not supported in .Net Framework 4.5, so it is a good environment to test the program). This exception is thrown when .Net Framework 4.0 is not installed:

Type 'System.Threading.Tasks.Task`1[System.Boolean]' 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. See the Microsoft .NET Framework documentation for other supported types.

Summing up, it is not a good workaround.

like image 91
Luis Sagasta Avatar answered Oct 22 '22 09:10

Luis Sagasta