Service Reference - Generate task-based operations

ElvinAsadovElvinAsadov AZMember ✭✭
edited October 2014 in Visual Studio

Hi.
I'd like to generate WCF Service client with task-based operations for a Portable project.
But for some reason the settings section for that is disabled and default selected option is "Generate asynchronous operations".
Is it a bug or some kind of limitation? How can I fix it?
Maybe there's another way to generate task-based operations?
Thanks.

Posts

  • ElvinAsadovElvinAsadov AZMember ✭✭

    Forgot to mention I use Visual Studio 2013 and the latest Xamarin v3.7.
    Don't know if it's intrinsic to prev versions.

  • appsdevappsdev USMember

    Hi,

    I had the same problem. Apparently it is not possible to generate Task-Based Operations. I suppose it is because of compatibility among the different platforms.

    I solved it by using "event-based" operations. That is, you have a webservice method (...Async), and an event (...Completed) which will be raised when the method has finished its execution. However, you can turn it to a Task.

    Instead of:

            public static async Task<MyObject[]> LoadData(...) {
                WSClient webservice = new WSClient();
                getDataResponse resp = await webservice.getDataAsync(...);
                return [email protected];
            }
    

    you can use:

            public static Task<MyObject[]> LoadData(...) {
                var tcs = new TaskCompletionSource<MyObject[]>();
                WSClient webservice = new WSClient();
                webservice.getDataAsync(...);
                webservice.getDataCompleted += (sender, e) =>{
                    if (e.Error != null) {
                        tcs.TrySetException(e.Error);
                    } else if (e.Cancelled) {
                        tcs.TrySetCanceled();
                    } else {
                        tcs.TrySetResult(e.Result);
                    }
                };
                return tcs.Task;
            }
    

    Don't hesitate to ask again if it is still not clear. I have been working on this issue during several hours :D

  • ElvinAsadovElvinAsadov AZMember ✭✭

    Thanks, it worked for me.

  • vickzzvickzz PKMember

    Im getting a System.NullReferenceException: Object reference not set to an instance of an object.

    I am using a old amsx based service

  • ragonzalezragonzalez USMember

    Same problem here. When the web service method returns a primitive type (bool, string...) it works. But when it is supposed to return a more complex type I get Object reference not set to an instance of an object.

    The exception trace:
    at App6.MyWebService.MyWebServiceSoapClient.EndLogin(IAsyncResult result)
    at App6.MyWebService.MyWebServiceSoapClient.OnEndLogin(IAsyncResult result)
    at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)

    The error is the same if I just subscribe to the method's Completed event without turning it to a Task.

    I'm using VisualStudio 2015 with Xamarin 4.1.2. I just added a new Service Reference (asmx service) to a PCL project.

    Any kind of help is much appreciated.

  • ragonzalezragonzalez USMember

    @ragonzalez said:
    Same problem here. When the web service method returns a primitive type (bool, string...) it works. But when it is supposed to return a more complex type I get Object reference not set to an instance of an object.

    The problem actually arises when the WS method accepts some parameters regardless of what it returns.

  • Any body has a sample of the code ASXM please post over here thanks.

Sign In or Register to comment.