Post API call error handling


I’d like to use SS client to send WEB API calls asynchronously. My scenario is 2-tier server architecture where 2nd tier provides auditing services. I’d like at the end of the request handling in tier 1 to send audit request to tier 2 and end request handling (without waiting for results of the call to audit API of tier 2).

I’ve two questions related the scenario:

  1. What is a best practice to send a request asynchronously? Code example will be highly appreciated.
  2. What happens if the asynchronous request returns error? How should I handle it? My goal is to ignore it in worst case and log it into file in best case.

Thank you

What’s wrong with just calling the async ServiceClient API’s? What am I missing?

They just return .NET’s standard async Task<T> response which you handle Exceptions in a try/catch block if you’re awaiting them, otherwise you can manually chain on the Task response to subscribe to error responses. Other than our .NET C#/.NET ServiceClients docs, googling for async Exception examples returned a good article on different ways to handle Async Exceptions.

Does it mean that the following example will send response immediately:

client.GetAsync(new Hello { Name = "World!" })
.Error(ex => { throw ex; });

without waiting for async request from Hello API?

Yes calling Async methods invokes them straight away, but if you’re not going to use await you’ll also need to provide a .Success() or .ContinueWith() callback in order to access the async response.

1 Like

Hello Demis,

I’ve a POST request which doesn’t return a response (void). However, I cannot inherit the DTO class from IReturnVoid.

When I’m writing a code

client.PostAsync(new MyRequestDTO() { Field1 = 1, Field2 = "some text"})
.Success(r => {})
.Error(ex => Logger.Error("Failed to perform request", ex));

The compiler shows me errors:

error CS1502: The best overloaded method match for ‘ServiceStack.ServiceClientBase.PostAsync(ServiceStack.IReturnVoid)’ has some invalid arguments
error CS1503: Argument 1: cannot convert from ‘MyDTORequest’ to ‘ServiceStack.IReturnVoid’

Is inheriting from IReturnVoid is the only way to invoke the request asynchronously?

I’m guessing you mean you cannot implement the IReturnVoid interface. You should be able to invoke it with a string or byte[], it will just be empty, e.g:

await client.PostAsync<string>(new MyRequest { ... });