简体   繁体   中英

WCF asynchronous call not returning data

I have a problem regarding an asynchronous call of a WCF web service method. It refuses to return the requested object when called using the await operator. Calling its synchronous counterpart works just fine or using the Result property on the Task<> object it returns. It is being called from a WPF app's main thread that serves as a client. I didn't play around with the thread apartments, just left everything as it was generated.
I really don't understand, I've created a simple console test app, added the very same service reference and asynchronous calls work just fine. I'm using Visual Studio 2013.

    public async Task<GetComputerResponse> GetComputer(int computerId)
    {
        try
        {
            _client = new ServiceClient(_endpoint);
            _client.Open();

            GetComputerRequest request = new GetComputerRequest()
            {
                ComputerId = computerId
            };

            var result = await _client.GetComputerAsync(request); // the debugger seems to stop execution here, refuses to break at the very next line
            /* _client.GetComputer(request) or await _client.GetComputerAsync(request).Result work as expected */
            _client.Close();
            return result;
        }
        catch (Exception)
        {
            _client.Abort();
            return null;
        }
    }

I'm not receiving any kind of an error, it simply doesn't fetch the required data.
Thank you in advance.

Adding a call to ConfigureAwait and passing false as the continueOnCapturedContext value seems to have fixed the problem.
It has something to do with async code being called from the main, UI thread resulting in a deadlock. So says the quick research, at least. At the moment I don't have enough time to learn about it more, unfortunately.

var result = await _client.GetComputerAsync(request).ConfigureAwait(false);

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