ExecuteAsyncPost Example in RestSharp.NetCore

2019-04-28 04:34发布

I'm working with RestSharp.NetCore package and have a need to call the ExecuteAsyncPost method. I'm struggling with the understanding the callback parameter.

    var client = new RestClient("url");
    request.AddParameter("application/json", "{myobject}",  ParameterType.RequestBody);
    client.ExecuteAsyncPost(request,**callback**, "POST");

The callback is of type Action<IRestResponse,RestRequestAsyncHandler>

Would someone please post a small code example showing how to use the callback parameter with an explanation.

Thanks -C

2条回答
对你真心纯属浪费
2楼-- · 2019-04-28 05:08

RestSharp v106 support .NET Standard 2.0 so if your code worked with RestSharp 105 under .NET Framework - it will also work with .NET Core 2.

RestSharp.NetCore package is not from RestSharp team and is not supported by us. It is also not being updated and the owner does not respond on messages, neither the source code of the package is published.

查看更多
一纸荒年 Trace。
3楼-- · 2019-04-28 05:21

This worked for me using ExecuteAsync for a Get call. It should hopefully point you in the right direction. Note that the code and credit goes to https://www.learnhowtoprogram.com/net/apis-67c53b46-d070-4d2a-a264-cf23ee1d76d0/apis-with-mvc

public void ApiTest()
    {
        var client = new RestClient("url");
        var request = new RestRequest(Method.GET);
        var response = new RestResponse();
        Task.Run(async () =>
        {
            response = await GetResponseContentAsync(client, request) as RestResponse;
        }).Wait();
        var jsonResponse = JsonConvert.DeserializeObject<JObject>(response.Content);

    }

public static Task<IRestResponse> GetResponseContentAsync(RestClient theClient, RestRequest theRequest)
    {
        var tcs = new TaskCompletionSource<IRestResponse>();
        theClient.ExecuteAsync(theRequest, response => {
            tcs.SetResult(response);
        });
        return tcs.Task;
    }
查看更多
登录 后发表回答