xmorera
xmorera

Reputation: 1961

How to implement call to call ExecuteAsync using RestSharp

I am trying to call a REST service using RESTSharp and continuing execution immediately as I just need a task started but need to continue execution immediately so I am trying to use ExecuteAsync instead of Execute.

My code should look like this now

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

However, I have no idea how to implement the callback function and all samples don't show it. I assume it is like this but it does not compile.

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

Any ideas?

Upvotes: 0

Views: 2163

Answers (1)

Grady G Cooper
Grady G Cooper

Reputation: 1064

There are a few ways to implement what you're trying to do, but the it looks like your callback has wrong method signature...to get something "basic" running, the following should work (I added wait simply for testing):

            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);
    }

Upvotes: 2

Related Questions