Task based asynchronous operation disabled in PCL Service Reference setting
Asked Answered
F

2

13

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?

Faradmeter answered 19/2, 2014 at 8:50 Comment(1)
I have exactly the same problem. I read somewhere that disabling targeting to wp8 in project properties might enable that option. But that did not work for me.Grimes
Q
2

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!

Quickel answered 25/6, 2016 at 6:45 Comment(0)
C
1

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.

Cuffs answered 22/6, 2014 at 6:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.