简体   繁体   English

创建自定义异步方法时出现问题

[英]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. 我很清楚现在在C#中内置了async方法,例如ToListAsync()等。如果我们想将异步功能包装在一个没有提供即用即用的库中,这似乎是一个有点困难。 However, perhaps I just don't understand async programming in C# enough. 但是,也许我只是不太了解C#中的异步编程。

This article on TAP makes it seem pretty simple TAP上的这篇文章看起来很简单

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. 在Visual Studio 2012和.NET Framework 4.5中,任何带有async关键字(在Visual Basic中为Async)属性的方法都被视为异步方法,并且C#和Visual Basic编译器执行必要的转换,以通过使用以下方式异步实现该方法TAP。 An asynchronous method should return either a System.Threading.Tasks.Task or a System.Threading.Tasks.Task object. 异步方法应返回System.Threading.Tasks.Task或System.Threading.Tasks.Task对象。 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. 在后者的情况下,函数的主体应返回TResult,并且编译器确保可通过结果任务对象使该结果可用。

Using several examples I have come up with the following to try and make a heavy couchdb call async. 通过使用几个示例,我提出了以下示例,以尝试使沉重的Sofadb调用异步。

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. 一旦将.Results.Rows添加到调用中,行var allDocs = await....就会自动失败。 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. 深入研究代码表明,在没有附加.Results.Rows情况下调用该行时,它确实是动态的,但是intellisense似乎没有意识到这一点。

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()). 我不太确定,但是我假设在这种情况下调用await,它认为它需要等待.Rows调用,而不是要等待的实际任务(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. 同样,在返回任务时,使用“返回等待”是多余的,因为您将对返回的任务调用等待。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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