Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Task based asynchronous operation disabled in PCL Service Reference setting

I'm currently building a Xamarin based mobile application. For that project, I have created a PCL project with framework 4.5. I'm using VS 2013 as the development IDE. Now I want add a WCF service reference to this PCL. While adding service reference to this PCL project, I noticed that generation of asynchronous operation is disabled. Please check the image for more detail.

enter image description here

I added the BCL.Async package via Nuget to the project. But still I can't access the Task based operation from the radiobutton list (its disabled).

So is there any way to generate task based asynchronous operation in service client?

like image 938
Dennis Jose Avatar asked Feb 19 '14 08:02

Dennis Jose


2 Answers

Hate to break it to you but you cannot generate Task based WCF client in Xamarin. The reason is Xamarin or Mono implements the Silverlight set which is a limited WCF implementation. As such you need to use SLSVCUTIL.exe instead(Adding a service reference in Xamarin would use this tool). The silverlight WCF client generated by SLSVCUTIL will be async based only.

All is not lost! You can easily wrap the silverlight async client into a task based client using the Task.FromAsync method.

A sample taken from the Xamarin website:

public async Task<List<TodoItem>> RefreshDataAsync ()
{
  ...
  var todoItems = await Task.Factory.FromAsync <ObservableCollection<TodoWCFService.TodoItem>> (
    todoService.BeginGetTodoItems,
    todoService.EndGetTodoItems,
    null,
    TaskCreationOptions.None);

  foreach (var item in todoItems) {
    Items.Add (FromWCFServiceTodoItem (item));
  }
  ...
}

https://developer.xamarin.com/guides/xamarin-forms/web-services/consuming/wcf/

Now if someone can figure out how to catch an Fault Exception when wrapping in Tasks that would be awesome!

like image 136
Cyberpass Avatar answered Nov 17 '22 12:11

Cyberpass


I've not used Xamarin before, but I'll assume APM and maybe Tasks are actually supported in it and this is just a Visual Studio limitation. Try using wsdl.exe manually to generate code. This is the tool Visual Studio calls when you add a service reference.

You'll need to pass either newAsync (Tasks) or oldAsync (APM) through the /parameters switch.

like image 1
Cory Nelson Avatar answered Nov 17 '22 13:11

Cory Nelson