首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Windows phone7上使用RestSharp实现ExecuteAsync?

如何在Windows phone7上使用RestSharp实现ExecuteAsync?
EN

Stack Overflow用户
提问于 2012-04-14 20:48:36
回答 5查看 66.1K关注 0票数 35

我正在尝试使用RestSharp GitHub wiki上的文档来实现对REST API服务的调用,但我在使用ExecuteAsync方法时遇到了特别的问题。

目前,我的API类代码如下所示:

代码语言:javascript
复制
public class HarooApi
{
    const string BaseUrl = "https://domain.here";

    readonly string _accountSid;
    readonly string _secretKey;

    public HarooApi(string accountSid, string secretKey)
    {
        _accountSid = accountSid;
        _secretKey = secretKey;
    }

    public T Execute<T>(RestRequest request) where T : new()
    {
        var client = new RestClient();
        client.BaseUrl = BaseUrl;
        client.Authenticator = new HttpBasicAuthenticator(_accountSid, _secretKey);
        request.AddParameter("AccountSid", _accountSid, ParameterType.UrlSegment);
        client.ExecuteAsync<T>(request, (response) =>
        {
            return response.Data;
        });
    }
}

我知道这与GitHub页面上的稍有不同,但我将其用于WP7,并相信该示例是针对C#的,因此使用了ExecuteAsync方法。

我的问题是ExecuteAsync命令应该包含什么内容。我不能使用return response.Data,因为我被警告:

代码语言:javascript
复制
'System.Action<RestSharp.RestResponse<T>,RestSharp.RestRequestAsyncHandle>' returns void, a return keyword must not be followed by an object expression

有没有人有任何关于如何解决这个问题的见解或一个可能会有所帮助的教程?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2012-04-15 01:22:07

您的代码应该如下所示:

代码语言:javascript
复制
public class HarooApi
{
    const string BaseUrl = "https://domain.here";

    readonly string _accountSid;
    readonly string _secretKey;

    public HarooApi(string accountSid, string secretKey)
    {
        _accountSid = accountSid;
        _secretKey = secretKey;
    }

    public void ExecuteAndGetContent(RestRequest request, Action<string> callback)
    {
        var client = new RestClient();
        client.BaseUrl = BaseUrl;
        client.Authenticator = new HttpBasicAuthenticator(_accountSid, _secretKey);
        request.AddParameter("AccountSid", _accountSid, ParameterType.UrlSegment);
        client.ExecuteAsync(request, response =>
        {
            callback(response.Content);
        });
    }

    public void ExecuteAndGetMyClass(RestRequest request, Action<MyClass> callback)
    {
        var client = new RestClient();
        client.BaseUrl = BaseUrl;
        client.Authenticator = new HttpBasicAuthenticator(_accountSid, _secretKey);
        request.AddParameter("AccountSid", _accountSid, ParameterType.UrlSegment);
        client.ExecuteAsync<MyClass>(request, (response) =>
        {
            callback(response.Data);
        });
    }
}

我添加了两个方法,这样您就可以检查所需的内容(来自响应体的字符串内容,或者由MyClass表示的反序列化的类)。

票数 32
EN

Stack Overflow用户

发布于 2014-02-10 22:41:52

老问题,但是如果你使用的是C# 5,你可以通过创建一个返回任务T的TaskCompleteSource来创建一个通用的execute类。你的代码可能是这样的:

代码语言:javascript
复制
public Task<T> ExecuteAsync<T>(RestRequest request) where T : new()
    {
        var client = new RestClient();
        var taskCompletionSource = new TaskCompletionSource<T>();
        client.BaseUrl = BaseUrl;
        client.Authenticator = new HttpBasicAuthenticator(_accountSid, _secretKey);
        request.AddParameter("AccountSid", _accountSid, ParameterType.UrlSegment);
        client.ExecuteAsync<T>(request, (response) => taskCompletionSource.SetResult(response.Data));
        return taskCompletionSource.Task;
    }

并像这样使用它:

代码语言:javascript
复制
private async Task DoWork()
    {
        var api = new HarooApi("MyAcoountId", "MySecret");
        var request = new RestRequest();
        var myClass = await api.ExecuteAsync<MyClass>(request);

        // Do something with myClass
    }
票数 50
EN

Stack Overflow用户

发布于 2016-03-29 10:50:48

或者更准确地说,像这样:

代码语言:javascript
复制
    public async Task<IRestResponse<T>> ExecuteAsync<T>(IRestRequest request) where T : class, new()
    {
        var client = new RestClient(_settingsViewModel.BaseUrl);

        var taskCompletionSource = new TaskCompletionSource<IRestResponse<T>>();
        client.ExecuteAsync<T>(request, restResponse =>
        {
            if (restResponse.ErrorException != null)
            {
                const string message = "Error retrieving response.";
                throw new ApplicationException(message, restResponse.ErrorException);
            }
            taskCompletionSource.SetResult(restResponse);
        });

        return await taskCompletionSource.Task;
    }
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10153749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档