简体   繁体   中英

Issues creating custom async method

I'm well aware there are built in async methods in C# now such as ToListAsync() etc. In the event that we would like to wrap async functionality around a library that does not provide it out of the box, it seems to be a little more difficult. However, perhaps I just don't understand async programming in C# enough.

This article on TAP makes it seem pretty simple

In Visual Studio 2012 and the .NET Framework 4.5, any method that is attributed with the async keyword (Async in Visual Basic) is considered an asynchronous method, and the C# and Visual Basic compilers perform the necessary transformations to implement the method asynchronously by using TAP. An asynchronous method should return either a System.Threading.Tasks.Task or a System.Threading.Tasks.Task object. In the case of the latter, the body of the function should return a TResult, and the compiler ensures that this result is made available through the resulting task object.

Using several examples I have come up with the following to try and make a heavy couchdb call async.

var allDocs = await _couchDbServices.Value.GetItemsForSearchAsync().Result.Rows;  


public async Task<dynamic> GetItemsForSearchAsync()
    {
        return await RunQueryAsync("Items", "GetItemsForSearch", "include_docs=true");
    }  


public Task<LoveSeat.ViewResult<object>> RunQueryAsync(string designDoc, string view, string query)
    {
        return Task.Run(() => RunQuery(designDoc, view, query));
    }  


public LoveSeat.ViewResult<object> RunQuery(string designDoc, string view, string query)
    {
        //SetDefaultDesignDoc was causing issues with async query requests.
        //Explicitly set the design doc in call to CouchDb.View -- DP
        //CouchDb.SetDefaultDesignDoc(designDoc);

        LoveSeat.ViewResult<object> result = CouchDb.View<object>(view + "?" + query, new ViewOptions(), designDoc); //empty view options required in new loveseat version --SB
        return result;
    }  

The line var allDocs = await.... fails silently as soon as I add .Results.Rows to the call. This is somewhat confusing because if I am awaiting the call I shouldn't be getting back the Task, it should be a dynamic by this point. Breaking into code shows that it indeed is a dynamic when the line gets called without the .Results.Rows appended to it, yet intellisense does not seem to pick this up.

How can I get this async call to properly return a dynamic?

I am not completely sure but I would assume calling the await in this situation, it's thinking it needs to await the .Rows call, instead of the actual task you want to await (GetItemsForSearchAsync()). What does this code do for you:

var allDocsDynamic = await _couchDbServices.Value.GetItemsForSearchAsync();
var allDocs = allDocsDynamic.Result.Rows

Also, when returning a Task, it's redundant to use 'return await' since you will be calling await on the task that is returned.

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