异步/等待模式。如何将等待方法传递给另一个方法 - c#

在我的应用程序中,我需要在所有API请求之前调用一个方法。如果满足特定条件,则需要在该方法中执行一组语句。

为了概括这一点,我创建了一个类似这样的帮助器类。

public class CertificateValidator {
readonly IDependencyService _serviceLocator;
public CertificateValidator(IDependencyService serviceLocator) {
    _serviceLocator = serviceLocator;
}

public async Task <T> TryExecuteWithCertificateValidationAsync <T> (Task <T> operation) {
    var service = _serviceLocator.Get <IDeviceService> ();
    if (service.CertificateValidationRequired()) {
        // My Code.
    }
    T actualResult = await operation;
    return actualResult;
}

}

在我的视图模型中,我做了类似的事情。

 public CertificateValidator ValidateCertificate => new CertificateValidator(_serviceLocator);

var response = await ValidateCertificate
                            .TryExecuteWithCertificateValidationAsync(MyMethodAsync());

private async Task<RequestResult<Response>> MyMethodAsync()
{
     // Some code
}

但是当我这样实现时,执行流程是

首先将调用MyMethodAsync()。
当到达await方法时,它将执行
TryExecuteWithCertificateValidationAsync方法并在那里运行其余代码。
然后,当它到达T actualResult = await operation; return
actualResult;
时,控件返回MyMethodAsync()-等待语句。

我的疑问是,

我需要完全执行TryExecuteWithCertificateValidationAsync,然后再执行MyMethodAsync。

简而言之,正如我早先所说的,我需要在调用所有API调用之前执行一组代码。我如何使用异步等待来实现类似的目的。

参考方案

而不是通过Task传递函数:

public async Task<T> TryExecuteWithCertificateValidationAsync<T>(Func<Task<T>> operation)
{
    var service = _serviceLocator.Get<IDeviceService>();
    if (service.CertificateValidationRequired())
    {
        // My Code.
    }
    T actualResult = await operation();
    return actualResult;
}

var response = await ValidateCertificate
    .TryExecuteWithCertificateValidationAsync(MyMethodAsync);

根据评论更新

如果该方法需要参数,则类型必须作为Func的附加通用参数添加:

private async Task<RequestResult<Response>> MyMethodAsync(int i)
{
     // Some code
}

public async Task<T> TryExecuteWithCertificateValidationAsync<T>(Func<int, Task<T>> operation) // Add int as second generic argument
{
    T actualResult = await operation(1); // Can now be called with an integer
    return actualResult;
}

将谓词<T>转换为Func <T,bool> - c#

我有一个包含成员Predicate的类,希望在Linq表达式中使用该类:using System.Linq; class MyClass { public bool DoAllHaveSomeProperty() { return m_instrumentList.All(m_filterExpression); } private IEnumerable&…

当我所有的都是T时,如何返回Interface <T>的实例? - java

我有一个界面:public interface ILoginResult<T> { public T get(); } 我有一个LoginPage对象:public class LoginPage<T> { ... public ILoginResult<T> login(...) { ... } } 我也有一些登录页面对…

客户端反序列化为数组序列化字典<string,string>数据 - c#

我有一个字典,该字典使用C#中的JavaScriptSerializer进行了序列化。在客户端,我有:"{"dd049eda-e289-4ca2-8841-4908f94d5b65":"2","ab969ac2-320e-42e1-b759-038eb7f57178":"5�…

Maven的Shade插件产生的罐子之间有什么区别? - java

在我的Maven项目中,我尝试使用maven-shade-plugin在运行mvn package时生成一个超级jar。结果,我的目标目录中出现三个jar:original-hello-world-0.1.0-SNAPSHOT.jar hello-world-0.1.0-SNAPSHOT.jar hello-world-0.1.0-SNAPSHOT-shad…

根据激活的Maven配置文件更新战争名称 - java

在pom中,我有两个配置文件。测试1测试2现在,我希望根据激活的配置文件更改战争名称。预期结果激活test1配置文件后,战争名称应为prefix-test1.war。激活test1和test2时,战争名称应为prefix-test1-test2.war。如果没有激活任何配置文件,则战争名称应为prefix.war。我的POM文件....<?xml ve…