简体   繁体   中英

c# call async web service with return value

I need to use a 3rd party async web services.

One specific service should return a string. I'm calling it from a xamarin android app and I created the service access logic on a core portable project.

The web service work fine, I tested it on Soap UI and the return is valid (it has two string parameters one the request and a string return value).

This is how I created the service access on core portable library:

public static async Task<string> GetResult(string param2)
{
    XSoapClient client = new XSoapClient();
    var result = await GetResultAsync(client, PARAM_1, param2);
    return result;
}

private static Task<string> GetResultAsync(this XSoapClient @this,
        string param1, string param2)
{
    var tcs = new TaskCompletionSource<string>();
    EventHandler<MyServiceCompletedEventArgs> callback = null;

    callback = (sender, args) =>
    {
        @this.MyServiceCompleted -= callback;
        if (args.Cancelled) tcs.TrySetCanceled();
        else if (args.Error != null) tcs.TrySetException(args.Error);
        else tcs.TrySetResult(args.Result);
    };

    @this.MyServiceCompleted += callback;
    @this.MyServiceAsync(param1, param2);

    return tcs.Task;
}

And this how I call this service on a client - xamarin android app in this case:

button.Click += async delegate
        {
            string param2 = p2EditText.Text;
            var result = await ServiceAccessLayer.GetResult(param2);
            resultEditText.Text = result;
        };

This throws an exception on this part of the web service code:

[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
    private string EndMyService(System.IAsyncResult result) {
        Core.ServiceReference.MyServiceResponse retVal = ((Core.ServiceReference.XSoap)(this)).EndMyService(result);
        return retVal.Body.MyServiceResult; // <= this line because Body is null
    }

I don't understand why Body is null

EDIT: I also tried this way:

public static void GetResult(string param2)
{
    XSoapClient client = new XSoapClient();
    client.MyServiceAsync(PARAM_1, param2);
    client.MyServiceCompleted += Client_MyServiceCompleted;
}

private static void Client_MyServiceCompleted(object sender, MyServiceCompletedEventArgs e)
{
    // do something with e.Result
    var result = e.Result;
}

But I got the same error.

Got it

private Task<string> MakeRequest()
{
    XSoapClient client = new XSoapClient();

    Task<string> request = Task.Factory.FromAsync(
        (callback, state) => c.BeginMyService(PARAM_1, param2, callback, state),
        result => c.EndMyService(result),
        TaskCreationOptions.None);

    Task<string> resultTask = request.ContinueWith(response =>
        {
            return response.Result;
        });

    return resultTask;
}

public async Task<string> GetResponse()
{
    var response = await MakeRequest();
    return response;
}

And the call in android app:

button.Click += async delegate
    {
        string param2 = p2EditText.Text;
        var result = await ServiceAccessLayer.GetResponse(param2);
        resultEditText.Text = result;
    };

Is this the best practice?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM