【问题标题】:How to implement call to call ExecuteAsync using RestSharp如何使用 RestSharp 实现调用 ExecuteAsync 的调用
【发布时间】:2014-12-21 01:49:27
【问题描述】:

我正在尝试使用 RESTSharp 调用 REST 服务并立即继续执行,因为我只需要启动一个任务但需要立即继续执行,因此我尝试使用 ExecuteAsync 而不是 Execute。

我的代码现在应该是这样的

 IRestResponse<ExpandoObject> restResponse = client.ExecuteAsync<ExpandoObject>(restRequest, response =>
        {
           callback(response.Content);
        });

但是,我不知道如何实现回调函数,并且所有示例都没有显示它。我假设它是这样的,但它不会编译。

 private IRestResponse<ExpandoObject> callback(string content)
    {
        return null;
    }

有什么想法吗?

【问题讨论】:

    标签: c# restsharp


    【解决方案1】:

    有几种方法可以实现您正在尝试做的事情,但它看起来像您的 回调有错误的方法签名......要让一些“基本”运行,以下应该工作 (我添加了等待只是为了测试):

                EventWaitHandle resetEvent = new AutoResetEvent(false);
            client.ExecuteAsync(request, response =>
            {
                callback(response.Content);
    
                resetEvent.Set();
                return;
            });
    
            resetEvent.WaitOne();
    
        }
    
        private static void callback(string content)
        {
            System.Console.WriteLine(content);
        }
    

    【讨论】:

    • 谢谢。这行得通。我需要学习如何正确使用回调和线程,但今天看来我已经准备好了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多