client.ExecuteAsync<T> 委托不接受 1 个参数

client.ExecuteAsync<T> Delegate does not take 1 arguments

我正在尝试将我的 Restsharp 客户端更改为工作 async 而不是 sync。 我的每个 API-调用都引用 GetAsync<T> 方法。当我现在尝试将客户端更改为调用 ExecuteAsync<T> 而不是 Execute 时,我收到此错误:

Delegate 'Action, RestRequestAsyncHandle>' does not take 1 Arguments

我目前使用的是 RestSharp 版本 106.6.10。

这是我的 GetAsyncMethod:

public async Task<T> GetAsync<T>(string url, Dictionary<string, object> keyValuePairs = null)
    {
        try
        {
            // Check token is expired
            DateTime expires = DateTime.Parse(Account.Properties[".expires"]);
            if (expires < DateTime.Now)
            {
                // Get new Token 
                await GetRefreshTokenAsync();
            }

            // Get AccessToken
            string token = Account.Properties["access_token"];

            if (string.IsNullOrEmpty(token))
                throw new NullReferenceException("AccessToken is null or empty!");

            // Create client
            var client = new RestClient()
            {
                Timeout = 3000000
            };

            //Create Request
            var request = new RestRequest(url, Method.GET);
            request.RequestFormat = DataFormat.Json;
            request.AddHeader("Authorization", "Bearer " + token);

            // Add Parameter when necessary 
            if (keyValuePairs != null)
            {
                foreach (var pair in keyValuePairs)
                {
                    request.AddParameter(pair.Key, pair.Value);
                }
            }
            // Call
            var result = default(T);

            var asyncHandle = client.ExecuteAsync<T>(request, restResponse =>
            {
                // check respone 
                if (restResponse.ResponseStatus == ResponseStatus.Completed)
                {
                    result = restResponse.Data;
                }

                //else
                //    throw new Exception("Call stopped with Status: " + response.StatusCode +
                //                        " Description: " + response.StatusDescription);
            });

            return result;
        }
        catch (Exception ex)
        {
            Crashes.TrackError(ex);
            return default(T);
        }
    }

这里调用方法之一:

public async Task<List<UcAudit>> GetAuditByHierarchyID(int hierarchyID)
    {
        string url = AuthSettings.ApiUrl + "/ApiMethod/" + hierarchyID;

        List<UcAudit> auditList = await GetAsync<List<UcAudit>>(url);
        return auditList;
    }

当我在我的 类 之一中更改 ExecuteAsync<T> 中的 T 时,错误消失了。我如何更改方法以使用 async<T>???

根据 Lasse Vågsæther Karlsen 提供的信息,我找到了解决方案。

这是开始:

var asyncHandle = client.ExecuteAsync<T>(request, restResponse =>
        {
            // check respone 
            if (restResponse.ResponseStatus == ResponseStatus.Completed)
            {
                result = restResponse.Data;
            }

            //else
            //    throw new Exception("Call stopped with Status: " + response.StatusCode +
            //                        " Description: " + response.StatusDescription);
        });

为我工作:

client.ExecuteAsync<T>(request, (response, asyncHandle )=>
            {
                //check respone
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    result = response.Data;
                }
                else
                    throw new Exception("Call stopped with Status: " + response.StatusCode +
                                        " Description: " + response.StatusDescription);
            });

谢谢!