2

I have read a few SO questions that touches in this question, even though many of them are several years old:

How do you write a Service handler in ServiceStack API so that it becomes async/await?

There are no docs on docs.servicestack.net that mentions async/await at all, I just find some community forum posts. I think that the only thing you need to do, to change this non-async method:

public GetBookingResponse Get(GetBooking getBooking)
{
    Booking booking = _objectGetter.GetBooking(getBooking.Id);
    return new GetBookingResponse(booking);
}

to an async method, is this:

public async Task<GetBookingResponse> Get(GetBooking getBooking)
{
    Booking booking = await _objectGetter.GetBookingAsync(getBooking.Id);
    return new GetBookingResponse(booking);
}

and by doing this, the async/await model will magically be leveraged through the call stack?

Mythz? =)

Ted
  • 19,727
  • 35
  • 96
  • 154
  • The magic here all comes from `GetBookingAsync()`. And that should also `await` something and so on. – H H Jun 20 '19 at 11:40
  • Yes, the async await thing must be used throughout, from start to finish, where finish is where the code hands it off to an async method that in turn hands it off to the OS. – Ted Jun 20 '19 at 11:42

1 Answers1

2

Yes just returning a Task will make your Services Async and non-blocking in ServiceStack.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • So my code above is correct, returning `Task` and making it `async` is all thats needed then... – Ted Jun 19 '19 at 17:08
  • @Ted FYI It just needs to return a `Task` or `Task` instance (e.g. can also return an `object` that's a `Task`), also doesn't matter if it has `async` or not. So return method signature doesn't matter, just needs to return a `Task` instance. – mythz Jun 20 '19 at 03:35
  • Doesnt matter if has `async`? Do you care to elaborate on this? That sounds like you're not using the async/await (TAP) model in .NET? – Ted Jun 20 '19 at 08:18
  • 1
    @Ted returning a `Task` (i.e. C#'s wrapper around a future result) makes a method async, the `async` keyword is just C# compiler syntactic sugar that compiles a state machine to wrap code blocks after await into the Task's continuation to give the illusion that async is sequential code so it's easier for devs to read/write, but it's no more async/non-blocking than manually constructing and returning the `Task` value (i.e. what was needed before C# had async/await and what the C# compiler does behind the scenes). – mythz Jun 20 '19 at 08:28