Create WCF service calls on the client side of .NET 4.0

Visual Studio 2012 has a convenient option "Create tasks from tasks", which you can use to create Task and Task<T> versions of WCF service calls.

I use the Microsoft.Bcl.Async NuGet package to use async / await in my .NET 4.0 project. However, I don't seem to be able to generate task-based operations using the configuration service help wizard in VS 2012 (the option is disabled).

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

+6
source share
1 answer

This workaround can be used to create task-based wcf async client in .NET 4.0

  • Open NuGet and add Microsoft.Bcl.Async package to .Net 4.0 project
  • Create a new solution with a new .Net 4.5 project with the same name as your .Net 4.0 project
  • Add the service link (to the desired web service) with the same name as the service link created in the .Net 4.0 project
  • Make sure that the option "Create tasks based on tasks" in the "Allow creation of asynchronous operations" section is checked in the advanced settings dialog
  • Open the help folder for your .Net 4.5 project service ("NET_4.5_PROJECT_FOLDER \ List of Services \ YOUR__SERVICE_REFERENCE_FOLDER \")
  • Copy all files from the folder
  • Open the help folder for your .Net 4.0 project service ("NET_4.0_PROJECT_FOLDER \ List of Services \ YOUR__SERVICE_REFERENCE_FOLDER \")
  • Paste the files (in the .Net 4.0 service help folder) that you copied earlier (from the .Net 4.5 service help folder)

In conclusion, you must create a link to the service in .Net 4.5 and copy it into the .Net 4.0 project. Be sure to add the Microsoft.Bcl.Async package to your .Net 4.0 project.

EDIT

I found that this workaround only works when installing the .NET Framework 4.5 on the computer. If you run the program on a computer with the .Net Framework 4.0 installed, it does not work (Windows XP is not supported in the .Net Framework 4.5, so this is a good environment for testing the program). This exception occurs when the .NET Framework 4.0 is not installed:

The type 'System.Threading.Tasks.Task`1 [System.Boolean]' cannot be serialized. Consider labeling it with the DataContractAttribute attribute and labeling all of its elements that you want to serialize with the DataMemberAttribute attribute. If the type is a collection, consider labeling it with CollectionDataContractAttribute. See the Microsoft.NET Framework documentation for other supported types.

To summarize, this is a bad workaround .

+2
source

Source: https://habr.com/ru/post/948106/


All Articles